Package question.

Ian Sparks Ian.Sparks at etrials.com
Mon Sep 29 15:26:35 EDT 2003


I'm sure its been thrashed through a hundred times but googling for an answer left me more confused than before and yes, I did read the Tutorial (maybe not well enough, but I read it).

On Windows, I want to set up a package structure like :

Fred
  Bob
    test.py

So I can say :

from Fred.Bob import *

To do this I need to put the package on the python path. So I drop this into my python root in a file called Fred.pth :

C:\Fred\

Now I need to make sure that Fred and Bob both look like packages with empty __init__.py files like so :

C:\Fred
   __init__.py
C:\Fred\Bob
   __init__.py
    test.py

so :

C:\python.exe -c from Fred.Bob import *

OK!

But if I do :

C:\>cd Fred
C:\Fred>python.exe -c "from Fred.Bob import *"
Traceback (most recent call last):
  File "<string>", line 1, in ?
ImportError: No module named Fred.Bob

Now lets say I change my Fred.pth contents to C:\MyTest and put my whole directory structure under c:\MyTest :

C:\MyTest\Fred
     __init__.py
C:\MyTest\Fred\Bob
       __init__.py
        test.py


C:\>cd MyTest\Fred
C:\MyTest\Fred>python.exe -c "from Fred.Bob import *"

Works fine. 

Is this quirk expected? If yes, I would like to expand my python knowledge with the why.

Thanks for your attention on this long post.











More information about the Python-list mailing list