app name in python import path

1.2k views Asked by At

How do I use "relative static" paths in my import path

For example, if I have an app named asdf, with the following directory

asdf/
 +- asdf/
 |   +- main.py
 |   +- config.py
 +- tests/
 etc.

Currently I have in my main.py...

from config import Config

I want to be able to use...

from asdf.config import Config

The reason why is that the modules are getting moved around as the project is still in its infancy, relatively speaking. (no pun intended)

3

There are 3 answers

0
hspandher On BEST ANSWER

You can easily find a workaround this problem. But presence of modules/folders with same name as its parent always causes issues, especially when both the parent and child module are in python path. Since django1.6, django also names project folder and internal folder similarly, which causes lot of issues, especially in testing.

So as a norm try to avoid naming modules same as parent.

0
ToddJCrane On

I found the answer (or possibly just a workaround).

If I "compile" it with setuptools it will work.

0
sadaf2605 On
asdf/
 +- asdf/
 |   +- asdf/
 |       config.py
 |   +- main.py
 +- tests/
 etc.

if you have this directory structure then you can import Config method of config.py at main.py using this from asdf.config import Config