Original Translation
137
The expected convention for locally installed packages is to put them in the :file:`{...}/site-packages/` directory, but you may want to install Python modules into some arbitrary directory. For example, your site may have a convention of keeping all software related to the web server under :file:`/www`. Add-on Python modules might then belong in :file:`/www/python`, and in order to import them, this directory must be added to ``sys.path``. There are several different ways to add the directory.
138
The most convenient way is to add a path configuration file to a directory that's already on Python's path, usually to the :file:`.../site-packages/` directory. Path configuration files have an extension of :file:`.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.)
139
Paths can be absolute or relative, in which case they're relative to the directory containing the :file:`.pth` file. See the documentation of the :mod:`site` module for more information.
140
A slightly less convenient way is to edit the :file:`site.py` file in Python's standard library, and modify ``sys.path``. :file:`site.py` is automatically imported when the Python interpreter is executed, unless the :option:`-S` switch is supplied to suppress this behaviour. So you could simply edit :file:`site.py` and add two lines to it::
141
import sys sys.path.append('/www/python/')
142
However, if you reinstall the same major version of Python (perhaps when upgrading from 2.2 to 2.2.2, for example) :file:`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.
143
There are two environment variables that can modify ``sys.path``. :envvar:`PYTHONHOME` sets an alternate value for the prefix of the Python installation. For example, if :envvar:`PYTHONHOME` is set to ``/www/python``, the search path will be set to ``['', '/www/python/lib/pythonX.Y/', '/www/python/lib/pythonX.Y/plat-linux2', ...]``.
144
The :envvar:`PYTHONPATH` variable can be set to a list of paths that will be added to the beginning of ``sys.path``. For example, if :envvar:`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 :mod:`site` module removes paths that don't exist.)
145
Finally, ``sys.path`` is just a regular Python list, so any Python application can modify it by adding or removing entries.
146
Distutils Configuration Files