Installed Python setup.py CLI script does not allow importing the same module

I want to create a python application called knife that can be executed from the CLI, the problem is that it cannot import modules. I followed the same folder structure as the Django project for reference.

My directory structure is 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 the 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 installing setup.py, I try to start knife-cli, but continue to throw 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 going on? and how can i solve it?

+5
source share
2 answers

You will need another __init__ . The __init__.py file tells python that the folder is a python module. You redefine them as modules in your script setup, 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 

This should solve the underlying problem. However, you also declare two scripts: one uses the scripts section, and the other console_scripts . Console scripts will actually create a script for you, so you don’t need to include your own in the β€œbin”.

Here is the best setup.py for you: (note that I just deleted 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, ) 
+3
source

Got it, the script was running the old /usr/bin/knife.pyc file, I just deleted it and now works fine.

+5
source