Search code examples
pythonpython-3.xpython-click

python click setuptools integration issue


I am following the setuptools example from the python click documentation here using a python3.7.1 conda virtual env and continue to receive an error:

ModuleNotFoundError: No module named 'yourpackage.scripts'

I must be missing something obvious here but not sure what it is.

yourpackage/
├── __init__.py
├── main.py
├── scripts
│   ├── __init__.py
│   └── yourscript.py
├── setup.py
└── utils.py
#scripts/yourscript.py
import click

@click.command()
def cli():
    """Example script."""
    click.echo('Hello World!')
# setup.py
from setuptools import setup, find_packages
setup(
    name='yourpackage',
    version='0.1',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'Click',
    ],
    entry_points='''
        [console_scripts]
        yourscript=yourpackage.scripts.yourscript:cli
    ''',
)
$ pip install -e .
Looking in indexes: https://InternalArtifactoryUrl
Obtaining file:///private/tmp/yourpackage
Requirement already satisfied: Click in /Users/myusername/miniconda3/envs/test_20190220/lib/python3.7/site-packages (from yourpackage==0.1) (7.0)
Installing collected packages: yourpackage
  Found existing installation: yourpackage 0.1
    Uninstalling yourpackage-0.1:
      Successfully uninstalled yourpackage-0.1
  Running setup.py develop for yourpackage
Successfully installed yourpackage

$ which yourscript
/Users/myusername/miniconda3/envs/test_20190220/bin/yourscript

$ yourscript
Traceback (most recent call last):
  File "/Users/myusername/miniconda3/envs/test_20190220/bin/yourscript", line 11, in <module>
    load_entry_point('yourpackage', 'console_scripts', 'yourscript')()
  File "/Users/myusername/miniconda3/envs/test_20190220/lib/python3.7/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/Users/myusername/miniconda3/envs/test_20190220/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2793, in load_entry_point
    return ep.load()
  File "/Users/myusername/miniconda3/envs/test_20190220/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2411, in load
    return self.resolve()
  File "/Users/myusername/miniconda3/envs/test_20190220/lib/python3.7/site-packages/pkg_resources/__init__.py", line 2417, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
ModuleNotFoundError: No module named 'yourpackage.scripts'

I also tried the following in my package directory: export PYTHONPATH=/tmp/yourpackage/ and continue to receive the error when running yourscript


Solution

  • Python path should include the directory that contains yourpackage. In this case that would be /tmp.

    However, a better approach might be to rearrange your source code so that there would be no need to adjust the python path when using setuptools. Something like this:

    yourpackage/
    ├── yourpackage/
    |   ├── __init__.py
    |   ├── main.py
    |   ├── scripts
    │   ├── __init__.py
    │   ├── yourscript.py
    |   └── utils.py
    ├── README.md
    ├── LICENSE
    ├── requirements.txt
    └── setup.py
    

    See this project on github for an example of a much larger click based project.