Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's setup.py installed CLI script doesn't allow importing same module

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?

like image 249
gbriones.gdl Avatar asked May 13 '15 17:05

gbriones.gdl


People also ask

Why can't I import modules in python?

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.

How do I set Python version in setup py?

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.

What is Python Setuptools used for?

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.

How do I fix Python module not found?

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 .


1 Answers

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

like image 154
gbriones.gdl Avatar answered Sep 22 '22 07:09

gbriones.gdl