3. 配置 Python

3.1. 建置需求

要建置 CPython,你將需要:

在 3.5 版的變更: 在 Windows 上現在需要 Visual Studio 2015 或更新版本。

在 3.6 版的變更: Selected C99 features, like <stdint.h> and static inline functions, are now required.

在 3.7 版的變更: 執行緒支援現在是必要的。

在 3.11 版的變更: 現在需要 C11 編譯器、IEEE 754 和 NaN 支援。在 Windows 上需要 Visual Studio 2017 或更新版本。

See also PEP 7 "Style Guide for C Code" and PEP 11 "CPython platform support".

3.1.1. 可選模組的需求

Some optional modules of the standard library require third-party libraries installed for development (for example, header files must be available).

Missing requirements are reported in the configure output. Modules that are missing due to missing dependencies are listed near the end of the make output, sometimes using an internal name, for example, _ctypes for ctypes module.

If you distribute a CPython interpreter without optional modules, it's best practice to advise users, who generally expect that standard library modules are available.

建置可選模組的依賴項目為:

Dependency

Minimum version

Python 模組

libbz2

bz2

libffi

建議使用 3.3.0 版本

ctypes

liblzma

lzma

libmpdec

2.5.0

decimal [1]

libreadlinelibedit [2]

readline

libuuid

_uuid [3]

ncurses [4]

curses

OpenSSL

3.0.18 recommended
(1.1.1 minimum)

sslhashlib [5]

SQLite

3.15.2

sqlite3

Tcl/Tk

8.5.12

tkinterIDLEturtle

zlib

1.2.2.1

zlibgzipensurepip

zstd

1.4.5

compression.zstd

Note that the table does not include all optional modules; in particular, platform-specific modules like winreg are not listed here.

也參考

在 3.1 版的變更: tkinter 現在需要 Tcl/Tk version 8.3.1。

在 3.5 版的變更: tkinter 現在需要 Tcl/Tk version 8.4。

在 3.7 版的變更: hashlibssl 現在需要 OpenSSL 1.0.2。

在 3.10 版的變更: hashlibssl 現在需要 OpenSSL 1.1.1。sqlite3 現在需要 SQLite 3.7.15。

在 3.11 版的變更: tkinter 現在需要 Tcl/Tk version 8.5.12。

在 3.13 版的變更: sqlite3 現在會需要 SQLite 3.15.2。

3.2. 產生的檔案

To reduce build dependencies, Python source code contains multiple generated files. Commands to regenerate all generated files:

make regen-all
make regen-stdlib-module-names
make regen-limited-abi
make regen-configure

The Makefile.pre.in file documents generated files, their inputs, and tools used to regenerate them. Search for regen-* make targets.

3.2.1. 設定腳本

The make regen-configure command regenerates the aclocal.m4 file and the configure script using the Tools/build/regen-configure.sh shell script which uses an Ubuntu container to get the same tools versions and have a reproducible output.

The container is optional, the following command can be run locally:

autoreconf -ivf -Werror

The generated files can change depending on the exact versions of the tools used. The container that CPython uses has Autoconf 2.72, aclocal from Automake 1.16.5, and pkg-config 1.8.1.

在 3.13 版的變更: Autoconf 2.71 and aclocal 1.16.5 and are now used to regenerate configure.

在 3.14 版的變更: 現在使用 Autoconf 2.72 來重新產生 configure

3.3. 設定選項

使用以下命令列出所有 configure 腳本選項:

./configure --help

請見 Python 原始碼發行版中的 Misc/SpecialBuilds.txt

3.3.1. 一般選項

--enable-loadable-sqlite-extensions

Support loadable extensions in the _sqlite extension module (default is no) of the sqlite3 module.

請見 sqlite3 模組的 sqlite3.Connection.enable_load_extension() 方法。

在 3.6 版被加入.

--disable-ipv6

停用 IPv6 支援(如果支援的話預設是啟用的),請見 socket 模組。

--enable-big-digits=[15|30]

Define the size in bits of Python int digits: 15 or 30 bits.

By default, the digit size is 30.

PYLONG_BITS_IN_DIGIT 定義為 1530

參閱 sys.int_info.bits_per_digit

--with-suffix=SUFFIX

將 Python 執行檔的後綴設定為 SUFFIX

在 Windows 和 macOS 上預設的後綴是 .exepython.exe 執行檔)、在 Emscripten node 上為 .js、在 Emscripten 瀏覽器為 .html、在 WASI 上為 .wasm,以及在其他平台為空字串(python 執行檔)。

在 3.11 版的變更: 在 WASM 平台上預設的後綴是 .js.html.wasm 中的一個。

--with-tzpath=<list of absolute paths separated by pathsep>

Select the default time zone search path for zoneinfo.TZPATH. See the Compile-time configuration of the zoneinfo module.

預設值:/usr/share/zoneinfo:/usr/lib/zoneinfo:/usr/share/lib/zoneinfo:/etc/zoneinfo

請見 os.pathsep 路徑分隔符號。

在 3.9 版被加入.

--without-decimal-contextvar

Build the _decimal extension module using a thread-local context rather than a coroutine-local context (default), see the decimal module.

請見 decimal.HAVE_CONTEXTVARcontextvars 模組。

在 3.9 版被加入.

--with-dbmliborder=<list of backend names>

Override order to check db backends for the dbm module

A valid value is a colon (:) separated string with the backend names:

  • ndbm;

  • gdbm;

  • bdb.

--without-c-locale-coercion

Disable C locale coercion to a UTF-8 based locale (enabled by default).

不要定義 PY_COERCE_C_LOCALE 巨集。

請見 PYTHONCOERCECLOCALEPEP 538

--with-platlibdir=DIRNAME

Python 函式庫目錄名稱(預設為 lib )。

Fedora 和 SuSE 在 64 位元平台上使用 lib64

參閱 sys.platlibdir

在 3.9 版被加入.

--with-wheel-pkg-dir=PATH

Directory of wheel packages used by the ensurepip module (none by default).

Some Linux distribution packaging policies recommend against bundling dependencies. For example, Fedora installs wheel packages in the /usr/share/python-wheels/ directory and don't install the ensurepip._bundled package.

在 3.10 版被加入.

--with-pkg-config=[check|yes|no]

Whether configure should use pkg-config to detect build dependencies.

  • check(預設):pkg-config 是可選的

  • yespkg-config 是必要的

  • no:即使存在也不使用 pkg-config 來配置

在 3.11 版被加入.

--enable-pystats

Turn on internal Python performance statistics gathering.

By default, statistics gathering is off. Use python3 -X pystats command or set PYTHONSTATS=1 environment variable to turn on statistics gathering at Python startup.

At Python exit, dump statistics if statistics gathering was on and not cleared.

效果:

  • 新增 -X pystats 命令列選項。

  • 新增 PYTHONSTATS 環境變數。

  • 定義 Py_STATS 巨集。

  • 新增函式到 sys 模組。

    • sys._stats_on():啟用統計資料收集。

    • sys._stats_off():關閉統計資料收集。

    • sys._stats_clear():清除統計資料。

    • sys._stats_dump():將統計資料轉儲到檔案,並清除統計資料。

統計資料將被轉儲到 /tmp/py_stats/ (Unix) 或 C:\temp\py_stats\ (Windows) 中的任意(可能是唯一的)檔案。如果該目錄不存在,結果將被列印到 stderr。

使用 Tools/scripts/summarize_stats.py 來讀取統計資料。

Statistics:

  • Opcode:

    • Specialization: success, failure, hit, deferred, miss, deopt, failures;

    • Execution count;

    • Pair count.

  • Call:

    • Inlined Python calls;

    • PyEval calls;

    • Frames pushed;

    • Frame object created;

    • Eval calls: vector, generator, legacy, function VECTORCALL, build class, slot, function "ex", API, method.

  • 物件:

    • incref and decref;

    • interpreter incref and decref;

    • allocations: all, 512 bytes, 4 kiB, big;

    • free;

    • to/from free lists;

    • dictionary materialized/dematerialized;

    • type cache;

    • 最佳化嘗試;

    • optimization traces created/executed;

    • uops executed.

  • 垃圾回收器:

    • 垃圾回收;

    • Objects visited;

    • Objects collected.

在 3.11 版被加入.

--disable-gil

Enables support for running Python without the global interpreter lock (GIL): free-threaded build.

Defines the Py_GIL_DISABLED macro and adds "t" to sys.abiflags.

細節請參見 Free-threaded CPython

在 3.13 版被加入.

--enable-experimental-jit=[no|yes|yes-off|interpreter]

Indicate how to integrate the experimental just-in-time compiler.

  • no: Don't build the JIT.

  • yes: Enable the JIT. To disable it at runtime, set the environment variable PYTHON_JIT=0.

  • yes-off: Build the JIT, but disable it by default. To enable it at runtime, set the environment variable PYTHON_JIT=1.

  • interpreter: Enable the "JIT interpreter" (only useful for those debugging the JIT itself). To disable it at runtime, set the environment variable PYTHON_JIT=0.

--enable-experimental-jit=no is the default behavior if the option is not provided, and --enable-experimental-jit is shorthand for --enable-experimental-jit=yes. See Tools/jit/README.md for more information, including how to install the necessary build-time dependencies.

備註

When building CPython with JIT enabled, ensure that your system has Python 3.11 or later installed.

在 3.13 版被加入.

PKG_CONFIG

Path to pkg-config utility.

PKG_CONFIG_LIBDIR
PKG_CONFIG_PATH

pkg-config 選項。

3.3.2. C 編譯器選項。

CC

C 編譯器指令。

CFLAGS

C 編譯器旗標。

CPP

C 預處理器指令。

CPPFLAGS

C 預處理器旗標,例如:-Iinclude_dir

3.3.3. Linker options

LDFLAGS

Linker flags, e.g. -Llibrary_directory.

LIBS

Libraries to pass to the linker, e.g. -llibrary.

MACHDEP

Name for machine-dependent library files.

3.3.4. Options for third-party dependencies

在 3.11 版被加入.

BZIP2_CFLAGS
BZIP2_LIBS

C compiler and linker flags to link Python to libbz2, used by bz2 module, overriding pkg-config.

CURSES_CFLAGS
CURSES_LIBS

C compiler and linker flags for libncurses or libncursesw, used by curses module, overriding pkg-config.

GDBM_CFLAGS
GDBM_LIBS

C compiler and linker flags for gdbm.

LIBEDIT_CFLAGS
LIBEDIT_LIBS

C compiler and linker flags for libedit, used by readline module, overriding pkg-config.

LIBFFI_CFLAGS
LIBFFI_LIBS

C compiler and linker flags for libffi, used by ctypes module, overriding pkg-config.

LIBMPDEC_CFLAGS
LIBMPDEC_LIBS

C compiler and linker flags for libmpdec, used by decimal module, overriding pkg-config.

備註

These environment variables have no effect unless --with-system-libmpdec is specified.

LIBLZMA_CFLAGS
LIBLZMA_LIBS

C compiler and linker flags for liblzma, used by lzma module, overriding pkg-config.

LIBREADLINE_CFLAGS
LIBREADLINE_LIBS

C compiler and linker flags for libreadline, used by readline module, overriding pkg-config.

LIBSQLITE3_CFLAGS
LIBSQLITE3_LIBS

C compiler and linker flags for libsqlite3, used by sqlite3 module, overriding pkg-config.

LIBUUID_CFLAGS
LIBUUID_LIBS

C compiler and linker flags for libuuid, used by uuid module, overriding pkg-config.

LIBZSTD_CFLAGS
LIBZSTD_LIBS

C compiler and linker flags for libzstd, used by compression.zstd module, overriding pkg-config.

在 3.14 版被加入.

PANEL_CFLAGS
PANEL_LIBS

C compiler and linker flags for PANEL, overriding pkg-config.

C compiler and linker flags for libpanel or libpanelw, used by curses.panel module, overriding pkg-config.

TCLTK_CFLAGS
TCLTK_LIBS

C compiler and linker flags for TCLTK, overriding pkg-config.

ZLIB_CFLAGS
ZLIB_LIBS

C compiler and linker flags for libzlib, used by gzip module, overriding pkg-config.

3.3.5. WebAssembly 選項

--enable-wasm-dynamic-linking

Turn on dynamic linking support for WASM.

Dynamic linking enables dlopen. File size of the executable increases due to limited dead code elimination and additional features.

在 3.11 版被加入.

--enable-wasm-pthreads

Turn on pthreads support for WASM.

在 3.11 版被加入.

3.3.6. 安裝選項

--prefix=PREFIX

Install architecture-independent files in PREFIX. On Unix, it defaults to /usr/local.

這個值可以在 runtime 使用 sys.prefix 取得。

As an example, one can use --prefix="$HOME/.local/" to install a Python in its home directory.

--exec-prefix=EPREFIX

Install architecture-dependent files in EPREFIX, defaults to --prefix.

這個值可以在 runtime 使用 sys.exec_prefix 取得

--disable-test-modules

Don't build nor install test modules, like the test package or the _testcapi extension module (built and installed by default).

在 3.10 版被加入.

--with-ensurepip=[upgrade|install|no]

選擇在 Python 安裝時執行的 ensurepip 命令:

  • upgrade (預設):執行 python -m ensurepip --altinstall --upgrade 命令。

  • install:執行 python -m ensurepip --altinstall 命令;

  • no:不要執行 ensurepip;

在 3.6 版被加入.

3.3.7. 效能選項

Configuring Python using --enable-optimizations --with-lto (PGO + LTO) is recommended for best performance. The experimental --enable-bolt flag can also be used to improve performance.

--enable-optimizations

Enable Profile Guided Optimization (PGO) using PROFILE_TASK (disabled by default).

The C compiler Clang requires llvm-profdata program for PGO. On macOS, GCC also requires it: GCC is just an alias to Clang on macOS.

Disable also semantic interposition in libpython if --enable-shared and GCC is used: add -fno-semantic-interposition to the compiler and linker flags.

備註

During the build, you may encounter compiler warnings about profile data not being available for some source files. These warnings are harmless, as only a subset of the code is exercised during profile data acquisition. To disable these warnings on Clang, manually suppress them by adding -Wno-profile-instr-unprofiled to CFLAGS.

在 3.6 版被加入.