[Tutor] editing Path

Christopher Arndt chris.arndt at web.de
Mon Nov 6 13:25:26 CET 2006


Eli Brosh schrieb:
> Hello.
> I am beginning to use python on windows.
> how can I add a path to Python search-list where I can store my python
> scripts ?
> On IDLE the command *browse path* lets me to see the existing path but
> not to edit it.

Hello,

you may be confusing* two different concepts here:

1) The system shell search path for executables, i.e. the PATH environment
variable. This is used by the shell (i.e. cmd.exe aka the command box on
Windows) to find executable programs when you type in only the name of a
program without the full path.

You can change the PATH environment variable in the 'System' dialog in the
Windows control panel (somewhere on the "Advanced" tab).

2) The Python search path, which is used by the Python interpreter to locate
modules that should be imported. This has a default value, which is compiled
into the Python executable and can be queried and changed through the sys.path
variable at runtime, e.g.

$ python
Python 2.4.3 (#2, Oct  6 2006, 07:52:30)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['', '/usr/lib/python24.zip', '/usr/lib/python2.4',
'/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-tk',
'/usr/lib/python2.4/lib-dynload', '/usr/local/lib/python2.4/site-packages',
'/usr/lib/python2.4/site-packages', '/usr/lib/site-python']


This example shows the content of sys.path on my Linux system (I deleted a lot
of additional entries for the sake of brevity), on a Windows system this will
of course be different and will point to the library directory of your Python
installation, usually in 'C:/Python24/Lib'.


To change the path, just append/prepend entries to sys.path, which is a normal
Python list, e.g.

>>> sys.path.append('/home/joe/myproject/lib')
>>> sys.path.insert(0, '/home/joe/myotherproject/lib')

Changes to sys.path last only until your program exits. To make permanent
changes to your Python path, you can set the PYTHONPATH environment variable.
The contents of this variable will be prepended to the default sys.path.


* At least your mentioning of "python scripts" makes me think so. The Python
path only concerns Python *modules*, Python scripts are like any other
executable, batch file, shell script etc. and need to be place in a directory
in your PATH when you want to call them without a full path name.

HTH, Chris


More information about the Tutor mailing list