import problems *newbie*

Steve Holden steve at holdenweb.com
Fri Jan 14 17:03:36 CET 2005


F. Petitjean wrote:

> Le 13 Jan 2005 21:58:36 -0800, mike kreiner a écrit :
> 
>>I am having trouble importing a module I created. I'm running PythonWin
>>on Windows XP if that helps. I saved my module in a folder called
>>my_scripts in the site-packages directory. I edited the python path to
>>include the my_scripts folder (it now reads
>>C:\Python23\Lib;C:\Python23\DLLs;C:\Python23\Lib\lib-tk;C:\Python23\Lib\site-packages\my_scripts).
> 
> Not a very godd idea to mess with the python path
> 
Furthermore it should not be necessary!

>>When I try to import the module, I get this error:
>>
>>
>>>>>from PolyDraw import *
>>
>>Traceback (most recent call last):
>>File "<interactive input>", line 1, in ?
>>ImportError: No module named PolyDraw
>>
OK, have your modifications to the path worked?

Try adding

   import sys
   print sys.path

before the import statement to verify what Python is actually using as 
the path.

>>When I select Browse PythonPath from the tools menu, I'm able to locate
>>my module, PolyDraw.py.
>>
>>The problem goes away if I open PolyDraw.py from PythonWin, which I'm
>>assuming is because opening the module makes my_scripts the current
>>working directory. This is just a quick workaround, but I'd like to
>>know how to fix the problem. Thanks.
> 
> A quick fix is to promote your my_scripts folder to be a python package,
> by creating a python module (file) named __init__.py right in the
> package directory. The content of __init__.py can be for instance

The __init__.py can actually be completely empty, but surely then you'd 
have to import the module by

   from my_scripts import PolyDraw

which is a little less convenient. It would be easier (and also easier 
than modifying the PYTHONPATH) just to create a .pth file (say 
C:\Python23\Lib\site-packages\my.pth) containing the single line

my_scripts

and that should ensure that the directory really *is* on your path.

The *name* of the .pth file is irrelevant, and you can actually have 
several lines naming different directories (whose paths can be absolute, 
or relative to the directory containing the .pth file).

Obviously you should check that the path's setting is correct using the 
technique allowed above.


> #!/usr/bin/env python
> # -*- coding: Latin-1 -*-
> """
> my_scripts package containing miscellaneous modules 
>   PolyDraw
>   ....
> """
> __author__ = 'mike kreiner'
> 
> To import from this package the syntax is
> from my_scripts import PolyDraw
> 
Let's not recommend this as a way around the problem - let's find out 
what the problem actually *is* and fix it ;-)

regards
  Steve
-- 
Steve Holden               http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC      +1 703 861 4237  +1 800 494 3119




More information about the Python-list mailing list