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

importing modules from other packages

P: 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
Share this Question
Share on Google+
3 Replies


P: 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
Expert Mod 2.5K+
P: 2,851
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

P: 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.