CPython 的 C 扩展是共享库 (如
.so
文件在 Linux,
.pyd
在 Windows),导出
初始化函数
.
要可导入,共享库必须可用于
PYTHONPATH
,且必须以模块名称命名,带有适当扩展名。当使用 distutils 时,自动生成正确文件名。
初始化函数带有签名:
它返回完全初始化模块,或
PyModuleDef
实例。见
初始化 C 模块
了解细节。
对于采用仅 ASCII 名称的模块,函数必须被命名为
PyInit_<modulename>
,采用
<modulename>
替换模块名称。当使用
多阶段初始化
,非 ASCII 模块名称是允许的。在这种情况下,初始化函数名称是
PyInitU_<modulename>
,采用
<modulename>
编码使用 Python 的
punycode
编码采用下划线替换连字符。在 Python 中:
def initfunc_name(name):
try:
suffix = b'_' + name.encode('ascii')
except UnicodeEncodeError:
suffix = b'U_' + name.encode('punycode').replace(b'-', b'_')
return b'PyInit' + suffix
从单个共享库导出多个模块是可能的,通过定义多个初始化函数。不管怎样,导入它们要求使用符号链接或自定义导入器,因为默认情况下只能找到对应文件名的函数。见 在一个库中多个模块 章节在 PEP 489 了解细节。
可以使用 Python 中包括的 distutils 构建扩展模块。由于 distutils 还支持创建二进制包,因此,用户不一定需要编译器和 distutils 来安装扩展。
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 additional 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
文件;见
指定要分发的文件
了解细节。
若源分发已成功构建,维护者还可以创建二进制分发。从属平台,要做到这可以使用以下命令之一。
python setup.py bdist_wininst
python setup.py bdist_rpm
python setup.py bdist_dumb