Initialization, Finalization, and Threads¶
Before Python Initialization¶
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:
- Configuration functions:
- Informative functions:
- Utilities:
- Memory allocators:
備註
The following functions should not be called before
Py_Initialize(): Py_EncodeLocale(), Py_GetPath(),
Py_GetPrefix(), Py_GetExecPrefix(),
Py_GetProgramFullPath(), Py_GetPythonHome(),
Py_GetProgramName() and PyEval_InitThreads().
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.
-
Py_BytesWarningFlag¶ Issue a warning when comparing
bytesorbytearraywithstrorbyteswithint. Issue an error if greater or equal to2.Set by the
-boption.
-
Py_DebugFlag¶ Turn on parser debugging output (for expert only, depending on compilation options).
Set by the
-doption and thePYTHONDEBUGenvironment variable.
-
Py_DontWriteBytecodeFlag¶ If set to non-zero, Python won’t try to write
.pycfiles on the import of source modules.Set by the
-Boption and thePYTHONDONTWRITEBYTECODEenvironment variable.
-
Py_FrozenFlag¶ Suppress error messages when calculating the module search path in
Py_GetPath().Private flag used by
_freeze_importlibandfrozenmainprograms.
-
Py_HashRandomizationFlag¶ Set to
1if thePYTHONHASHSEEDenvironment variable is set to a non-empty string.If the flag is non-zero, read the
PYTHONHASHSEEDenvironment variable to initialize the secret hash seed.
-
Py_IgnoreEnvironmentFlag¶ Ignore all
PYTHON*environment variables, e.g.PYTHONPATHandPYTHONHOME, that might be set.
-
Py_InspectFlag¶ 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.Set by the
-ioption and thePYTHONINSPECTenvironment variable.
-
Py_IsolatedFlag¶ Run Python in isolated mode. In isolated mode
sys.pathcontains neither the script’s directory nor the user’s site-packages directory.Set by the
-Ioption.3.4 版新加入.
-
Py_LegacyWindowsFSEncodingFlag¶ If the flag is non-zero, use the
mbcsencoding instead of the UTF-8 encoding for the filesystem encoding.Set to
1if thePYTHONLEGACYWINDOWSFSENCODINGenvironment variable is set to a non-empty string.See PEP 529 for more details.
Availability: Windows.
-
Py_LegacyWindowsStdioFlag¶ If the flag is non-zero, use
io.FileIOinstead ofWindowsConsoleIOforsysstandard streams.Set to
1if thePYTHONLEGACYWINDOWSSTDIOenvironment variable is set to a non-empty string.See PEP 528 for more details.
Availability: Windows.
