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='[email protected]',
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?
This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.
As the setup.py file is installed via pip (and pip itself is run by the python interpreter) it is not possible to specify which Python version to use in the setup.py file.
Setuptools is a package development process library designed to facilitate packaging Python projects by enhancing the Python standard library distutils (distribution utilities). Essentially, if you are working with creating and distributing Python packages, it is very helpful.
Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .
Got it, The script was executing the old /usr/bin/knife.pyc
file, I just deleted it and now works well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With