I want to create a python app named knife that can be executed from CLI, the problem is that it can't import the modules. I followed the same folder structure as the Django project for reference.
My directory structure is like this:
knife/
knife/
bin/
knife-cli.py
core/
main/
__init__.py
__init__.py
__init__.py
setup.py
My setup.py looks like this:
#!/usr/bin/env python
from setuptools import setup, find_packages
exclude = ['knife.bin']
setup(name='Knife',
version='0.3',
description='Very cool project',
author='John Doe',
author_email='author@email.com',
packages=find_packages(exclude=exclude),
include_package_data=True,
scripts=['knife/bin/knife-cli.py'],
entry_points={
'console_scripts': [
'knife-cli = knife.core.main:main'
]
},
zip_safe=False,
)
My knife/core/main/__init__.py contains a main() function and my knife/bin/knife-cli.py looks like this:
#!/usr/bin/env python
from knife.core import main
if __name__ == "__main__":
main.main()
So after installing the module with setup.py install, I try to run knife-cli but keeps on throwing this error:
$ knife-cli
Traceback (most recent call last):
File "/usr/bin/knife-cli", line 9, in <module>
load_entry_point('Knife==0.3', 'console_scripts', 'knife-cli')()
File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 468, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 2563, in load_entry_point
return ep.load()
File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 2254, in load
['__name__'])
File "/usr/bin/knife.py", line 4, in <module>
File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 646, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 1559, in run_script
raise ResolutionError("No script named %r" % script_name)
pkg_resources.ResolutionError: No script named 'knife.py'
What is really happening? and how can I solve it?
You need some more __init__'s. The __init__.py file tells python that the folder is a python module. You are refrencing these as modules in your setup script, so you need to tell python that they are modules.
knife/
knife/
bin/
knife-cli.py
core/
main/
__init__.py
__init__.py
__init__.py
setup.py
That should fix the main problem. However, you are also declaring two scripts, one using the scripts section and another using the console_scripts. Console scripts will actually create the script for you, so you dont need to include your own in "bin".
Here is a better setup.py for you: (note I just removed the scripts section)
#!/usr/bin/env python
from setuptools import setup, find_packages
exclude = ['knife.bin']
setup(name='Knife',
version='0.3',
description='Very cool project',
author='John Doe',
author_email='author@email.com',
packages=find_packages(exclude=exclude),
include_package_data=True,
entry_points={
'console_scripts': [
'knife-cli = knife.core.main:main'
]
},
zip_safe=False,
)
Got it, The script was executing the old /usr/bin/knife.pyc file, I just deleted it and now works well.
来源:https://stackoverflow.com/questions/30221573/pythons-setup-py-installed-cli-script-doesnt-allow-importing-same-module