1

I am writing some scripts for a bigger machine learning application. To keep it nicely structured, I have some subdirectories for different steps, e.g.

/
/preprocessing (containing preprocess.py)
/training (containing train.py)
/utils (config.py)

So what I would like to have, is a clean possibility to use code from utils in preprocessing and training modules. However, the problem is that I run the code directly in the subdirectories, e.g.

cd preprocessing
python3 preprocess.py

So this means that preprocessing is my main module and this cannot see anything that is contained in a higher directory, thus I also cannot import modules form utils.

I know that there would be some possibilities that include changing the PYTHONPATH, but I find this somehow ugly. Everybody using my code would have to do this. So my question is if there is a clean and recommended way of importing code from parent or sibling directories.

Simon Hessner
  • 1,757
  • 1
  • 22
  • 49

1 Answers1

1

You can use relative imports as it presented in documentation. So you can try something like this in your preprocess module:

from ..utils import config
Lev Zakharov
  • 2,409
  • 1
  • 10
  • 24
  • Then I get: 'ValueError: attempted relative import beyond top-level package' – Simon Hessner Aug 02 '18 at 11:53
  • 1
    The top answer here https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time answers the question quite good. Short: When calling python modules as scripts, relative imports does not work because __name__ = "__main_". It only works when using python -m my.package.module. Thus, I will switch to deploying my scripts as a package using setuptools. – Simon Hessner Aug 02 '18 at 14:10
  • Yes, you found a good explanation! If I were you, I would move the running scripts to the top of the project. And in the modules themselves, I would leave only their logic. – Lev Zakharov Aug 02 '18 at 15:34
  • Yes, but than I would have 1000 scripts in the root of the project. I think it is better to install my project using python setup.py install/develop and then just import everything as `from myproject.mymodule import bla` – Simon Hessner Aug 02 '18 at 20:54