Starting in Python 1.4, Python provides, on Unix, a special make file for building make files for building dynamically-linked extensions and custom interpreters. Starting with Python 2.0, this mechanism (known as related to Makefile.pre.in, and Setup files) is no longer supported. Building custom interpreters was rarely used, and extension modules can be built using distutils.
Building an extension module using distutils requires that distutils is installed on the build machine, which is included in Python 2.x and available separately for Python 1.5. Since distutils also supports creation of binary packages, users don’t necessarily need a compiler and distutils to install the extension.
distutils 包包含驱动程序脚本
setup.py
。这是纯 Python 文件,在最简单情况下,可能看起来像这样:
from distutils.core import setup, Extension
module1 = Extension('demo',
sources = ['demo.c'])
setup (name = 'PackageName',
version = '1.0',
description = 'This is a demo package',
ext_modules = [module1])
采用此
setup.py
,和文件
demo.c
,运行
python setup.py build
将编译
demo.c
,和产生的扩展模块名为
demo
在
build
目录。从属系统,模块文件将结束于子目录
build/lib.system
,且可能有名称像
demo.so
or
demo.pyd
.
在
setup.py
,所有执行的履行是通过调用
setup
function. This takes a variable number of keyword arguments, of which the example above uses only a subset. Specifically, the example specifies meta-information to build packages, and it specifies the contents of the package. Normally, a package will contain of addition modules, like Python source modules, documentation, subpackages, etc. Please refer to the distutils documentation in
分发 Python 模块 (旧版)
to learn more about the features of distutils; this section explains building extension modules only.
It is common to pre-compute arguments to
setup()
, to better structure the driver script. In the example above, the
ext_modules
自变量对于
setup()
is a list of extension modules, each of which is an instance of the
Extension
. In the example, the instance defines an extension named
demo
which is build by compiling a single source file,
demo.c
.
In many cases, building an extension is more complex, since additional preprocessor defines and libraries may be needed. This is demonstrated in the example below.
from distutils.core import setup, Extension
module1 = Extension('demo',
define_macros = [('MAJOR_VERSION', '1'),
('MINOR_VERSION', '0')],
include_dirs = ['/usr/local/include'],
libraries = ['tcl83'],
library_dirs = ['/usr/local/lib'],
sources = ['demo.c'])
setup (name = 'PackageName',
version = '1.0',
description = 'This is a demo package',
author = 'Martin v. Loewis',
author_email = 'martin@v.loewis.de',
url = 'https://docs.python.org/extending/building',
long_description = '''
This is really just a demo package.
''',
ext_modules = [module1])
在此范例中,
setup()
is called with additional meta-information, which is recommended when distribution packages have to be built. For the extension itself, it specifies preprocessor defines, include directories, library directories, and libraries. Depending on the compiler, distutils passes this information in different ways to the compiler. For example, on Unix, this may result in the compilation commands
gcc -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -DMAJOR_VERSION=1 -DMINOR_VERSION=0 -I/usr/local/include -I/usr/local/include/python2.2 -c demo.c -o build/temp.linux-i686-2.2/demo.o gcc -shared build/temp.linux-i686-2.2/demo.o -L/usr/local/lib -ltcl83 -o build/lib.linux-i686-2.2/demo.so
These lines are for demonstration purposes only; distutils users should trust that distutils gets the invocations right.
当已成功构建扩展,有 3 种方式使用它。
最终用户通常想要安装模块,做到这通过运行
python setup.py install
模块维护者应产生源代码包;要做到这运行
python setup.py sdist
在某些情况下,在源代码分发中需要包括额外文件;做到这是透过
MANIFEST.in
file; see the distutils documentation for details.
若源分发已成功构建,维护者还可以创建二进制分发。从属平台,要做到这可以使用以下命令之一。
python setup.py bdist_wininst python setup.py bdist_rpm python setup.py bdist_dumb