19

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?

ThoseKind
  • 754
  • 2
  • 13
  • 22
  • ```(env) $ python``` are you in a Anaconda environment (or similar) or are you just stating the directory you are currently in? If its a directory, perhaps try backing out one directory and trying again. – Fletchy Aug 28 '19 at 15:35
  • 1
    That is just to show the commands are being executed from the shell and with the `venv` activated. I've tried it from the directory containing `setup.py` as well as the directory above it. Also, I've edited the question to make my first statement more apparent. – ThoseKind Aug 28 '19 at 15:40
  • https://stackoverflow.com/questions/44834/can-someone-explain-all-in-python – Dinko Pehar Aug 29 '19 at 07:01
  • I've been facing similar issues with [frappe](https://github.com/frappe/frappe) as well, which installs all its apps in editable mode. However, the problem exists only in macOS currently. any distro of Linux works just fine. – gavin Mar 30 '20 at 11:00

2 Answers2

1

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.

Sam
  • 101
  • 1
  • 5
-1

It may work if you modify the setup.py to

from setuptools import setup, find_packages

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

Then, you can then run pip install -e from the root directory to install the package in editable mode.

Seth P
  • 82
  • 1
  • 6