ModuleNotFoundError in Python 3.7.3 Setup.py Package

Viewed 1287

I have a Python package in the format:

mypackage
├── README.md
├── doc
│   └── README.md
├── examples
│   └── README.md
├── setup.py
└── src
    ├── __init__.py
    ├── core
    │   ├── __init__.py
    │   └── main.py
    └── test
        └── README.md

It has an accompanying setup.py file:

import os
from setuptools import setup, find_packages


def read(fname):
    """
    Reads the README functions are prints them into the long_description in
    the setup routine.

    Parameters
    ----------
    fname : README file name

    Returns
    -------
    Rendered README

    """
    return open(os.path.join(os.path.dirname(__file__), fname)).read()


classifiers = [
    "Development Status :: 3 - Alpha",
    "Intended Audience :: Developers",
    "Programming Language :: Python",
]

def run_setup():
    """
    This functions holds the setup command. Rather than running setup directly,
    it is wrapped in a 'try-except' that will print out errors if they occur.
    """
    setup(
        name='My Package',
        version='0.1',
        description='My package',
        long_description=read('README.md'),
        long_description_content_type='text/markdown',
        classifiers=classifiers,
        packages=['src', 'src.core'],
        python_requires='>=2.7.9',
        entry_points="""
        [console_scripts]
        my_package = src.core.main:main
        """,
    )


try:
    run_setup()
except SystemExit as e:
    print(e)

When I run python setup.py develop or pip install -e ., the package says it installs successfully. However, when I run my_package, it gives the error:

Traceback (most recent call last):
  File "/Users/mm/opt/anaconda3/bin/my_package", line 33, in <module>
    sys.exit(load_entry_point('My-Package', 'console_scripts', 'my_package')())
  File "/Users/mm/opt/anaconda3/bin/my_package", line 25, in importlib_load_entry_point
    return next(matches).load()
  File "/Users/mm/opt/anaconda3/lib/python3.7/site-packages/importlib_metadata/__init__.py", line 105, in load
    module = import_module(match.group('module'))
  File "/Users/mm/opt/anaconda3/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'src.core'

I have the __init__.py files in the appropriate places. I've tried using setuptools' explicit find_packages routine. What am I doing wrong here?

1 Answers

Here are two things you can check. Often times, after doing check 1 your console script starts working. If not, try check 2 and see if it pinpoints you the problem.

Check 1: Correct python executable & pip combination

Sometimes the plain pip install installs using wrong python executable. Try to force the install with the same python executable as you are using when running the script.

python -m pip install -e .

To be even more sure, you can check your python executable path and use it directly in place of python:

python -c "import sys; print(sys.executable)"

Check 2: The code runs without the console script

Then, check that you can run the script directly. If not in the src/core/main.py already, add

if __name__ == '__main__':
    main()

and then, try to run the script directly with

python src/core/main.py
Related