直譯器的初始化與終結化

關於如何在初始化之前設定直譯器的細節,請參見 Python 初始化設定

Python 初始化之前

In an application embedding Python, the Py_Initialize() function must be called before using any other Python/C API functions; with the exception of a few functions and the global configuration variables.

The following functions can be safely called before Python is initialized:

備註

Despite their apparent similarity to some of the functions listed above, the following functions should not be called before the interpreter has been initialized: Py_EncodeLocale(), PyEval_InitThreads(), and Py_RunMain().

Global configuration variables

Python has variables for the global configuration to control different features and options. By default, these flags are controlled by command line options.

When a flag is set by an option, the value of the flag is the number of times that the option was set. For example, -b sets Py_BytesWarningFlag to 1 and -bb sets Py_BytesWarningFlag to 2.

int Py_BytesWarningFlag

This API is kept for backward compatibility: setting PyConfig.bytes_warning should be used instead, see Python Initialization Configuration.

Issue a warning when comparing bytes or bytearray with str or bytes with int. Issue an error if greater or equal to 2.

-b 選項設定。

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_DebugFlag

This API is kept for backward compatibility: setting PyConfig.parser_debug should be used instead, see Python Initialization Configuration.

Turn on parser debugging output (for expert only, depending on compilation options).

-d 選項與 PYTHONDEBUG 環境變數設定。

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_DontWriteBytecodeFlag

This API is kept for backward compatibility: setting PyConfig.write_bytecode should be used instead, see Python Initialization Configuration.

If set to non-zero, Python won't try to write .pyc files on the import of source modules.

-B 選項與 PYTHONDONTWRITEBYTECODE 環境變數設定。

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_FrozenFlag

This API is kept for backward compatibility: setting PyConfig.pathconfig_warnings should be used instead, see Python Initialization Configuration.

Private flag used by _freeze_module and frozenmain programs.

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_HashRandomizationFlag

This API is kept for backward compatibility: setting PyConfig.hash_seed and PyConfig.use_hash_seed should be used instead, see Python Initialization Configuration.

如果環境變數 PYTHONHASHSEED 被設定為一個非空字串則設為 1

If the flag is non-zero, read the PYTHONHASHSEED environment variable to initialize the secret hash seed.

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_IgnoreEnvironmentFlag

This API is kept for backward compatibility: setting PyConfig.use_environment should be used instead, see Python Initialization Configuration.

忽略所有可能被設定的 PYTHON* 環境變數,例如 PYTHONPATHPYTHONHOME

-E-I 選項設定。

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_InspectFlag

This API is kept for backward compatibility: setting PyConfig.inspect should be used instead, see Python Initialization Configuration.

When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command, even when sys.stdin does not appear to be a terminal.

-i 選項與 PYTHONINSPECT 環境變數設定。

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_InteractiveFlag

This API is kept for backward compatibility: setting PyConfig.interactive should be used instead, see Python Initialization Configuration.

-i 選項設定。

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_IsolatedFlag

This API is kept for backward compatibility: setting PyConfig.isolated should be used instead, see Python Initialization Configuration.

Run Python in isolated mode. In isolated mode sys.path contains neither the script's directory nor the user's site-packages directory.

-I 選項設定。

在 3.4 版被加入.

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_LegacyWindowsFSEncodingFlag

This API is kept for backward compatibility: setting PyPreConfig.legacy_windows_fs_encoding should be used instead, see Python Initialization Configuration.

If the flag is non-zero, use the mbcs encoding with replace error handler, instead of the UTF-8 encoding with surrogatepass error handler, for the filesystem encoding and error handler.

如果環境變數 PYTHONLEGACYWINDOWSFSENCODING 被設定為一個非空字串則設為 1

更多詳情請見 PEP 529

可用性: Windows.

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_LegacyWindowsStdioFlag

This API is kept for backward compatibility: setting PyConfig.legacy_windows_stdio should be used instead, see Python Initialization Configuration.

If the flag is non-zero, use io.FileIO instead of io._WindowsConsoleIO for sys standard streams.

Set to 1 if the PYTHONLEGACYWINDOWSSTDIO environment variable is set to a non-empty string.

更多詳情請見 PEP 528

可用性: Windows.

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_NoSiteFlag

This API is kept for backward compatibility: setting PyConfig.site_import should be used instead, see Python Initialization Configuration.

Disable the import of the module site and the site-dependent manipulations of sys.path that it entails. Also disable these manipulations if site is explicitly imported later (call site.main() if you want them to be triggered).

-S 選項設定。

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_NoUserSiteDirectory

This API is kept for backward compatibility: setting PyConfig.user_site_directory should be used instead, see Python Initialization Configuration.

Don't add the user site-packages directory to sys.path.

-s 選項、-I 選項與 PYTHONNOUSERSITE 環境變數設定。

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_OptimizeFlag

This API is kept for backward compatibility: setting PyConfig.optimization_level should be used instead, see Python Initialization Configuration.

-O 選項與 PYTHONOPTIMIZE 環境變數設定。

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_QuietFlag

This API is kept for backward compatibility: setting PyConfig.quiet should be used instead, see Python Initialization Configuration.

Don't display the copyright and version messages even in interactive mode.

-q 選項設定。

在 3.2 版被加入.

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_UnbufferedStdioFlag

This API is kept for backward compatibility: setting PyConfig.buffered_stdio should be used instead, see Python Initialization Configuration.

Force the stdout and stderr streams to be unbuffered.

-u 選項與 PYTHONUNBUFFERED 環境變數設定。

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

int Py_VerboseFlag

This API is kept for backward compatibility: setting PyConfig.verbose should be used instead, see Python Initialization Configuration.

Print a message each time a module is initialized, showing the place (filename or built-in module) from which it is loaded. If greater or equal to 2, print a message for each file that is checked for when searching for a module. Also provides information on module cleanup at exit.

-v 選項與 PYTHONVERBOSE 環境變數設定。

自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.

Initializing and finalizing the interpreter

void Py_Initialize()
穩定 ABI 的一部分.

Initialize the Python interpreter. In an application embedding Python, this should be called before using any other Python/C API functions; see Before Python Initialization for the few exceptions.

This initializes the table of loaded modules (sys.modules), and creates the fundamental modules builtins, __main__ and sys. It also initializes the module search path (sys.path). It does not set sys.argv; use the Python Initialization Configuration API for that. This is a no-op when called for a second time (without calling Py_FinalizeEx() first). There is no return value; it is a fatal error if the initialization fails.

Use Py_InitializeFromConfig() to customize the Python Initialization Configuration.

備註

On Windows, changes the console mode from O_TEXT to O_BINARY, which will also affect non-Python uses of the console using the C Runtime.

void Py_InitializeEx(int initsigs)
穩定 ABI 的一部分.

This function works like Py_Initialize() if initsigs is 1. If initsigs is 0, it skips initialization registration of signal handlers, which may be useful when CPython is embedded as part of a larger application.

Use Py_InitializeFromConfig() to customize the Python Initialization Configuration.

PyStatus Py_InitializeFromConfig(const PyConfig *config)

Initialize Python from config configuration, as described in Initialization with PyConfig.

See the Python 初始化設定 section for details on pre-initializing the interpreter, populating the runtime configuration structure, and querying the returned status structure.

int Py_IsInitialized()
穩定 ABI 的一部分.

Return true (nonzero) when the Python interpreter has been initialized, false (zero) if not. After Py_FinalizeEx() is called, this returns false until Py_Initialize() is called again.