@@ -598,13 +598,11 @@ \section{Custom Installation}
598598python setup.py install --install-purelib=Site --install-platlib=Site
599599\end {verbatim }
600600
601- The specified installation directories are relative to \filevar {prefix}.
602- Of course, you also have to ensure that these directories are in
603- Python's module search path, such as by putting a \file {.pth} file in
604- \filevar {prefix}.
605-
606- % \XXX should have a section describing \file{.pth} files and
607- % cross-ref it here
601+ The specified installation directories are relative to
602+ \filevar {prefix}. Of course, you also have to ensure that these
603+ directories are in Python's module search path, such as by putting a
604+ \file {.pth} file in \filevar {prefix}. See section~\ref {search-path }
605+ to find out how to modify Python's search path.
608606
609607If you want to define an entire installation scheme, you just have to
610608supply all of the installation directory options. The recommended way
@@ -690,6 +688,87 @@ \section{Custom Installation}
690688% installation schemes be needed on those platforms?
691689
692690
691+ % XXX I'm not sure where this section should go.
692+ \subsection {Modifying Python's Search Path }
693+ \label {search-path }
694+
695+ When the Python interpreter executes an \keyword {import} statement, it
696+ searches for both Python code and extension modules along a search
697+ path. A default value for the path is configured into the Python
698+ binary when the interpreter is built. You can determine the path by
699+ importing the \module {sys} module and printing the value of
700+ \code {sys.path}.
701+
702+ \begin {verbatim }
703+ $ python
704+ Python 2.2 (#11, Oct 3 2002, 13:31:27)
705+ [GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
706+ Type ``help'', ``copyright'', ``credits'' or ``license'' for more information.
707+ >>> import sys
708+ >>> sys.path
709+ ['', '/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2',
710+ '/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload',
711+ '/usr/local/lib/python2.3/site-packages']
712+ >>>
713+ \end {verbatim }
714+
715+ The null string in \code {sys.path} represents the current working
716+ directory.
717+
718+ The expected convention for locally installed packages is to put them
719+ in the \file {.../site-packages/} directory, but you may want to
720+ install Python modules into some arbitrary directory. For example,
721+ your site may have a convention of keeping all software related to the
722+ web server under \file {/www}. Add-on Python modules might then belong
723+ in \file {/www/python}, and in order to import them, this directory
724+ must be added to \code {sys.path}. There are several different ways to
725+ add the directory.
726+
727+ The most convenient way is to add a path configuration file to a
728+ directory that's already on Python's path, usually to the
729+ \file {.../site-packages/} directory. Path configuration files have an
730+ extension of \file {.pth}, and each line must contain a single path
731+ that will be added to \code {sys.path}. Paths can be absolute or
732+ relative, in which case they're relative to the directory containing
733+ the \file {.pth} file. Any directories added to the search path will
734+ be scanned in turn for \file {.pth} files. See
735+ \citetitle [http://www.python.org/dev/doc/devel/lib/module-site.html]{the
736+ documentation for the \module {site } module} for more information.
737+
738+ A slightly less convenient way is to edit the \file {site.py} file in
739+ Python's standard library, and modify \code {sys.path}. \file {site.py}
740+ is automatically imported when the Python interpreter is executed,
741+ unless the \programopt {-S} switch is supplied to suppress this
742+ behaviour. So you could simply edit \file {site.py} and add two lines to it:
743+
744+ \begin {verbatim }
745+ import sys
746+ sys.path.append('/www/python/')
747+ \end {verbatim }
748+
749+ However, if you reinstall the same major version of Python (perhaps
750+ when upgrading from 2.2 to 2.2.2, for example) \file {site.py} will be
751+ overwritten by the stock version. You'd have to remember that it was
752+ modified and save a copy before doing the installation.
753+
754+ There are two environment variables that can modify \code {sys.path}.
755+ \envvar {PYTHONHOME} sets an alternate value for the prefix of the
756+ Python installation. For example, if \envvar {PYTHONHOME} is set to
757+ \samp {/www/python}, the search path will be set to \code {['',
758+ '/www/python/lib/python2.2/' , '/www/python/lib/python2.3/plat-linux2' ,
759+ ...]}.
760+
761+ The \envvar {PYTHONPATH} variable can be set to a list of paths that
762+ will be added to the beginning of \code {sys.path}. For example, if
763+ \envvar {PYTHONPATH} is set to \samp {/www/python:/opt/py}, the search
764+ path will begin with \code {['/www/python', '/opt/py' ]}. (Note that
765+ directories must exist in order to be added to \code {sys.path}; the
766+ \module {site} module removes paths that don't exist.)
767+
768+ Finally, \code {sys.path} is just a regular Python list, so any Python
769+ application can modify it by adding or removing entries.
770+
771+
693772\section {Distutils Configuration Files }
694773\label {config-files }
695774
0 commit comments