By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,714 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,714 IT Pros & Developers. It's quick & easy.

Import a module from parent package

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Peter L. Buschman wrote:
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.


See http://www.python.org/peps/pep-0328.html for what would appear to be
some related material.

-Peter
Jul 18 '05 #2

P: n/a
[snip relative import comments]

As Peter Hansen has already stated, PEP 328 is all about including this
functionality. The PEP has basically been accepted as being a good
idea, the only question is what syntax is the correct one. It should be
decided by Python 2.4.

- Josiah
Jul 18 '05 #3

P: n/a
Peter L. Buschman <pl*@iotk.com> wrote:
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.


I needed something very similar last year; I had this directory structure:

topdir/
topdir/usefulmodule.py
topdir/subdir1/
topdir/subdir1/somecode.py
topdir/subdir2/
topdir/subdir2/morecode.py

Both somecode.py and morecode.py needed to use functions from
usefulmodule.py. I solved it as follows:

----- Begin somecode.py -----
import os
import sys
# Other imports...
sys.path.append('..')
import usefulmodule
sys.path.remove('..')
..
..
..
------ End somecode.py ------

----- Begin othercode.py -----
import os
import sys
# Other imports...
sys.path.append('..')
import usefulmodule
sys.path.remove('..')
..
..
..
------ End othercode.py ------

In your case, you'd want to add '../..' to sys.path. I think this will
solve your problem.

--
Robin Munn
rm***@pobox.com
Jul 18 '05 #4

P: n/a
Thanks for the info guys! :)

--PLB

Peter Hansen <pe***@engcorp.com> wrote in message news:<Uc********************@powergate.ca>...
Peter L. Buschman wrote:
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.


See http://www.python.org/peps/pep-0328.html for what would appear to be
some related material.

-Peter

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.