relative imports and sub-module execution

King animator333 at
Mon Sep 27 19:54:28 CEST 2010


After reading couple of docs and articles, I have implemented a simple
test package with nested modules.
When running "", everything is working fine. Some of my sub-
modules has some small test routines for debug purpose.
It's because I am using relative package imports at the top, I am not
able to run these sub modules individually.

For example, this works when running from

Here is example structure

in folder2/, I am importing

from core.folder1 import f1a
print f1a.myvar

Now I can't execute '' individually. It's giving an error:

ImportError: No module named core.folder2

Is this mean when you have created a package where modules are using
relative imports, they can't execute individually?

Another solution that I have discovered is using sys.path. Check this:

import sys
import os
from core.folder1 import f1a
print f1a.myvar

This works fine and easier too. I can execute modules individually as
well as package is also working. Are there any disadvantages when
using above technique? Couple of articles suggests that this is a bad
practice and should not be used.

Need some clarifications.



More information about the Python-list mailing list