Package Question - please help.

Peter Hess plhess at cvzoom.net
Sat Dec 15 00:08:53 EST 2001


"A. Keyton Weissinger" <keyton at weissinger.org> wrote in
news:mailman.1008384979.29742.python-list at python.org: 

> I have (on a Win2K box) the following dir setup:
> 
> c:\package1
>            \__init__.py
>            \subpackage1
>                        \__init__.py
>                     \myCode.py
>           \subpackage2
>                        \__init__.py
>                     \testMyCode.py
> 
> testMyCode.py is a unit test for myCode.py. It attempts to load a class
> from within myCode.py with something like the following:
> 
> from package1.subpackage1.myCode import MyClass
> 
> I've added c:\package1 to the path (by adding it in the registy -- is
> there an easier way?).

Yep.  For Win2k, right-click My Computer | Properties | Advanced tab, 
click Environment Variable button, add your environment variables in the 
appropriate box (probably System).  The one you want to add is PYTHONPATH, 
not PATH.  Python uses PYTHONPATH (among other things) to determine where 
to look for modules.  You'll need to restart your command shell to pick 
this change up.  More info in the docs (look for PYTHONPATH).

> If I attempt to run testMyCode.py from within subpackage2, it dies and
> gives me:
> 
> Traceback (most recent call last):
>   File "testMyCode.py", line 3, in ?
>     from package1.subpackage1.myCode import MyClass
> ImportError: No module named package1.subpackage1.myCode

Because the interpreter is looking for a package named 
package1.subpackage1.myCode in the search path, which doesn't contain a 
reference to c:\ (where the root of this package is).  Check the search 
path interactively like so:

>>> import sys
>>> sys.path
['', 'path1', 'path2', ... ]

The empty string at the beginning represents the current working 
directory.  os.getcwd() will tell you what it is at the moment.

> BUT, if I move that file to the root (c:\) and run it from there, it
> runs like a champ.

Now the root of the package (package1) is in the CWD, which is on the 
module search path.

-- 
Peter Hess
<plhess at cvzoom.net>


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list