[Tutor] Windows IDE ..., Problems while executing python code ?

Jeff Shannon jeff@ccvcorp.com
Mon, 10 Sep 2001 17:44:41 -0700


> Hi,
>
> I am getting a suprising error with Windows IDE, When I want to test my code
> on windows to check I jsut run uing Ctrl + F5, but when I have a import
> statements for the user defined modules ..., python is telling me Import
> error...like this...,
>
>  File "C:\Python20\projects\VirtualHandset\utils.py", line 8, in ?
>     from HandSetUtils import *
> ImportError: No module named HandSetUtils
>
> But suprisingly my modules are present there it self. Then I just copied
> these files to the python installation directory...it started working
> fine..., then I deleted from python installation directory..., but it
> started working fine...then I closed all windows ..., this time again it
> started giving the above problem...,

You're almost certainly on the right track, in that it's a path problem.  Python is not looking in your \projects\* directory for modules, and won't unless you specifically tell it to.  When you put your modules into the regular python install dir, then they were found.  As
for why it worked after you deleted those copies, until you shut down PythonWin (I'm presuming that's what you mean by Windows IDE, but the same would happen with IDLE), you have to know how module imports work.  Once Python has loaded a module, further calls to import it
simply add a reference to the existing module in memory.  Since you'd already imported your modules, the IDE didn't need to find the files to do a second import, so it never noticed that you'd deleted them.  However, once you shut down the IDE and restarted it, then the
modules were (obviously) no longer in memory and needed to be loaded from the files... and you're back to where you were to start off with.

The simplest solution to this whole problem, is to create a text file in your main Python directory (c:\Python20\).  The file needs to have the extension .pth (best bet is to name it python.pth).  Each line in this file should contain a directory name.  Each directory so
named (and any subdirectories, IIRC), will be added to Python's search path.  So, if you create this file with the single line  "C:\Python20\projects", your problem should be solved.  If that doesn't work, you *might* need to add another line
"C:\Python20\projects\VirtualHandset".  Once you have this file, restart your IDE.  From then on, you should be set.

HTH

Jeff Shannon
Technician/Programmer
Credit International