470,628 Members | 2,312 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,628 developers. It's quick & easy.

Import a module from parent package

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
4 7063
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
[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
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
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.

Similar topics

2 posts views Thread by Markus Doering | last post: by
reply views Thread by Stian Søiland | last post: by
reply views Thread by Remy C. Cool | last post: by
4 posts views Thread by Steve Holden | last post: by
5 posts views Thread by Steve Holden | last post: by
7 posts views Thread by Ron Adam | last post: by
49 posts views Thread by Martin Unsal | last post: by
12 posts views Thread by Alan Isaac | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.