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:

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 bytes or bytearray with str or bytes with int. Issue an error if greater or equal to 2.

Set by the -b option.

Py_DebugFlag

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

Set by the -d option and the PYTHONDEBUG environment variable.

Py_DontWriteBytecodeFlag

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

Set by the -B option and the PYTHONDONTWRITEBYTECODE environment variable.

Py_FrozenFlag

Suppress error messages when calculating the module search path in Py_GetPath().

Private flag used by _freeze_importlib and frozenmain programs.

Py_HashRandomizationFlag

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

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

Py_IgnoreEnvironmentFlag

Ignore all PYTHON* environment variables, e.g. PYTHONPATH and PYTHONHOME, that might be set.

Set by the -E and -I options.

Py_InspectFlag

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.

Set by the -i option and the PYTHONINSPECT environment variable.

Py_InteractiveFlag

Set by the -i option.

Py_IsolatedFlag

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

Set by the -I option.

3.4 版新加入.

Py_LegacyWindowsFSEncodingFlag

If the flag is non-zero, use the mbcs encoding instead of the UTF-8 encoding for the filesystem encoding.

Set to 1 if the PYTHONLEGACYWINDOWSFSENCODING environment 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.FileIO instead of WindowsConsoleIO for sys standard streams.

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

See PEP 528 for more details.

Availability: Windows.

Py_NoSiteFlag

Disable the import of the module site and the site-dependent manipulations of sys.path that it entails. Also disable these manipulations if