直譯器的初始化與終結化¶
關於如何在初始化之前設定直譯器的細節,請參見 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:
Functions that initialize the interpreter:
the runtime pre-initialization functions covered in Python 初始化設定
Configuration functions:
PyInitFrozenExtensions()the configuration functions covered in Python 初始化設定
Informative functions:
Utilities:
the status reporting and utility functions covered in Python 初始化設定
Memory allocators:
Synchronization:
備註
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_warningshould be used instead, see Python Initialization Configuration.Issue a warning when comparing
bytesorbytearraywithstrorbyteswithint. Issue an error if greater or equal to2.由
-b選項設定。自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.
-
int Py_DebugFlag¶
This API is kept for backward compatibility: setting
PyConfig.parser_debugshould 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_bytecodeshould be used instead, see Python Initialization Configuration.If set to non-zero, Python won't try to write
.pycfiles 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_warningsshould be used instead, see Python Initialization Configuration.Private flag used by
_freeze_moduleandfrozenmainprograms.自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.
-
int Py_HashRandomizationFlag¶
This API is kept for backward compatibility: setting
PyConfig.hash_seedandPyConfig.use_hash_seedshould be used instead, see Python Initialization Configuration.如果環境變數
PYTHONHASHSEED被設定為一個非空字串則設為1。If the flag is non-zero, read the
PYTHONHASHSEEDenvironment variable to initialize the secret hash seed.自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.
-
int Py_IgnoreEnvironmentFlag¶
This API is kept for backward compatibility: setting
PyConfig.use_environmentshould be used instead, see Python Initialization Configuration.忽略所有可能被設定的
PYTHON*環境變數,例如PYTHONPATH與PYTHONHOME。自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.
-
int Py_InspectFlag¶
This API is kept for backward compatibility: setting
PyConfig.inspectshould be used instead, see Python Initialization Configuration.When a script is passed as first argument or the
-coption is used, enter interactive mode after executing the script or the command, even whensys.stdindoes not appear to be a terminal.由
-i選項與PYTHONINSPECT環境變數設定。自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.
-
int Py_InteractiveFlag¶
This API is kept for backward compatibility: setting
PyConfig.interactiveshould be used instead, see Python Initialization Configuration.由
-i選項設定。自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.
-
int Py_IsolatedFlag¶
This API is kept for backward compatibility: setting
PyConfig.isolatedshould be used instead, see Python Initialization Configuration.Run Python in isolated mode. In isolated mode
sys.pathcontains 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_encodingshould be used instead, see Python Initialization Configuration.If the flag is non-zero, use the
mbcsencoding withreplaceerror handler, instead of the UTF-8 encoding withsurrogatepasserror 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_stdioshould be used instead, see Python Initialization Configuration.If the flag is non-zero, use
io.FileIOinstead ofio._WindowsConsoleIOforsysstandard streams.Set to
1if thePYTHONLEGACYWINDOWSSTDIOenvironment 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_importshould be used instead, see Python Initialization Configuration.Disable the import of the module
siteand the site-dependent manipulations ofsys.paththat it entails. Also disable these manipulations ifsiteis explicitly imported later (callsite.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_directoryshould be used instead, see Python Initialization Configuration.Don't add the
user site-packages directorytosys.path.由
-s選項、-I選項與PYTHONNOUSERSITE環境變數設定。自從版本 3.12 後不推薦使用,將會自版本 3.15 中移除。.
-
int Py_OptimizeFlag¶
This API is kept for backward compatibility: setting
PyConfig.optimization_levelshould 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.quietshould 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_stdioshould 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.verboseshould 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 modulesbuiltins,__main__andsys. It also initializes the module search path (sys.path). It does not setsys.argv; use the Python Initialization Configuration API for that. This is a no-op when called for a second time (without callingPy_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_TEXTtoO_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 is1. If initsigs is0, 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 untilPy_Initialize()is called again.