Reputation: 903
My source tree is as follows. I am using python3.7
:
proj/
src/
__init__.py
module1.py
module2.py
venv
__main__.py
__main__.py
imports module1
, which imports module2
. I am running this in a virtual environment. See below:
In __main__.py
:
import src.module1
...
In module1.py
:
import module2
...
Note that I do have the __init__.py
file in the src
directory.
When I run __main__.py
, I end up getting an import error:
ModuleNotFoundError: No module named 'module2'
What I have found is if I run the program outside of the virtual environment, module1
is able to import module2
, but in the venv
, I hit this error. I am stumped as to how to get this work properly as I believe I have done everything correctly here, using absolute imports and an __init__.py
.
Upvotes: 0
Views: 516
Reputation: 2990
Python import's are so sensitive! I broke my entire project the other day...
First:
export PYTHONPATH=/path/to/proj/
Secondly:
import src.module2
Thirdly: In main.py if you are calling a single function or variable do
from src.module2 import x,y
Do this instead of ... import *
Upvotes: 1
Reputation: 903
The absolute paths were not used within the src
modules, so I wasn't actually using absolute paths, which was my problem. In other words, change module1.py
to be:
import src.module2
...
Upvotes: 0