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

Packages

P: n/a
Hi All -

I'm having a problem and I hope you can help. I can't seem to import
packages from within the package substructure as I think I should be
able to. For example, I create a directory structure as follows:

testpkg
__init__.py [empty]
testsub1/
__init__.py [empty]
bad.py [import testpkg.testsub2.good; print "hello from bad"] <-
error
testsub2/
__init__.py [empty]
good.py [print "hello from good"]

Whenever I try to run bad.py (just python bad.py from within the
testsub2 subdirectory or from above the testpkg directory) I get an
error. For example:

$ python testpkg/testsub1/bad.py
Traceback (most recent call last):
File "testpkg/testsub1/bad.py", line 1, in <modul
import testpkg.testsub2.good
ImportError: No module named testpkg.testsub2.good

How can I get my subpackages to recognize other subpackages in the
same top-level package? Thanks in advance!!

Kevin

Jul 27 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Kevin T. Ryan" <ke**********@gmail.comwrote in
news:11**********************@b79g2000hse.googlegr oups.com:
Hi All -

I'm having a problem and I hope you can help. I can't seem to import
packages from within the package substructure as I think I should be
able to. For example, I create a directory structure as follows:

testpkg
__init__.py [empty]
testsub1/
__init__.py [empty]
bad.py [import testpkg.testsub2.good; print "hello from bad"] <-
error
testsub2/
__init__.py [empty]
good.py [print "hello from good"]

Whenever I try to run bad.py (just python bad.py from within the
testsub2 subdirectory or from above the testpkg directory) I get an
error. For example:

$ python testpkg/testsub1/bad.py
Traceback (most recent call last):
File "testpkg/testsub1/bad.py", line 1, in <modul
import testpkg.testsub2.good
ImportError: No module named testpkg.testsub2.good

How can I get my subpackages to recognize other subpackages in the
same top-level package? Thanks in advance!!

Kevin
When executing bad.py python will be looking for imported module (in the
standard path) and then in your current directory (so to find it you
should have testsub1/testpkg/testsub2). You can solve this by adding
your toplevel director (the parent of testpkg) to the PYTHONPATH.

bests,
../alex
--
..w( the_mindstorm )p.

Jul 27 '07 #2

P: n/a
On Jul 27, 7:21 pm, Alex Popescu <nospam.themindst...@gmail.com>
wrote:
"Kevin T. Ryan" <kevin.t.r...@gmail.comwrote innews:11**********************@b79g2000hse.google groups.com:
Hi All -
I'm having a problem and I hope you can help. I can't seem to import
packages from within the package substructure as I think I should be
able to. For example, I create a directory structure as follows:
testpkg
__init__.py [empty]
testsub1/
__init__.py [empty]
bad.py [import testpkg.testsub2.good; print "hello from bad"] <-
error
testsub2/
__init__.py [empty]
good.py [print "hello from good"]
Whenever I try to run bad.py (just python bad.py from within the
testsub2 subdirectory or from above the testpkg directory) I get an
error. For example:
$ python testpkg/testsub1/bad.py
Traceback (most recent call last):
File "testpkg/testsub1/bad.py", line 1, in <modul
import testpkg.testsub2.good
ImportError: No module named testpkg.testsub2.good
How can I get my subpackages to recognize other subpackages in the
same top-level package? Thanks in advance!!
Kevin

When executing bad.py python will be looking for imported module (in the
standard path) and then in your current directory (so to find it you
should have testsub1/testpkg/testsub2). You can solve this by adding
your toplevel director (the parent of testpkg) to the PYTHONPATH.

bests,
./alex
--
.w( the_mindstorm )p.
Ok, thanks. I was under the impression that this wouldn't be
necessary (eg, see http://docs.python.org/tut/node8.htm...0000000000000),
but perhaps I was wrong. Thanks again for the advice :)

Jul 28 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.