Python Forum

Full Version: ModuleNotFoundError when application is not installed via setup.py
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there,

i've packaged my program and uploaded it to pypi. When installed via pip everything works flawlessly. However, when cloning the repository, installing all dependencies and running the main application (here: runner.py) i get a ModuleNotFoundError.

Error:
Traceback (most recent call last): File "dpp/runner.py", line 34, in <module> from dpp.core.argparse.ordered_multi_args import OrderedMultiArgs ModuleNotFoundError: No module named 'dpp'
I use following folder structure:

Output:
decoder-plus-plus ├── dpp │   ├── core │   ├── images │   ├── __init__.py │   ├── __main__.py │   ├── plugins │   ├── runner.py │   └── ui ├── LICENSE ├── MANIFEST.in ├── README.md ├── requirements.txt └── setup.py
setup.py
...
setup(
    ...
    entry_points={
        "console_scripts": [
            "dpp=dpp.runner:main",
        ]
    },
)
I fixed the error by adding following code at the beginning of runner.py:
# FIX #27: Add 'dpp' to python package path if not present. 
#          This may happen when dpp was not installed via setup.py.
DPP_PACKAGE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if DPP_PACKAGE_PATH not in sys.path:
    sys.path.append(DPP_PACKAGE_PATH)
However, i don't think that my approach is common practice and that there is a more elegant solution to this.

I hope that all makes sense.

Any help would be much appreciated!

Note: The complete source code can be found at github.
I think you can simply do
Output:
pip install /path/to/decoder-plus-plus
If you intend to edit the files in the local copy of the repository, you could use instead
Output:
pip install --editable /path/to/decoder-plus-plus
allowing the imported module to stay up to date as you edit the files.
Hi Gribouillis,

thanks for your reply. Didn't know about this option. However, i still wonder. Is my project setup correct? Is my fix common practice or is there any other way around this issue?

Output:
$ pip3 install decoder-plus-plus $ cd /usr/lib/python3.8/dist-packages/dpp/ $ python3 runner.py # works flawlessly with or without the fix mentioned in the first post
Output:
$ git clone https://github.com/bytebutcher/decoder-plus-plus $ cd decoder-plus-plus/dpp/ $ python3 runner.py # produces error without the fix mentioned in the first post Traceback (most recent call last): File "dpp/runner.py", line 34, in <module> from dpp.core.argparse.ordered_multi_args import OrderedMultiArgs ModuleNotFoundError: No module named 'dpp'
No, your fix is not standard practice. If 'runner.py' contains this
from dpp.core...
it means that runner.py is designed to run on a system where the dpp module is installed. The normal command to install dpp is pip. You shouldn't need to change the module's code to make it work on your system.

You could also consider creating a symbolic link
Output:
/directory/on/the/python/path/dpp -> /path/to/directory/decoder-plus-plus
You can also manipulate sys.path from within a sitecustomize.py or usercustomize.py file.