0

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

1 Answer 1

1

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.

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

3 Comments

That did it, thank you. Now, just wait for the downvotes to flood in :). By the way, where would be the best place to put this PYTHONPATH export in my application?
You should consider using a cleaner arrangement of the source tree for your project which isolates the package source from the "meta". Done correctly, you shouldn't need to modify your PYTHONPATH. Here's an article about one way to arrange things: docs.python-guide.org/writing/structure
I read the article but it's not clear what thing(s) would help isolate 'the package source from the "meta"' so that I wouldnt need to modify the PYTHONPATH.

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.