[Tutor] Idle (and sys.path)

Jeff Shannon jeff@ccvcorp.com
Mon, 14 Jan 2002 10:26:20 -0800


> "Paul Hartley" <p.hartley@spitech.com> wrote:
>
> I have just started to use Idle and have prited out the manual. Looking =
> at using the debugger it says type in the module called dvtest.py save =
> it as a file and then import it.
>
> Now I did that but saved the file dvtest.py somewhere more convenient =
> for my source code, not in the python directories, now when I type =
> import dvtest.py it cannot find the module. It will not let me type in =
> import c:\source\python etc. so how do I get python (or idle) to look =
> elsewhere for the module? (I have looked at the documentation for import =
> and it talks about sys.path, so maybe I could add directories to =
> sys.path in Idle?

You can indeed do this.  A simpler way is to have Python do this automatically
for you.

Create a file, in your c:\python21 directory (or wherever you have python
installed), with the name python.pth (the name itself isn't important, just that
it ends in .pth).  In this file, list (one per line) any directories that you
want Python to search for imported modules.  So in your case, if the contents of
this file are:

-------------------
c:\source\python
-------------------

then any attempt to import a file will look for that file in c:\source\python as
well as the standard directories.

Jeff Shannon
Technician/Programmer
Credit International