467,146 Members | 1,009 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

sys.path.append('/my/new/path')

Is it possible to append a new Path to some file permanently? It seems like a
sys.path.append('/my/new/path') statement is temporary.

In other words, where and in what file on a Win32 box or Linux box is the sys.path info kept. I
have a couple of paths I keep practice files in I want to add to the path permanently.

Thanks
Jul 18 '05 #1
  • viewed: 23584
Share:
3 Replies
Jeff Wagner wrote:
Is it possible to append a new Path to some file permanently? It seems like a
sys.path.append('/my/new/path') statement is temporary.


Use the PYTHONPATH environment variable.

--irmen

Jul 18 '05 #2
Jeff Wagner wrote:
Is it possible to append a new Path to some file permanently? It seems
like a sys.path.append('/my/new/path') statement is temporary.

In other words, where and in what file on a Win32 box or Linux box is the
sys.path info kept. I have a couple of paths I keep practice files in I
want to add to the path permanently.


The easiest way to extend the list of paths is to put a .pth file (a text
file with an arbitrary name, the .pth extension containing one path per
line) in the site-packages directory. For details, see

http://www.python.org/doc/current/inst/search-path.html

Peter

Jul 18 '05 #3
From the Python doc:

The most convenient way is to add a path configuration file to a
directory that's already on Python's path, usually to the
..../site-packages/ directory. Path configuration files have an extension
of .pth, and each line must contain a single path that will be appended
to sys.path. (Because the new paths are appended to sys.path, modules in
the added directories will not override standard modules. This means you
can't use this mechanism for installing fixed versions of standard
modules.)

Paths can be absolute or relative, in which case they're relative to the
directory containing the .pth file. Any directories added to the search
path will be scanned in turn for .pth files. See site module" >the
documentation for the site module for more information.

A slightly less convenient way is to edit the site.py file in Python's
standard library, and modify sys.path. site.py is automatically imported
when the Python interpreter is executed, unless the -S switch is
supplied to suppress this behaviour. So you could simply edit site.py
and add two lines to it:
import sys
sys.path.append('/www/python/')

However, if you reinstall the same major version of Python (perhaps when
upgrading from 2.2 to 2.2.2, for example) site.py will be overwritten by
the stock version. You'd have to remember that it was modified and save
a copy before doing the installation.

There are two environment variables that can modify sys.path. PYTHONHOME
sets an alternate value for the prefix of the Python installation. For
example, if PYTHONHOME is set to "/www/python", the search path will be
set to ['', '/www/python/lib/python2.2/',
'/www/python/lib/python2.3/plat-linux2', ...].

The PYTHONPATH variable can be set to a list of paths that will be added
to the beginning of sys.path. For example, if PYTHONPATH is set to
"/www/python:/opt/py", the search path will begin with ['/www/python',
'/opt/py']. (Note that directories must exist in order to be added to
sys.path; the site module removes paths that don't exist.)

Finally, sys.path is just a regular Python list, so any Python
application can modify it by adding or removing entries.
visit my homepage at http://www.homepages.lu/pu/
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Golawala, Moiz M (GE Infrastructure) | last post: by
2 posts views Thread by Tom Wells | last post: by
19 posts views Thread by Ju Hui | last post: by
2 posts views Thread by Dr. Pastor | last post: by
1 post views Thread by Frank | last post: by
5 posts views Thread by Russ | last post: by
4 posts views Thread by andrea | last post: by
3 posts views Thread by SMALLp | last post: by
4 posts views Thread by Python | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.