468,287 Members | 1,910 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

importing modules from other packages

3
Hi,

I'm having trouble with my new directory hierarchy. My little python programme worked as long as I had all my modules in one big file. I have decided now that this was just to messy and I couldn't find stuff right away anymore. So I wanted to put similar modules into packages. I have read the 'An Introduction to Python'
by Guido van Rossum bit about modules and packages. I think I did it like it was written there, but when I try to run the programme it doesn't find the modules (ImportError message).
My tree looks like this:
folder/
Intro.py ---> this is the modules that starts other modules
tried with and without _init_.py in this folder
May 4 '07 #1
3 1484
Tinka
3
sorry there is more:
again...
folder/
Intro.py
_ini_.py or not
subfolder1/
module1.py
module2.py
_init_.py
subfolder2/
subsubfolder1/
module3.py
..........
_init_.py
subsub......
_init_.py
Do I need to call my Intro.py something with main?
So far I use import subfolder1.module1
or subfolder2.subsubfolder1.module3 but it doesn't work. Also including folder. in the path doesn't help. Thanks for any advise,

Tinka
May 4 '07 #2
bvdet
2,851 Expert Mod 2GB
sorry there is more:
again...
folder/
Intro.py
_ini_.py or not
subfolder1/
module1.py
module2.py
_init_.py
subfolder2/
subsubfolder1/
module3.py
..........
_init_.py
subsub......
_init_.py
Do I need to call my Intro.py something with main?
So far I use import subfolder1.module1
or subfolder2.subsubfolder1.module3 but it doesn't work. Also including folder. in the path doesn't help. Thanks for any advise,

Tinka
Check your system path:
Expand|Select|Wrap|Line Numbers
  1. >>> import sys
  2. >>> for p in sys.path:
  3. ...     print p
  4. ...     
  5.  
  6. C:\WINDOWS\system32\python23.zip
  7. C:\Python23
  8. C:\Python23\DLLs
  9. C:\Python23\lib
  10. C:\Python23\lib\plat-win
  11. C:\Python23\lib\lib-tk
  12. ..............................
For example, to import a module from directory 'C:\\Python23\\macro'.
Expand|Select|Wrap|Line Numbers
  1. import macro.module1
  2. a = module1.function1()
OR
Expand|Select|Wrap|Line Numbers
  1. from macro.module1 import function1()
  2. a = function1()
Python will not import a module from a directory that does not contain a file named __init__.py. The file can be empty.
To add a folder to the system path:
Expand|Select|Wrap|Line Numbers
  1. if 'C:\\macro' not in sys.path:
  2.     sys.path.append('C:\\macro')
It is probably something simple that you are overlooking.
May 4 '07 #3
Tinka
3
ok, thanks, I found the mistake.
May 9 '07 #4

Post your reply

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

Similar topics

reply views Thread by Rogue 9 | last post: by
12 posts views Thread by qwweeeit | last post: by
4 posts views Thread by Peter Tillotson | last post: by
10 posts views Thread by Derek van Vliet | last post: by
reply views Thread by Jure Vrscaj | last post: by
4 posts views Thread by Fabian Braennstroem | last post: by
reply views Thread by sudharsh | last post: by
reply views Thread by Martin P. Hellwig | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.