Bob Sacamano
Bob Sacamano

Reputation: 439

setuptools: ImportError on load_entry_point

I'm trying to create an entry point when installing my package. Here is my package:

.
├── foo
│   ├── example.py
│   └── __init__.py
└── setup.py

My setup.py is:

import sys
from setuptools import setup, find_packages


console_scripts = \
    ['foo.py = foo.example:main']

setup(name='foo',
      version='1.1.1',
      author='me',
      author_email='me',
      url='',
      entry_points={'console_scripts': console_scripts},
      include_package_data=True,
      packages=find_packages(),
      zip_safe=False)

And this is example.py:

def main():
    print "hello world"

I'm installing like so:

python setup.py sdist
pip install dist/foo-1.1.1.tar.gz

It works fine. But when I try to use the entry point /usr/bin/foo.py I get this traceback:

Traceback (most recent call last):
  File "/usr/bin/foo.py", line 11, in <module>
    load_entry_point('foo==1.1.1', 'console_scripts', 'foo.py')()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 570, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2751, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2405, in load
    return self.resolve()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2411, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
ImportError: No module named example

importing the foo module in python shell works fine as well.

Upvotes: 0

Views: 1060

Answers (1)

phd
phd

Reputation: 94407

You named your entry point script foo.py. When you run it any import from foo import from the script. That is, you shadowed your package foo. The script doesn't have submodule example hence the error.

Do not name the script foo.py, name it just foo:

console_scripts = \
    ['foo = foo.example:main']

Or name it bar.py if you need .py extension. That way it wouldn't shadow package foo.

Upvotes: 2

Related Questions