ref: python doc - Installing Python Modules (Legacy version)
python setup.py build
python setup.py install
1. build
python setup.py build
the build command is responsible for putting the files to install into a build directory.
change the build dir:
python setup.py build --build-base=/path/to/pybuild/foo-1.0
2. install
python setup.py install
the install command is relatively simple: copy everything under build/lib (or build/lib.plat ) to your chosen installation directory.
2.1 alternate installation
<u>The basic idea is that you supply a base directory for the installation, and the install command picks a set of directories (called an installation scheme) under this base directory in which to install files.</u>
--user
--home
--prefix
--exec-prefix
--install-base
--install-platbase
2.1.1 alternate installation - the user scheme
When don’t have write permission to the global site-packages directory or don’t want to install into it:
python setup.py install --user
Type of file | Installation directory |
---|---|
modules | userbase/lib/pythonX.Y/site-packages |
scripts | userbase/bin |
data | userbase |
C headers | userbase/include/pythonX.Y/distname |
2.1.2 Alternate installation: the home scheme
when you build and maintain a personal stash of Python modules. just like Unix's "home" dir.
e.g. python setup.py install --home=~
Type of file | Installation directory |
---|---|
modules | home/lib/python |
scripts | home/bin |
data | home |
C headers | home/include/python/distname |
2.1.3 Alternate installation: Unix (the prefix scheme)
when you wish to use one <u>Python installation</u> to perform the build/install (i.e., to run the setup script), but install modules into the <u>third-party module directory</u> of a different Python installation (or something that looks like a different Python installation).
e.g. /usr/local/bin/python setup.py install --prefix=/mnt/@server/export
Type of file | Installation directory |
---|---|
Python modules | prefix/lib/pythonX.Y/site-packages |
extension modules | exec-prefix/lib/pythonX.Y/site-packages |
scripts | prefix/bin |
data | prefix |
C headers | prefix/include/pythonX.Y/distname |
every time you run python setup.py install without any other options, you’re using --prefix and --exec-prefix.
2.2 Custom installation
Type of file | Override option |
---|---|
Python modules | --install-purelib |
extension modules | --install-platlib |
all modules | --install-lib |
scripts | --install-scripts |
data | --install-data |
C headers | --install-headers |
python setup.py install --home=~/python \
--install-purelib=lib \
--install-platlib='lib.$PLAT' \
--install-scripts=scripts
--install-data=data
or use Distutils config file (see section Distutils Configuration Files):
[install]
install-base=$HOME/python
install-purelib=lib
install-platlib=lib.$PLAT
install-scripts=scripts
install-data=data
2.2.1 Modifying Python’s Search Path
When the Python interpreter executes an import
statement, it searches for both Python code and extension modules along a search path. A default value for the path is configured into the Python binary when the interpreter is built. You can determine the path by importing the sys
module and printing the value of sys.path.
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.)
import sys
sys.path.append('/www/python/')