Import a module from parent package
Peter L. Buschman
plb at iotk.com
Fri Mar 19 15:19:13 EST 2004
I have a package tree laid out like this
foo/bar/module/submodule
foo/bar/module/submodule/test
foo/bar/module/submodule/test/test1.py
foo/bar/module/submodule/test/test2.py
...
What I want is for test1.py and test2.py to be able
to import their parent "submodule" as part of a regression
testing scheme. I could do this by putting "import foo.bar.module.submodule"
in them, but this code won't necessarily always be installed in foo/bar.
Is there a way to do something like this from test1.py and test2.py?
import __parent__.__parent__.submodule
That way, no matter where the entire "module" package gets installed, the
regression tests will always be able to find and import their parent modules
whose functionality they need to test.
Essentially, this is like referencing ../../submodule from the command-line.
--PLB
More information about the Python-list
mailing list