问题
After exhaustive research on this topic the past few days, I still don't understand what is the proper way to handle python path for a specific project. I describe below the situation that I am in and the exact question I have about it.
Problem statement
I have downloaded a project that I want to work on from GitHub. An abstract structure of the project is represented below, having removed unnecessary components:
project
|-- experiments
| |-- cnn_experiments
| |-- __init__.py
| |-- train.py
| |-- datasets
| |-- __init__.py
| |-- module_A.py
|-- src
|-- cnn
|-- io
|-- __init__.py
|-- module_B.py
Note: from the structure above are missing only some directories under cnn directory with some extra functionalities. Except for this, the structure is the same as I downloaded it from GitHub.
In train.py
there is the following import
statement:
from cnn_experiments.datasets.module_A import Class_A
From inside cnn_experiments directory I run python train.py
command and I get this error:
ImportError: No module named cnn_experiments.datasets.module_A
Similarly, I get the corresponding error when I run module_A.py
that has the import
statement:
from cnn.io.module_B import Class_B
Especially for the second case, that the path is completely different, when I use print(sys.path)
from inside train.py
I can tell that Python can't resolve where my import
refers to, but the problem is why.
Question
- In this case, that the project is small, how can I have full access to import all the directories? I came across with some answers which refer to changing PYTHONPATH in .bashrc file. But it doesn't make sense to me configuring .bashrc exporting to it the path for every project.
- Since I understand that having full access from every module to every other module in a project is not generally desired for the sake of abstractness and independence, how can I set up the path so that the modules to be imported are visible?
Thanks in advance for any help!
EDIT: I forgot to mention that I use Python 2.7 on Ubuntu 18.04 LTS.
来源:https://stackoverflow.com/questions/65014153/how-path-should-be-configured-for-a-python-project