deficiencyOn
deficiencyOn

Reputation: 27

Import a python class from an upper directory

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?

Upvotes: 1

Views: 104

Answers (1)

Camille Tolsa
Camille Tolsa

Reputation: 261

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

Upvotes: 1

Related Questions