472,133 Members | 1,177 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

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 6040
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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.