469,358 Members | 1,566 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

accesing modules in sibling packages

9
Hi,
I've create a file hierarchy:
test/
__init__.py
one/
__init__.py
one.py
two/
__init__.py
two.py


all the __init__.py files are empty.
the code in one.py is
Expand|Select|Wrap|Line Numbers
  1. import test.two
  2. print two.v
  3.  
the code in two.py is
Expand|Select|Wrap|Line Numbers
  1. v=0
  2.  

but when i run one.py i get an error:

Traceback (most recent call last):
File "C:\Python25\MyScripts\Test\1\one.py", line 1, in <module>
import Test.two
ImportError: No module named Test.two
Sep 11 '07 #1
4 5919
bvdet
2,851 Expert Mod 2GB
Hi,
I've create a file hierarchy:
test/
__init__.py
one/
__init__.py
one.py
two/
__init__.py
two.py


all the __init__.py files are empty.
the code in one.py is
Expand|Select|Wrap|Line Numbers
  1. import test.two
  2. print two.v
  3.  
the code in two.py is
Expand|Select|Wrap|Line Numbers
  1. v=0
  2.  

but when i run one.py i get an error:

Traceback (most recent call last):
File "C:\Python25\MyScripts\Test\1\one.py", line 1, in <module>
import Test.two
ImportError: No module named Test.two
Try this:
Expand|Select|Wrap|Line Numbers
  1. import test.two.two
Sep 11 '07 #2
noama
9
Try this:
Expand|Select|Wrap|Line Numbers
  1. import test.two.two
Nope. still the same error.
Sep 12 '07 #3
elcron
43
try this:
Expand|Select|Wrap|Line Numbers
  1. import sys; sys.path.insert(0, "..")
  2. import two.two
  3. print two.two.v
  4.  
Though in the test/two/__init__.py I would add:
Expand|Select|Wrap|Line Numbers
  1. from two import *
  2.  
and change the code in one.py to:
Expand|Select|Wrap|Line Numbers
  1. import sys; sys.path.insert(0, "..")
  2. import two
  3. print two.v
  4.  
Sep 13 '07 #4
bartonc
6,596 Expert 4TB
try this:
Expand|Select|Wrap|Line Numbers
  1. import sys; sys.path.insert(0, "..")
  2. import two.two
  3. print two.two.v
  4.  
Though in the test/two/__init__.py I would add:
Expand|Select|Wrap|Line Numbers
  1. from two import *
  2.  
and change the code in one.py to:
Expand|Select|Wrap|Line Numbers
  1. import sys; sys.path.insert(0, "..")
  2. import two
  3. print two.v
  4.  
Nice trick, my friend!
Oct 7 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Nick Coghlan | last post: by
15 posts views Thread by Nick Coghlan | last post: by
3 posts views Thread by Dennis Clark | last post: by
4 posts views Thread by Fabian Braennstroem | last post: by
7 posts views Thread by tinnews | last post: by
reply views Thread by Martin P. Hellwig | last post: by
5 posts views Thread by John Ladasky | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.