Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pip Install Editable Package Produces ModuleNotFoundError

Due to import issues, I've followed the steps shown here to install my Python project as an editable pip package. Basically this entails running pip install -e . from my project root directory. The name of the project is 'myproject', with the setup.py configured as such:

from setuptools import setup, find_packages

setup(name='myproject', version='1.0', packages=find_packages())

The project structure is like so:

.
├── myproject
│   ├── core
│   │   ├── core.py
│   │   └── __init__.py
│   └── tests
│       ├── __init__.py
│       └── test_one.py
├── setup.py
└── env
    └── ...

With the venv activated, I get the following output:

(env) [root@localhost /]$ python -V
Python 3.6.3

(env) [root@localhost /]$ pip -V
pip 9.0.1 from /myproject/venv/lib64/python3.6/site-packages (python 3.6)

However, when running an interpreted session I experience the following:

(env) [root@localhost /]$ python
>>> import myproject
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'myproject'
>>> from myproject.core import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'myproject'

Starting up another interpreted session and running the setuptools stuff myself results in:

(env) [root@localhost /]$ python
>>> from setuptools import find_packages
>>> find_packages()
>>> ['core', 'tests']

I've also tried other methods of installing it, including:

python -m pip install -e .

And still get the same problems. Finally, I can do the following:

(env) [root@localhost /] pip list installed | grep myproject
myproject (1.0, /myproject)

UPDATE: As shown here and as mentioned by @Fletchy1995 below, changing the directory structure so that it is instead like:

.
├── setup.py
├── myproject
│   ├── core
│   │   ├── core.py
│   │   └── __init__.py
│   └── tests
│       ├── __init__.py
│       └── test_one.py
├── __init__.py
└── env
    └── ...

And modifying setup.py to look like:

from setuptools import setup

setup(name='myproject', version='1.0', packages=['myproject'])

along with running pip install -e . from the top level directory seems to have fixed the problem. But in the previous case, even though the packages loaded include all sub-packages of 'myproject', why can I still not do something like:

(env) [root@localhost /]$ python
>>> from myproject.core import *

as 'myproject' is listed in pip?

like image 545
ThoseKind Avatar asked Sep 14 '25 03:09

ThoseKind


1 Answers

At first glance, it appears you are missing __init__.py in your myproject subdirectory. Python does not consider a directory to be a package/subpackage unless it contains __init__.py.

like image 170
Sam Avatar answered Sep 15 '25 16:09

Sam