Unable to pun Python scripts via Pypi package - python

I'm trying to publish a python package, and I'm having trouble getting it to run after installing it via pip. I believe this is due to my "entry_points" property in my setup.py file.
Currently I can download the project via pip install my_package_name. When I try to run the package I enter my_package_name and get a NameError saying initialize() is not defined. However, if I enter python path/to/my_package_name/__main__.py it runs just fine. Below is my project tree and my setup.py and __main__.py files for reference:
my_package_name/
setup.py
my_package_name/
__init__.py
__main__.py
my_package_name.py
setup.py:
from distutils.core import setup
setup(
name = 'my_package_name',
version = '0.0.1',
packages = ['my_package_name'],
install_requires = ['this', 'that'],
license = 'Creative Commons Attribution-Noncommercial-Share Alike license',
description = 'My cool python package',
long_description = open('readme.txt').read(),
url = 'mypackagename.github.io',
author = 'My Name',
entry_points = {
'console_scripts': [
'my_package_name = my_package_name.__main__:main' # this is likely what I'm doing wrong
]
},
)
__main__.py:
from my_package_name import *
def main(args=None):
"""The main routine."""
if args is None:
args = sys.argv[1:]
initialize()
do_something()
do_something_else()
if __name__ == "__main__":
main()
If more info is needed please let me know. This is my first swing at python, so there might be a big, common-knowledge piece that I'm missing. Any help or suggestions are greatly appreciated. Thanks!
Edit
Here is the full error log when running my_package_name
Traceback (most recent call last):
File "C:\Program Files (x86)\Python36-32\Scripts\my_package_name-script.py", line 11, in <module>
load_entry_point('my_package_name==0.0.1', 'console_scripts', 'my_package_name')()
File "c:\program files (x86)\python36-32\lib\site-packages\my_package_name\__main__.py", line 12, in main
initialize()
NameError: name 'initialize' is not defined

Related

Python/C++ Extension, Undefined symbol error when linking a library

I made a project that has glfw as a library, my directory looks like this:
main_dir
|--include
|--|--glfw_binder.h
|--src
|--|--glfw_binder.cpp
|--lib
|--|--glfw
|--|--|--src
|--|--|--|--libglfw.so
|--|--|--|--libglfw.so.3
|--|--|--|--libglfw.so.3.3
|--|--|--|--...
|--|--|--include
|--|--|-- ...
|--main.cpp
|--setup.py
|--test.py
I've installed glfw from its website and simply extracted it to lib/ directory. Then I ran cmake -G "Unix Makefiles" followed by make to build it.
When I try to run it as pure C++ with this cmake file it runs just fine, it displays the glfw window:
cmake_minimum_required(VERSION 3.0)
project(Test)
add_subdirectory(lib/glfw)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_executable(Test main.cpp src/glfw_binder.cpp)
target_link_libraries(Test PRIVATE
${PYTHON_LIBRARIES}
glfw
)
And here is what my setup.py looks like:
from setuptools import setup, Extension, find_packages
module1 = Extension('test',
sources = ['main.cpp', 'src/glfw_binder.cpp'],
include_dirs=["include", "lib/glfw/include"],
library_dirs=["lib/glfw"],
libraries=["lib/glfw"],
extra_link_args=["lib/glfw"]
)
setup (name = 'python_test',
version = '1.0',
description = 'This is a demo package',
packages=find_packages("glfw"),
ext_modules = [module1]
)
Then I run python3 setup.py build followed by sudo python3 setup.py install. It creates the so files just fine. Then I run this test code:
import test
test.create_glfw_window()
When I run this script via python3 test.py I got this error:
Traceback (most recent call last):
File "/home/turgut/Desktop/TestDir/Python-Binder-Test/test.py", line 3, in <module>
import test
ImportError: /usr/local/lib/python3.10/dist-packages/python_test-1.0-py3.10-linux-x86_64.egg/test.cpython-310-x86_64-linux-gnu.so: undefined symbol: glfwDestroyWindow
It's not specific to glfwDestroyWindow it says this for all glfw functions. How am I supposed to link this?
Update: I've added these lines:
packages=['glfw'],
package_dir={'glfw': 'lib/glfw'},
#package_data={'glfw': ['lib/glfw/src/*.so']},
package_data={'glfw': ['lib/glfw/src/libglfw.so', "lib/glfw/src/libglfw.so.3", "lib/glfw/src/libglfw.so.3.3"]},
Now it gives me the following error:
ImportError: libglfw.so.3: cannot open shared object file: No such file or directory
Even though I've specified all the .so files together. I don't think this is the way to go but I thought it's worth mentioning.
Better update:
I've removed the above update and added these two lines to my extension:
include_dirs=["include", "lib/glfw/include"],
#extra_link_args=[ "-lm", "-lGL", "-lGLU", "-lglfw"],
library_dirs=["lib/glfw"],
libraries=['glfw']
and now I'm getting this error which I think is closer to what I'm looking for:
/usr/bin/ld: cannot find -lglfw: No such file or directory
collect2: error: ld returned 1 exit status
error: command '/usr/bin/x86_64-linux-gnu-g++' failed with exit code 1
For anyone facing a similar issue, you need to specify runtime_library_dirs so the setup.py file looks like this:
from setuptools import setup, Extension, find_packages
module1 = Extension('nerveblox',
sources = ['main.cpp', 'src/nerveblox_VM.cpp'],
include_dirs=["include", "lib/glfw/include"],
#extra_link_args=[ "-lm", "-lGL", "-lGLU", "-lglfw"],
library_dirs=["lib/glfw/src"],
libraries=['glfw', 'GL'],
runtime_library_dirs=['lib/glfw/src']
)
setup (
name = 'Nerveblox_python_test',
version = '1.0',
description = 'This is a demo package',
ext_modules = [module1],
install_requires=[
'importlib-metadata; python_version == "3.8"',
],
)

ImportError: cannot import name packages

I am using cx_Freeze version 5.1.1 and I have a python 2.7.9 application to package using cx_Freeze. The application is using the python 'requests' module (the version of requests is 2.18.4)
Here is my cx_Freeze setup.py file (note I am specifically including 'requests' in the packages to include):
from cx_Freeze import setup, Executable
import sys
from cx_Freeze import setup, Executable
packages_to_include =['lib','lib/DB','encodings.ascii','requests']
buildOptions = dict(
optimize=1,
excludes = ['tkinter'],
bin_includes = [
'libcrypto.so.1.0.0',
'libcrypto.so.10',
'libgssapi_krb5.so.2',
'libk5crypto.so.3',
'libkeyutils.so.1',
'libssl.so.1.0.1e',
'libssl.so.10'
],
includes = packages_to_include,
packages= ['urllib3','idna']
)
executables = [
Executable(
'workapp.py',
targetName = 'workapp'
)
]
setup(
name='Sample Flask App',
version = '0.1',
description = 'Sample Flask App',
#requires = ["requests"],
options = dict(build_exe = buildOptions),
executables = executables
)
The build completed successfully creating the executable, but upon running my application I got the below error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/cx_Freeze/initscripts/__startup__.py", line 14, in run
module.run()
File "/usr/local/lib/python2.7/site-packages/cx_Freeze/initscripts/Console.py", line 26, in run
exec(code, m.__dict__)
File "vaas.py", line 2, in <module>
import requests
File "/usr/local/lib/python2.7/site-packages/requests/__init__.py", line 113, in <module>
from . import packages
ImportError: cannot import name packages
I am thinking it is either an issue with my cx_Freeze setup or with requests, but since I tell cx_Freeze to include 'requests', this should work.
I checked in the build directory (that cx_Freeze builds that the requests modules has 'packages' defined and I can find it:
[user#centos-vm]$ ls build/exe.linux-x86_64-2.7/lib/requests/packages/
chardet idna urllib3
Any help is much appreciated!
Found a workaround to this issue:
edit:
/usr/local/lib/python2.7/site-packages/requests/__init__.py
and comment out:
from . import packages
Very strange issue indeed. My unfrozen application works just fine, but the above workaround is needed when using cx_freeze.
Try to add 'request' to the packages list option instead of adding it to the includes list options:
packages= ['urllib3', 'idna', 'requests']
According to the cx_Freeze documentation, the build_exe option includes is a
comma separated list of names of modules to include
whereas the build_exe option packages is a
comma separated list of packages to include, which includes all submodules in the package

Make CLI with argparse and setuptools

I have a module sfind.py. How can I do this so that I can run the script by the file name without the py prefix?
python sfind instead python sfind.py
sfind.py
import argparse
def createParser():
some logic
return parser
def main(namespace):
some logic
if __name__ == '__main__':
parser = createParser()
namespace = parser.parse_args()
main(namespace)
I tried to do it with setuptools, maked new file setup.py in same directory.
setup.py
from setuptools import setup
setup(
name='sfind',
version='0.1',
packages=['sfind'],
entry_points={
'console_scripts': [
'sfind=sfind:__main__'
]
})
But error occurred
python: can't open file 'sfind': [Errno 2] No such file or directory
Can anybody help me do it right?
File structure:
-sfind
--__init__.py
--__main__.py
--sfind.py
--test.py
You don't need setuptools to call your module as python sfind. Setuptools will allow you to call by just sfind. To call it with python sfind, you have to make your sfind a module and you can do this by making a python directory sfind with __init__.py, __main__.py, and sfind.py in the directory. Then, you can call with just python sfind. If you just want to call it with sfind, you can use setup tools, but it is recommended you provide callable function (e.g. sfind=sfind.sfind:main) instead of module itself (__main__) as your entry_points. You can then do either python setup.py install or python setup.py sdist then pip install.

Include CefPython library in setup.py

I am working on packaging our Python app and Py2App on Mac is not including CefPython library which forms the basis of our app. From what I can see in the app contents, it includes the entire Python3 library, but not CEFPython. How can I add CefPython in setup.py? Currently, when I am generating the .app file and executing it, I get an error and Mac asks me if I want to open the console. I see nothing in install.log
setup.py :
"""
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = ['Our_APP.py']
DATA_FILES = []
OPTIONS = {}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Updated script
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
from setuptools import setup
APP = ['20notes.py']
DATA_FILES = []
OPTIONS = {'packages':['cefpython3','objc']}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app','cefpython3']
)
Updated error :
Traceback (most recent call last):
File "setup.py", line 18, in <module>
setup_requires=['py2app','cefpython3'],
TypeError: None is not a string
I have tried removing the comma, removing cefpython3 option, nothing works. Any idea. THank you.
Try adding 'cefpython3' package to OPTIONS:
OPTIONS = {
'packages' : ['cefpython3', 'objc'],
}
Also set this:
os.environ['MACOSX_DEPLOYMENT_TARGET'] = "10.9"

py2app will not include PySide modules

Simple hello world QT python script. Works fine from the command line. When I package it i get:
Traceback (most recent call last):
File "/Users/jquick/bin/dist/gui.app/Contents/Resources/__boot__.py", line 340, in <module>
_run('/Users/jquick/bin/gui.py')
File "/Users/jquick/bin/dist/gui.app/Contents/Resources/__boot__.py", line 336, in _run
execfile(scriptpath, globals(), globals())
File "/Users/jquick/bin/gui.py", line 3, in <module>
from PySide.QtCore import *
ImportError: No module named PySide.QtCore
2012-06-02 00:23:04.823 gui[4835:707] gui Error
So it sounds like its not including the module.. but ive tried including it in both the setup.py and the command line. nothing works :(
setup.py:
"""
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = ['gui.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True, 'includes': ['PySide.QtCore', 'PySide.QtGui']}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
i've tried creating with both the --alias option and without. Even tried labeling them as packages. But nothing I do seems to include them.
Can Python find PySide.QtCore? At the command line type:
from PySide.QtCore import *
If (1) works then make sure the Python version your invoking when executing py2app at the command line is the same Python version you're using at step (1). Some operating systems such as Mac OS X come installed with an older version of Python, and if your application works correctly when invoking it at the command line, then be sure you're not invoking a totally different version of python when trying to build your app.

Categories

Resources