1

I defined a directory named common in the root directory of my project. Basically, there isn't a main file, but rather lots of scripts which I want to directly run.

What I want to do is something like that:

from ../../common/ import <python_file>

Is it possible without too much of a hustle?

Would I need to put __init__.py to make it work?

  • 1
    There is another way of doing it using [`imp`](https://stackoverflow.com/a/301298/1704235) module. However always use `virtualenv` as suggested by @Camille unless you have very specific reason. – MaNKuR Apr 16 '18 at 13:56

1 Answers1

1

The use of .. for imports is really a bad practice. What you should do is that. Create a virtualenv using virtualenvwrapper and then use add2virtualenv common

OR

Add the path of the root directory of your project into your PYTHONPATH env variable

export PYTHONPATH=$PYTHONPATH:/path/of/my/project

An __init__.py file must be placed in every folder containing python code

Camille Tolsa
  • 261
  • 6
  • 13
  • Is there a third option? What is the common practice to import a `common` package? the suggested solutions seems like an hack to me (I'm a python newbie though) – deficiencyOn Apr 16 '18 at 13:47
  • Python developpers always use a virtualenv for their projects, its easy to use, and you MUST use it. I reccommand you to read this documentation https://virtualenvwrapper.readthedocs.io/en/latest/ – Camille Tolsa Apr 16 '18 at 13:49