0

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.

lezaf
  • 482
  • 2
  • 10
  • The "proper" way how one should handle such things is for the project to be installable, e.g. via ``setup.py`` or ``pyproject.toml``. Everything else is a workaround. – MisterMiyagi Nov 26 '20 at 12:40
  • Ok. So a `setup.py` file is needed to build automatically all dependencies, right? But now, since for example this project I work on doesn't come with a `setup.py` file, is there a _trick_ to have access to all directories? – lezaf Nov 26 '20 at 12:44
  • Not if you are simultaneously asking for the proper way, without PYTHONPATH, and without ``setup.py``. – MisterMiyagi Nov 26 '20 at 12:50
  • I am interested in two things: 1) I want to know what is the _proper_ way to handle a python project so that it can be used _easily_ by others, etc (which you already answered to me) and 2) if there is a trick for now since the project is not mine and I just want to _investigate_ how it works. – lezaf Nov 26 '20 at 12:54

0 Answers0