python2安装_安装Python模块(旧版)

自定义安装¶

Sometimes, the alternate installation schemes described in section

Alternate Installation just don’t do what you want. You might want to tweak just

one or two directories while keeping everything under the same base directory,

or you might want to completely redefine the installation scheme. In either

case, you’re creating a custom installation scheme.

To create a custom installation scheme, you start with one of the alternate

schemes and override some of the installation directories used for the various

types of files, using these options:

文件类型

覆盖选项

Python 模块

--install-purelib

扩展模块

--install-platlib

所有模块

--install-lib

scripts

--install-scripts

data

--install-data

C headers

--install-headers

These override options can be relative, absolute,

or explicitly defined in terms of one of the installation base directories.

(There are two installation base directories, and they are normally the same—

they only differ when you use the Unix “prefix scheme” and supply different

--prefix and --exec-prefix options; using --install-lib will

override values computed or given for --install-purelib and

--install-platlib, and is recommended for schemes that don’t make a

difference between Python and extension modules.)

For example, say you’re installing a module distribution to your home directory

under Unix—but you want scripts to go in ~/scripts rather than

~/bin. As you might expect, you can override this directory with the

--install-scripts option; in this case, it makes most sense to supply

a relative path, which will be interpreted relative to the installation base

directory (your home directory, in this case):

python setup.py install --home=~ --install-scripts=scripts

Another Unix example: suppose your Python installation was built and installed

with a prefix of /usr/local/python, so under a standard installation

scripts will wind up in /usr/local/python/bin. If you want them in

/usr/local/bin instead, you would supply this absolute directory for the

--install-scripts option:

python setup.py install --install-scripts=/usr/local/bin

(This performs an installation using the “prefix scheme,” where the prefix is

whatever your Python interpreter was installed with— /usr/local/python

in this case.)

If you maintain Python on Windows, you might want third-party modules to live in

a subdirectory of prefix, rather than right in prefix

itself. This is almost as easy as customizing the script installation directory

—you just have to remember that there are two types of modules to worry about,

Python and extension modules, which can conveniently be both controlled by one

option:

python setup.py install --install-lib=Site

The specified installation directory is relative to prefix. Of

course, you also have to ensure that this directory is in Python’s module

search path, such as by putting a .pth file in a site directory (see

site). See section Modifying Python’s Search Path to find out how to modify

Python’s search path.

If you want to define an entire installation scheme, you just have to supply all

of the installation directory options. The recommended way to do this is to

supply relative paths; for example, if you want to maintain all Python

module-related files under python in your home directory, and you want a

separate directory for each platform that you use your home directory from, you

might define the following installation scheme:

python setup.py install --home=~ \

--install-purelib=python/lib \

--install-platlib=python/lib.$PLAT \

--install-scripts=python/scripts

--install-data=python/data

或者,等价于

python setup.py install --home=~/python \

--install-purelib=lib \

--install-platlib='lib.$PLAT' \

--install-scripts=scripts

--install-data=data

$PLAT is not (necessarily) an environment variable—it will be expanded by

the Distutils as it parses your command line options, just as it does when

parsing your configuration file(s).

Obviously, specifying the entire installation scheme every time you install a

new module distribution would be very tedious. Thus, you can put these options

into your Distutils config file (see section Distutils Configuration Files):

[install]

install-base=$HOME

install-purelib=python/lib

install-platlib=python/lib.$PLAT

install-scripts=python/scripts

install-data=python/data

或者,等价于

[install]

install-base=$HOME/python

install-purelib=lib

install-platlib=lib.$PLAT

install-scripts=scripts

install-data=data

Note that these two are not equivalent if you supply a different installation

base directory when you run the setup script. For example,

python setup.py install --install-base=/tmp

would install pure modules to /tmp/python/lib in the first case, and

to /tmp/lib in the second case. (For the second case, you probably

want to supply an installation base of /tmp/python.)

You probably noticed the use of $HOME and $PLAT in the sample

configuration file input. These are Distutils configuration variables, which

bear a strong resemblance to environment variables. In fact, you can use

environment variables in config files on platforms that have such a notion but

the Distutils additionally define a few extra variables that may not be in your

environment, such as $PLAT. (And of course, on systems that don’t have

environment variables, such as Mac OS 9, the configuration variables supplied by

the Distutils are the only ones you can use.) See section Distutils Configuration Files

for details.

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.

$ python

Python 2.2 (#11, Oct 3 2002, 13:31:27)

[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import sys

>>> sys.path

['', '/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2',

'/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload',

'/usr/local/lib/python2.3/site-packages']

>>>

The null string in sys.path represents the current working directory.

The expected convention for locally installed packages is to put them in the

…/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 /www.

Add-on Python modules might then belong in /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.

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. See the documentation of

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/pythonX.Y/',

'/www/python/lib/pythonX.Y/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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值