1

I have a Python package called cmdline. I use setuptools to define a console entry point. I would like to put this entry point inside a cli submodule, but I get an error when I try to run the installed script.

My project layout looks like this.

setup.py
cmdline/
    __init__.py
    cli/
        __init__.py
        main.py

The setup.py looks like this.

from setuptools import setup

setup(
    name='cmdline',
    version='1.0.0',
    packages=['cmdline'],
    url='',
    license='',
    author='W.P. McNeill',
    author_email='',
    description='',
    entry_points={
        'console_scripts': ['cmdline=cmdline.cli.main:main'],
    }
)

The main.py file looks like this.

def main():
    print("Hello, world")


if __name__ == "__main__":
    main()

Both __init__.py files are empty.

If I install this with python setup.py install and then try to run the console script, I get an error.

> cmdline
Traceback (most recent call last):
  File "//anaconda/envs/cmdline/bin/cmdline", line 9, in <module>
    load_entry_point('cmdline==1.0.0', 'console_scripts', 'cmdline')()
  File "build/bdist.macosx-10.5-x86_64/egg/pkg_resources/__init__.py", line 542, in load_entry_point
  File "build/bdist.macosx-10.5-x86_64/egg/pkg_resources/__init__.py", line 2569, in load_entry_point
  File "build/bdist.macosx-10.5-x86_64/egg/pkg_resources/__init__.py", line 2229, in load
  File "build/bdist.macosx-10.5-x86_64/egg/pkg_resources/__init__.py", line 2235, in resolve
ImportError: No module named cli.main

If, however, I install it via a softlink with python setup.py develop it works.

> cmdline
Hello, world

It also works if I don't use a cli submodule and just have main.py at the top level of the project.

How can I make the submodule configuration work?

1 Answer 1

2

Your setup.py doesn't include the cmdline.cli subpackage, only the cmdline package will be included. setuptools does not recursively add all subpackages, you need to explicitly specify all packages, or use the find_packages() function to do so:

from setuptools import setup, find_packages

setup(
    name='cmdline',
    version='1.0.0',
    packages=find_packages(),
    # or:
    # packages=['cmdline', 'cmdline.cli']
    url='',
    license='',
    author='W.P. McNeill',
    author_email='',
    description='',
    entry_points={
        'console_scripts': ['cmdline=cmdline.cli.main:main'],
    }
)

After that, the cmdline.cli package will be installed and the entry point should be resolveable.

Sign up to request clarification or add additional context in comments.

1 Comment

Oops sorry. That extra cmdline directory above was a typo. I've edited the original question to fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.