1.. highlight:: c 2 3.. _init-config: 4 5*********************************** 6Python Initialization Configuration 7*********************************** 8 9.. versionadded:: 3.8 10 11Python can be initialized with :c:func:`Py_InitializeFromConfig` and the 12:c:type:`PyConfig` structure. It can be preinitialized with 13:c:func:`Py_PreInitialize` and the :c:type:`PyPreConfig` structure. 14 15There are two kinds of configuration: 16 17* The :ref:`Python Configuration <init-python-config>` can be used to build a 18 customized Python which behaves as the regular Python. For example, 19 environments variables and command line arguments are used to configure 20 Python. 21 22* The :ref:`Isolated Configuration <init-isolated-conf>` can be used to embed 23 Python into an application. It isolates Python from the system. For example, 24 environments variables are ignored, the LC_CTYPE locale is left unchanged and 25 no signal handler is registered. 26 27The :c:func:`Py_RunMain` function can be used to write a customized Python 28program. 29 30See also :ref:`Initialization, Finalization, and Threads <initialization>`. 31 32.. seealso:: 33 :pep:`587` "Python Initialization Configuration". 34 35 36Example 37======= 38 39Example of customized Python always running in isolated mode:: 40 41 int main(int argc, char **argv) 42 { 43 PyStatus status; 44 45 PyConfig config; 46 PyConfig_InitPythonConfig(&config); 47 config.isolated = 1; 48 49 /* Decode command line arguments. 50 Implicitly preinitialize Python (in isolated mode). */ 51 status = PyConfig_SetBytesArgv(&config, argc, argv); 52 if (PyStatus_Exception(status)) { 53 goto exception; 54 } 55 56 status = Py_InitializeFromConfig(&config); 57 if (PyStatus_Exception(status)) { 58 goto exception; 59 } 60 PyConfig_Clear(&config); 61 62 return Py_RunMain(); 63 64 exception: 65 PyConfig_Clear(&config); 66 if (PyStatus_IsExit(status)) { 67 return status.exitcode; 68 } 69 /* Display the error message and exit the process with 70 non-zero exit code */ 71 Py_ExitStatusException(status); 72 } 73 74 75PyWideStringList 76================ 77 78.. c:type:: PyWideStringList 79 80 List of ``wchar_t*`` strings. 81 82 If *length* is non-zero, *items* must be non-``NULL`` and all strings must be 83 non-``NULL``. 84 85 Methods: 86 87 .. c:function:: PyStatus PyWideStringList_Append(PyWideStringList *list, const wchar_t *item) 88 89 Append *item* to *list*. 90 91 Python must be preinitialized to call this function. 92 93 .. c:function:: PyStatus PyWideStringList_Insert(PyWideStringList *list, Py_ssize_t index, const wchar_t *item) 94 95 Insert *item* into *list* at *index*. 96 97 If *index* is greater than or equal to *list* length, append *item* to 98 *list*. 99 100 *index* must be greater than or equal to 0. 101 102 Python must be preinitialized to call this function. 103 104 Structure fields: 105 106 .. c:member:: Py_ssize_t length 107 108 List length. 109 110 .. c:member:: wchar_t** items 111 112 List items. 113 114PyStatus 115======== 116 117.. c:type:: PyStatus 118 119 Structure to store an initialization function status: success, error 120 or exit. 121 122 For an error, it can store the C function name which created the error. 123 124 Structure fields: 125 126 .. c:member:: int exitcode 127 128 Exit code. Argument passed to ``exit()``. 129 130 .. c:member:: const char *err_msg 131 132 Error message. 133 134 .. c:member:: const char *func 135 136 Name of the function which created an error, can be ``NULL``. 137 138 Functions to create a status: 139 140 .. c:function:: PyStatus PyStatus_Ok(void) 141 142 Success. 143 144 .. c:function:: PyStatus PyStatus_Error(const char *err_msg) 145 146 Initialization error with a message. 147 148 *err_msg* must not be ``NULL``. 149 150 .. c:function:: PyStatus PyStatus_NoMemory(void) 151 152 Memory allocation failure (out of memory). 153 154 .. c:function:: PyStatus PyStatus_Exit(int exitcode) 155 156 Exit Python with the specified exit code. 157 158 Functions to handle a status: 159 160 .. c:function:: int PyStatus_Exception(PyStatus status) 161 162 Is the status an error or an exit? If true, the exception must be 163 handled; by calling :c:func:`Py_ExitStatusException` for example. 164 165 .. c:function:: int PyStatus_IsError(PyStatus status) 166 167 Is the result an error? 168 169 .. c:function:: int PyStatus_IsExit(PyStatus status) 170 171 Is the result an exit? 172 173 .. c:function:: void Py_ExitStatusException(PyStatus status) 174 175 Call ``exit(exitcode)`` if *status* is an exit. Print the error 176 message and exit with a non-zero exit code if *status* is an error. Must 177 only be called if ``PyStatus_Exception(status)`` is non-zero. 178 179.. note:: 180 Internally, Python uses macros which set ``PyStatus.func``, 181 whereas functions to create a status set ``func`` to ``NULL``. 182 183Example:: 184 185 PyStatus alloc(void **ptr, size_t size) 186 { 187 *ptr = PyMem_RawMalloc(size); 188 if (*ptr == NULL) { 189 return PyStatus_NoMemory(); 190 } 191 return PyStatus_Ok(); 192 } 193 194 int main(int argc, char **argv) 195 { 196 void *ptr; 197 PyStatus status = alloc(&ptr, 16); 198 if (PyStatus_Exception(status)) { 199 Py_ExitStatusException(status); 200 } 201 PyMem_Free(ptr); 202 return 0; 203 } 204 205 206PyPreConfig 207=========== 208 209.. c:type:: PyPreConfig 210 211 Structure used to preinitialize Python. 212 213 Function to initialize a preconfiguration: 214 215 .. c:function:: void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig) 216 217 Initialize the preconfiguration with :ref:`Python Configuration 218 <init-python-config>`. 219 220 .. c:function:: void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig) 221 222 Initialize the preconfiguration with :ref:`Isolated Configuration 223 <init-isolated-conf>`. 224 225 Structure fields: 226 227 .. c:member:: int allocator 228 229 Name of the Python memory allocators: 230 231 * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators 232 (use defaults). 233 * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): :ref:`default memory allocators 234 <default-memory-allocators>`. 235 * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): :ref:`default memory allocators 236 <default-memory-allocators>` with :ref:`debug hooks 237 <pymem-debug-hooks>`. 238 * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): use ``malloc()`` of the C library. 239 * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of 240 ``malloc()`` with :ref:`debug hooks <pymem-debug-hooks>`. 241 * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory 242 allocator <pymalloc>`. 243 * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc 244 memory allocator <pymalloc>` with :ref:`debug hooks 245 <pymem-debug-hooks>`. 246 247 ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` are 248 not supported if Python is :option:`configured using --without-pymalloc 249 <--without-pymalloc>`. 250 251 See :ref:`Memory Management <memory>`. 252 253 Default: ``PYMEM_ALLOCATOR_NOT_SET``. 254 255 .. c:member:: int configure_locale 256 257 Set the LC_CTYPE locale to the user preferred locale? 258 259 If equals to 0, set :c:member:`~PyPreConfig.coerce_c_locale` and 260 :c:member:`~PyPreConfig.coerce_c_locale_warn` members to 0. 261 262 See the :term:`locale encoding`. 263 264 Default: ``1`` in Python config, ``0`` in isolated config. 265 266 .. c:member:: int coerce_c_locale 267 268 If equals to 2, coerce the C locale. 269 270 If equals to 1, read the LC_CTYPE locale to decide if it should be 271 coerced. 272 273 See the :term:`locale encoding`. 274 275 Default: ``-1`` in Python config, ``0`` in isolated config. 276 277 .. c:member:: int coerce_c_locale_warn 278 279 If non-zero, emit a warning if the C locale is coerced. 280 281 Default: ``-1`` in Python config, ``0`` in isolated config. 282 283 .. c:member:: int dev_mode 284 285 If non-zero, enables the :ref:`Python Development Mode <devmode>`: 286 see :c:member:`PyConfig.dev_mode`. 287 288 Default: ``-1`` in Python mode, ``0`` in isolated mode. 289 290 .. c:member:: int isolated 291 292 Isolated mode: see :c:member:`PyConfig.isolated`. 293 294 Default: ``0`` in Python mode, ``1`` in isolated mode. 295 296 .. c:member:: int legacy_windows_fs_encoding 297 298 If non-zero: 299 300 * Set :c:member:`PyPreConfig.utf8_mode` to ``0``, 301 * Set :c:member:`PyConfig.filesystem_encoding` to ``"mbcs"``, 302 * Set :c:member:`PyConfig.filesystem_errors` to ``"replace"``. 303 304 Initialized the from :envvar:`PYTHONLEGACYWINDOWSFSENCODING` environment 305 variable value. 306 307 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for 308 Windows specific code. 309 310 Default: ``0``. 311 312 .. c:member:: int parse_argv 313 314 If non-zero, :c:func:`Py_PreInitializeFromArgs` and 315 :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the 316 same way the regular Python parses command line arguments: see 317 :ref:`Command Line Arguments <using-on-cmdline>`. 318 319 Default: ``1`` in Python config, ``0`` in isolated config. 320 321 .. c:member:: int use_environment 322 323 Use :ref:`environment variables <using-on-envvars>`? See 324 :c:member:`PyConfig.use_environment`. 325 326 Default: ``1`` in Python config and ``0`` in isolated config. 327 328 .. c:member:: int utf8_mode 329 330 If non-zero, enable the :ref:`Python UTF-8 Mode <utf8-mode>`. 331 332 Set by the :option:`-X utf8 <-X>` command line option and the 333 :envvar:`PYTHONUTF8` environment variable. 334 335 Default: ``-1`` in Python config and ``0`` in isolated config. 336 337 338.. _c-preinit: 339 340Preinitialize Python with PyPreConfig 341===================================== 342 343The preinitialization of Python: 344 345* Set the Python memory allocators (:c:member:`PyPreConfig.allocator`) 346* Configure the LC_CTYPE locale (:term:`locale encoding`) 347* Set the :ref:`Python UTF-8 Mode <utf8-mode>` 348 (:c:member:`PyPreConfig.utf8_mode`) 349 350The current preconfiguration (``PyPreConfig`` type) is stored in 351``_PyRuntime.preconfig``. 352 353Functions to preinitialize Python: 354 355.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig) 356 357 Preinitialize Python from *preconfig* preconfiguration. 358 359 *preconfig* must not be ``NULL``. 360 361.. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv) 362 363 Preinitialize Python from *preconfig* preconfiguration. 364 365 Parse *argv* command line arguments (bytes strings) if 366 :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero. 367 368 *preconfig* must not be ``NULL``. 369 370.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv) 371 372 Preinitialize Python from *preconfig* preconfiguration. 373 374 Parse *argv* command line arguments (wide strings) if 375 :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero. 376 377 *preconfig* must not be ``NULL``. 378 379The caller is responsible to handle exceptions (error or exit) using 380:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`. 381 382For :ref:`Python Configuration <init-python-config>` 383(:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with 384command line arguments, the command line arguments must also be passed to 385preinitialize Python, since they have an effect on the pre-configuration 386like encodings. For example, the :option:`-X utf8 <-X>` command line option 387enables the :ref:`Python UTF-8 Mode <utf8-mode>`. 388 389``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and 390before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator. 391It can be called before :c:func:`Py_PreInitialize` if 392:c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``. 393 394Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be 395used before the Python preinitialization, whereas calling directly ``malloc()`` 396and ``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called 397before the Python preinitialization. 398 399Example using the preinitialization to enable 400the :ref:`Python UTF-8 Mode <utf8-mode>`:: 401 402 PyStatus status; 403 PyPreConfig preconfig; 404 PyPreConfig_InitPythonConfig(&preconfig); 405 406 preconfig.utf8_mode = 1; 407 408 status = Py_PreInitialize(&preconfig); 409 if (PyStatus_Exception(status)) { 410 Py_ExitStatusException(status); 411 } 412 413 /* at this point, Python speaks UTF-8 */ 414 415 Py_Initialize(); 416 /* ... use Python API here ... */ 417 Py_Finalize(); 418 419 420PyConfig 421======== 422 423.. c:type:: PyConfig 424 425 Structure containing most parameters to configure Python. 426 427 When done, the :c:func:`PyConfig_Clear` function must be used to release the 428 configuration memory. 429 430 Structure methods: 431 432 .. c:function:: void PyConfig_InitPythonConfig(PyConfig *config) 433 434 Initialize configuration with the :ref:`Python Configuration 435 <init-python-config>`. 436 437 .. c:function:: void PyConfig_InitIsolatedConfig(PyConfig *config) 438 439 Initialize configuration with the :ref:`Isolated Configuration 440 <init-isolated-conf>`. 441 442 .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str) 443 444 Copy the wide character string *str* into ``*config_str``. 445 446 :ref:`Preinitialize Python <c-preinit>` if needed. 447 448 .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str) 449 450 Decode *str* using :c:func:`Py_DecodeLocale` and set the result into 451 ``*config_str``. 452 453 :ref:`Preinitialize Python <c-preinit>` if needed. 454 455 .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv) 456 457 Set command line arguments (:c:member:`~PyConfig.argv` member of 458 *config*) from the *argv* list of wide character strings. 459 460 :ref:`Preinitialize Python <c-preinit>` if needed. 461 462 .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv) 463 464 Set command line arguments (:c:member:`~PyConfig.argv` member of 465 *config*) from the *argv* list of bytes strings. Decode bytes using 466 :c:func:`Py_DecodeLocale`. 467 468 :ref:`Preinitialize Python <c-preinit>` if needed. 469 470 .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items) 471 472 Set the list of wide strings *list* to *length* and *items*. 473 474 :ref:`Preinitialize Python <c-preinit>` if needed. 475 476 .. c:function:: PyStatus PyConfig_Read(PyConfig *config) 477 478 Read all Python configuration. 479 480 Fields which are already initialized are left unchanged. 481 482 The :c:func:`PyConfig_Read` function only parses 483 :c:member:`PyConfig.argv` arguments once: :c:member:`PyConfig.parse_argv` 484 is set to ``2`` after arguments are parsed. Since Python arguments are 485 strippped from :c:member:`PyConfig.argv`, parsing arguments twice would 486 parse the application options as Python options. 487 488 :ref:`Preinitialize Python <c-preinit>` if needed. 489 490 .. versionchanged:: 3.10 491 The :c:member:`PyConfig.argv` arguments are now only parsed once, 492 :c:member:`PyConfig.parse_argv` is set to ``2`` after arguments are 493 parsed, and arguments are only parsed if 494 :c:member:`PyConfig.parse_argv` equals ``1``. 495 496 .. c:function:: void PyConfig_Clear(PyConfig *config) 497 498 Release configuration memory. 499 500 Most ``PyConfig`` methods :ref:`preinitialize Python <c-preinit>` if needed. 501 In that case, the Python preinitialization configuration 502 (:c:type:`PyPreConfig`) in based on the :c:type:`PyConfig`. If configuration 503 fields which are in common with :c:type:`PyPreConfig` are tuned, they must 504 be set before calling a :c:type:`PyConfig` method: 505 506 * :c:member:`PyConfig.dev_mode` 507 * :c:member:`PyConfig.isolated` 508 * :c:member:`PyConfig.parse_argv` 509 * :c:member:`PyConfig.use_environment` 510 511 Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv` 512 is used, this method must be called before other methods, since the 513 preinitialization configuration depends on command line arguments (if 514 :c:member:`parse_argv` is non-zero). 515 516 The caller of these methods is responsible to handle exceptions (error or 517 exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``. 518 519 Structure fields: 520 521 .. c:member:: PyWideStringList argv 522 523 Command line arguments: :data:`sys.argv`. 524 525 Set :c:member:`~PyConfig.parse_argv` to ``1`` to parse 526 :c:member:`~PyConfig.argv` the same way the regular Python parses Python 527 command line arguments and then to strip Python arguments from 528 :c:member:`~PyConfig.argv`. 529 530 If :c:member:`~PyConfig.argv` is empty, an empty string is added to 531 ensure that :data:`sys.argv` always exists and is never empty. 532 533 Default: ``NULL``. 534 535 See also the :c:member:`~PyConfig.orig_argv` member. 536 537 .. c:member:: wchar_t* base_exec_prefix 538 539 :data:`sys.base_exec_prefix`. 540 541 Default: ``NULL``. 542 543 Part of the :ref:`Python Path Configuration <init-path-config>` output. 544 545 .. c:member:: wchar_t* base_executable 546 547 Python base executable: :data:`sys._base_executable`. 548 549 Set by the :envvar:`__PYVENV_LAUNCHER__` environment variable. 550 551 Set from :c:member:`PyConfig.executable` if ``NULL``. 552 553 Default: ``NULL``. 554 555 Part of the :ref:`Python Path Configuration <init-path-config>` output. 556 557 .. c:member:: wchar_t* base_prefix 558 559 :data:`sys.base_prefix`. 560 561 Default: ``NULL``. 562 563 Part of the :ref:`Python Path Configuration <init-path-config>` output. 564 565 .. c:member:: int buffered_stdio 566 567 If equals to 0 and :c:member:`~PyConfig.configure_c_stdio` is non-zero, 568 disable buffering on the C streams stdout and stderr. 569 570 Set to 0 by the :option:`-u` command line option and the 571 :envvar:`PYTHONUNBUFFERED` environment variable. 572 573 stdin is always opened in buffered mode. 574 575 Default: ``1``. 576 577 .. c:member:: int bytes_warning 578 579 If equals to 1, issue a warning when comparing :class:`bytes` or 580 :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with 581 :class:`int`. 582 583 If equal or greater to 2, raise a :exc:`BytesWarning` exception in these 584 cases. 585 586 Incremented by the :option:`-b` command line option. 587 588 Default: ``0``. 589 590 .. c:member:: int warn_default_encoding 591 592 If non-zero, emit a :exc:`EncodingWarning` warning when :class:`io.TextIOWrapper` 593 uses its default encoding. See :ref:`io-encoding-warning` for details. 594 595 Default: ``0``. 596 597 .. versionadded:: 3.10 598 599 .. c:member:: wchar_t* check_hash_pycs_mode 600 601 Control the validation behavior of hash-based ``.pyc`` files: 602 value of the :option:`--check-hash-based-pycs` command line option. 603 604 Valid values: 605 606 - ``L"always"``: Hash the source file for invalidation regardless of 607 value of the 'check_source' flag. 608 - ``L"never"``: Assume that hash-based pycs always are valid. 609 - ``L"default"``: The 'check_source' flag in hash-based pycs 610 determines invalidation. 611 612 Default: ``L"default"``. 613 614 See also :pep:`552` "Deterministic pycs". 615 616 .. c:member:: int configure_c_stdio 617 618 If non-zero, configure C standard streams: 619 620 * On Windows, set the binary mode (``O_BINARY``) on stdin, stdout and 621 stderr. 622 * If :c:member:`~PyConfig.buffered_stdio` equals zero, disable buffering 623 of stdin, stdout and stderr streams. 624 * If :c:member:`~PyConfig.interactive` is non-zero, enable stream 625 buffering on stdin and stdout (only stdout on Windows). 626 627 Default: ``1`` in Python config, ``0`` in isolated config. 628 629 .. c:member:: int dev_mode 630 631 If non-zero, enable the :ref:`Python Development Mode <devmode>`. 632 633 Default: ``-1`` in Python mode, ``0`` in isolated mode. 634 635 .. c:member:: int dump_refs 636 637 Dump Python refererences? 638 639 If non-zero, dump all objects which are still alive at exit. 640 641 Set to ``1`` by the :envvar:`PYTHONDUMPREFS` environment variable. 642 643 Need a special build of Python with the ``Py_TRACE_REFS`` macro defined: 644 see the :option:`configure --with-trace-refs option <--with-trace-refs>`. 645 646 Default: ``0``. 647 648 .. c:member:: wchar_t* exec_prefix 649 650 The site-specific directory prefix where the platform-dependent Python 651 files are installed: :data:`sys.exec_prefix`. 652 653 Default: ``NULL``. 654 655 Part of the :ref:`Python Path Configuration <init-path-config>` output. 656 657 .. c:member:: wchar_t* executable 658 659 The absolute path of the executable binary for the Python interpreter: 660 :data:`sys.executable`. 661 662 Default: ``NULL``. 663 664 Part of the :ref:`Python Path Configuration <init-path-config>` output. 665 666 .. c:member:: int faulthandler 667 668 Enable faulthandler? 669 670 If non-zero, call :func:`faulthandler.enable` at startup. 671 672 Set to ``1`` by :option:`-X faulthandler <-X>` and the 673 :envvar:`PYTHONFAULTHANDLER` environment variable. 674 675 Default: ``-1`` in Python mode, ``0`` in isolated mode. 676 677 .. c:member:: wchar_t* filesystem_encoding 678 679 :term:`Filesystem encoding <filesystem encoding and error handler>`: 680 :func:`sys.getfilesystemencoding`. 681 682 On macOS, Android and VxWorks: use ``"utf-8"`` by default. 683 684 On Windows: use ``"utf-8"`` by default, or ``"mbcs"`` if 685 :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of 686 :c:type:`PyPreConfig` is non-zero. 687 688 Default encoding on other platforms: 689 690 * ``"utf-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero. 691 * ``"ascii"`` if Python detects that ``nl_langinfo(CODESET)`` announces 692 the ASCII encoding (or Roman8 encoding on HP-UX), whereas the 693 ``mbstowcs()`` function decodes from a different encoding (usually 694 Latin1). 695 * ``"utf-8"`` if ``nl_langinfo(CODESET)`` returns an empty string. 696 * Otherwise, use the :term:`locale encoding`: 697 ``nl_langinfo(CODESET)`` result. 698 699 At Python startup, the encoding name is normalized to the Python codec 700 name. For example, ``"ANSI_X3.4-1968"`` is replaced with ``"ascii"``. 701 702 See also the :c:member:`~PyConfig.filesystem_errors` member. 703 704 .. c:member:: wchar_t* filesystem_errors 705 706 :term:`Filesystem error handler <filesystem encoding and error handler>`: 707 :func:`sys.getfilesystemencodeerrors`. 708 709 On Windows: use ``"surrogatepass"`` by default, or ``"replace"`` if 710 :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of 711 :c:type:`PyPreConfig` is non-zero. 712 713 On other platforms: use ``"surrogateescape"`` by default. 714 715 Supported error handlers: 716 717 * ``"strict"`` 718 * ``"surrogateescape"`` 719 * ``"surrogatepass"`` (only supported with the UTF-8 encoding) 720 721 See also the :c:member:`~PyConfig.filesystem_encoding` member. 722 723 .. c:member:: unsigned long hash_seed 724 .. c:member:: int use_hash_seed 725 726 Randomized hash function seed. 727 728 If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly 729 at Python startup, and :c:member:`~PyConfig.hash_seed` is ignored. 730 731 Set by the :envvar:`PYTHONHASHSEED` environment variable. 732 733 Default *use_hash_seed* value: ``-1`` in Python mode, ``0`` in isolated 734 mode. 735 736 .. c:member:: wchar_t* home 737 738 Python home directory. 739 740 If :c:func:`Py_SetPythonHome` has been called, use its argument if it is 741 not ``NULL``. 742 743 Set by the :envvar:`PYTHONHOME` environment variable. 744 745 Default: ``NULL``. 746 747 Part of the :ref:`Python Path Configuration <init-path-config>` input. 748 749 .. c:member:: int import_time 750 751 If non-zero, profile import time. 752 753 Set the ``1`` by the :option:`-X importtime <-X>` option and the 754 :envvar:`PYTHONPROFILEIMPORTTIME` environment variable. 755 756 Default: ``0``. 757 758 .. c:member:: int inspect 759 760 Enter interactive mode after executing a script or a command. 761 762 If greater than 0, enable inspect: when a script is passed as first 763 argument or the -c option is used, enter interactive mode after executing 764 the script or the command, even when :data:`sys.stdin` does not appear to 765 be a terminal. 766 767 Incremented by the :option:`-i` command line option. Set to ``1`` if the 768 :envvar:`PYTHONINSPECT` environment variable is non-empty. 769 770 Default: ``0``. 771 772 .. c:member:: int install_signal_handlers 773 774 Install Python signal handlers? 775 776 Default: ``1`` in Python mode, ``0`` in isolated mode. 777 778 .. c:member:: int interactive 779 780 If greater than 0, enable the interactive mode (REPL). 781 782 Incremented by the :option:`-i` command line option. 783 784 Default: ``0``. 785 786 .. c:member:: int isolated 787 788 If greater than 0, enable isolated mode: 789 790 * :data:`sys.path` contains neither the script's directory (computed from 791 ``argv[0]`` or the current directory) nor the user's site-packages 792 directory. 793 * Python REPL doesn't import :mod:`readline` nor enable default readline 794 configuration on interactive prompts. 795 * Set :c:member:`~PyConfig.use_environment` and 796 :c:member:`~PyConfig.user_site_directory` to 0. 797 798 Default: ``0`` in Python mode, ``1`` in isolated mode. 799 800 See also :c:member:`PyPreConfig.isolated`. 801 802 .. c:member:: int legacy_windows_stdio 803 804 If non-zero, use :class:`io.FileIO` instead of 805 :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout` 806 and :data:`sys.stderr`. 807 808 Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSSTDIO` environment 809 variable is set to a non-empty string. 810 811 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for 812 Windows specific code. 813 814 Default: ``0``. 815 816 See also the :pep:`528` (Change Windows console encoding to UTF-8). 817 818 .. c:member:: int malloc_stats 819 820 If non-zero, dump statistics on :ref:`Python pymalloc memory allocator 821 <pymalloc>` at exit. 822 823 Set to ``1`` by the :envvar:`PYTHONMALLOCSTATS` environment variable. 824 825 The option is ignored if Python is :option:`configured using 826 the --without-pymalloc option <--without-pymalloc>`. 827 828 Default: ``0``. 829 830 .. c:member:: wchar_t* platlibdir 831 832 Platform library directory name: :data:`sys.platlibdir`. 833 834 Set by the :envvar:`PYTHONPLATLIBDIR` environment variable. 835 836 Default: value of the ``PLATLIBDIR`` macro which is set by the 837 :option:`configure --with-platlibdir option <--with-platlibdir>` 838 (default: ``"lib"``). 839 840 Part of the :ref:`Python Path Configuration <init-path-config>` input. 841 842 .. versionadded:: 3.9 843 844 .. c:member:: wchar_t* pythonpath_env 845 846 Module search paths (:data:`sys.path`) as a string separated by ``DELIM`` 847 (:data:`os.path.pathsep`). 848 849 Set by the :envvar:`PYTHONPATH` environment variable. 850 851 Default: ``NULL``. 852 853 Part of the :ref:`Python Path Configuration <init-path-config>` input. 854 855 .. c:member:: PyWideStringList module_search_paths 856 .. c:member:: int module_search_paths_set 857 858 Module search paths: :data:`sys.path`. 859 860 If :c:member:`~PyConfig.module_search_paths_set` is equal to 0, the 861 function calculating the :ref:`Python Path Configuration <init-path-config>` 862 overrides the :c:member:`~PyConfig.module_search_paths` and sets 863 :c:member:`~PyConfig.module_search_paths_set` to ``1``. 864 865 Default: empty list (``module_search_paths``) and ``0`` 866 (``module_search_paths_set``). 867 868 Part of the :ref:`Python Path Configuration <init-path-config>` output. 869 870 .. c:member:: int optimization_level 871 872 Compilation optimization level: 873 874 * ``0``: Peephole optimizer, set ``__debug__`` to ``True``. 875 * ``1``: Level 0, remove assertions, set ``__debug__`` to ``False``. 876 * ``2``: Level 1, strip docstrings. 877 878 Incremented by the :option:`-O` command line option. Set to the 879 :envvar:`PYTHONOPTIMIZE` environment variable value. 880 881 Default: ``0``. 882 883 .. c:member:: PyWideStringList orig_argv 884 885 The list of the original command line arguments passed to the Python 886 executable: :data:`sys.orig_argv`. 887 888 If :c:member:`~PyConfig.orig_argv` list is empty and 889 :c:member:`~PyConfig.argv` is not a list only containing an empty 890 string, :c:func:`PyConfig_Read` copies :c:member:`~PyConfig.argv` into 891 :c:member:`~PyConfig.orig_argv` before modifying 892 :c:member:`~PyConfig.argv` (if :c:member:`~PyConfig.parse_argv` is 893 non-zero). 894 895 See also the :c:member:`~PyConfig.argv` member and the 896 :c:func:`Py_GetArgcArgv` function. 897 898 Default: empty list. 899 900 .. versionadded:: 3.10 901 902 .. c:member:: int parse_argv 903 904 Parse command line arguments? 905 906 If equals to ``1``, parse :c:member:`~PyConfig.argv` the same way the regular 907 Python parses :ref:`command line arguments <using-on-cmdline>`, and strip 908 Python arguments from :c:member:`~PyConfig.argv`. 909 910 The :c:func:`PyConfig_Read` function only parses 911 :c:member:`PyConfig.argv` arguments once: :c:member:`PyConfig.parse_argv` 912 is set to ``2`` after arguments are parsed. Since Python arguments are 913 strippped from :c:member:`PyConfig.argv`, parsing arguments twice would 914 parse the application options as Python options. 915 916 Default: ``1`` in Python mode, ``0`` in isolated mode. 917 918 .. versionchanged:: 3.10 919 The :c:member:`PyConfig.argv` arguments are now only parsed if 920 :c:member:`PyConfig.parse_argv` equals to ``1``. 921 922 .. c:member:: int parser_debug 923 924 Parser debug mode. If greater than 0, turn on parser debugging output (for expert only, depending 925 on compilation options). 926 927 Incremented by the :option:`-d` command line option. Set to the 928 :envvar:`PYTHONDEBUG` environment variable value. 929 930 Default: ``0``. 931 932 .. c:member:: int pathconfig_warnings 933 934 On Unix, if non-zero, calculating the :ref:`Python Path Configuration 935 <init-path-config>` can log warnings into ``stderr``. If equals to 0, 936 suppress these warnings. 937 938 It has no effect on Windows. 939 940 Default: ``1`` in Python mode, ``0`` in isolated mode. 941 942 Part of the :ref:`Python Path Configuration <init-path-config>` input. 943 944 .. c:member:: wchar_t* prefix 945 946 The site-specific directory prefix where the platform independent Python 947 files are installed: :data:`sys.prefix`. 948 949 Default: ``NULL``. 950 951 Part of the :ref:`Python Path Configuration <init-path-config>` output. 952 953 .. c:member:: wchar_t* program_name 954 955 Program name used to initialize :c:member:`~PyConfig.executable` and in 956 early error messages during Python initialization. 957 958 * If :func:`Py_SetProgramName` has been called, use its argument. 959 * On macOS, use :envvar:`PYTHONEXECUTABLE` environment variable if set. 960 * If the ``WITH_NEXT_FRAMEWORK`` macro is defined, use 961 :envvar:`__PYVENV_LAUNCHER__` environment variable if set. 962 * Use ``argv[0]`` of :c:member:`~PyConfig.argv` if available and 963 non-empty. 964 * Otherwise, use ``L"python"`` on Windows, or ``L"python3"`` on other 965 platforms. 966 967 Default: ``NULL``. 968 969 Part of the :ref:`Python Path Configuration <init-path-config>` input. 970 971 .. c:member:: wchar_t* pycache_prefix 972 973 Directory where cached ``.pyc`` files are written: 974 :data:`sys.pycache_prefix`. 975 976 Set by the :option:`-X pycache_prefix=PATH <-X>` command line option and 977 the :envvar:`PYTHONPYCACHEPREFIX` environment variable. 978 979 If ``NULL``, :data:`sys.pycache_prefix` is set to ``None``. 980 981 Default: ``NULL``. 982 983 .. c:member:: int quiet 984 985 Quiet mode. If greater than 0, don't display the copyright and version at 986 Python startup in interactive mode. 987 988 Incremented by the :option:`-q` command line option. 989 990 Default: ``0``. 991 992 .. c:member:: wchar_t* run_command 993 994 Value of the :option:`-c` command line option. 995 996 Used by :c:func:`Py_RunMain`. 997 998 Default: ``NULL``. 999 1000 .. c:member:: wchar_t* run_filename 1001 1002 Filename passed on the command line: trailing command line argument 1003 without :option:`-c` or :option:`-m`. 1004 1005 For example, it is set to ``script.py`` by the ``python3 script.py arg`` 1006 command. 1007 1008 Used by :c:func:`Py_RunMain`. 1009 1010 Default: ``NULL``. 1011 1012 .. c:member:: wchar_t* run_module 1013 1014 Value of the :option:`-m` command line option. 1015 1016 Used by :c:func:`Py_RunMain`. 1017 1018 Default: ``NULL``. 1019 1020 .. c:member:: int show_ref_count 1021 1022 Show total reference count at exit? 1023 1024 Set to 1 by :option:`-X showrefcount <-X>` command line option. 1025 1026 Need a :ref:`debug build of Python <debug-build>` (the ``Py_REF_DEBUG`` 1027 macro must be defined). 1028 1029 Default: ``0``. 1030 1031 .. c:member:: int site_import 1032 1033 Import the :mod:`site` module at startup? 1034 1035 If equal to zero, disable the import of the module site and the 1036 site-dependent manipulations of :data:`sys.path` that it entails. 1037 1038 Also disable these manipulations if the :mod:`site` module is explicitly 1039 imported later (call :func:`site.main` if you want them to be triggered). 1040 1041 Set to ``0`` by the :option:`-S` command line option. 1042 1043 :data:`sys.flags.no_site` is set to the inverted value of 1044 :c:member:`~PyConfig.site_import`. 1045 1046 Default: ``1``. 1047 1048 .. c:member:: int skip_source_first_line 1049 1050 If non-zero, skip the first line of the :c:member:`PyConfig.run_filename` 1051 source. 1052 1053 It allows the usage of non-Unix forms of ``#!cmd``. This is intended for 1054 a DOS specific hack only. 1055 1056 Set to ``1`` by the :option:`-x` command line option. 1057 1058 Default: ``0``. 1059 1060 .. c:member:: wchar_t* stdio_encoding 1061 .. c:member:: wchar_t* stdio_errors 1062 1063 Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and 1064 :data:`sys.stderr` (but :data:`sys.stderr` always uses 1065 ``"backslashreplace"`` error handler). 1066 1067 If :c:func:`Py_SetStandardStreamEncoding` has been called, use its 1068 *error* and *errors* arguments if they are not ``NULL``. 1069 1070 Use the :envvar:`PYTHONIOENCODING` environment variable if it is 1071 non-empty. 1072 1073 Default encoding: 1074 1075 * ``"UTF-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero. 1076 * Otherwise, use the :term:`locale encoding`. 1077 1078 Default error handler: 1079 1080 * On Windows: use ``"surrogateescape"``. 1081 * ``"surrogateescape"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero, 1082 or if the LC_CTYPE locale is "C" or "POSIX". 1083 * ``"strict"`` otherwise. 1084 1085 .. c:member:: int tracemalloc 1086 1087 Enable tracemalloc? 1088 1089 If non-zero, call :func:`tracemalloc.start` at startup. 1090 1091 Set by :option:`-X tracemalloc=N <-X>` command line option and by the 1092 :envvar:`PYTHONTRACEMALLOC` environment variable. 1093 1094 Default: ``-1`` in Python mode, ``0`` in isolated mode. 1095 1096 .. c:member:: int use_environment 1097 1098 Use :ref:`environment variables <using-on-envvars>`? 1099 1100 If equals to zero, ignore the :ref:`environment variables 1101 <using-on-envvars>`. 1102 1103 Default: ``1`` in Python config and ``0`` in isolated config. 1104 1105 .. c:member:: int user_site_directory 1106 1107 If non-zero, add the user site directory to :data:`sys.path`. 1108 1109 Set to ``0`` by the :option:`-s` and :option:`-I` command line options. 1110 1111 Set to ``0`` by the :envvar:`PYTHONNOUSERSITE` environment variable. 1112 1113 Default: ``1`` in Python mode, ``0`` in isolated mode. 1114 1115 .. c:member:: int verbose 1116 1117 Verbose mode. If greater than 0, print a message each time a module is 1118 imported, showing the place (filename or built-in module) from which 1119 it is loaded. 1120 1121 If greater or equal to 2, print a message for each file that is checked 1122 for when searching for a module. Also provides information on module 1123 cleanup at exit. 1124 1125 Incremented by the :option:`-v` command line option. 1126 1127 Set to the :envvar:`PYTHONVERBOSE` environment variable value. 1128 1129 Default: ``0``. 1130 1131 .. c:member:: PyWideStringList warnoptions 1132 1133 Options of the :mod:`warnings` module to build warnings filters, lowest 1134 to highest priority: :data:`sys.warnoptions`. 1135 1136 The :mod:`warnings` module adds :data:`sys.warnoptions` in the reverse 1137 order: the last :c:member:`PyConfig.warnoptions` item becomes the first 1138 item of :data:`warnings.filters` which is checked first (highest 1139 priority). 1140 1141 The :option:`-W` command line options adds its value to 1142 :c:member:`~PyConfig.warnoptions`, it can be used multiple times. 1143 1144 The :envvar:`PYTHONWARNINGS` environment variable can also be used to add 1145 warning options. Multiple options can be specified, separated by commas 1146 (``,``). 1147 1148 Default: empty list. 1149 1150 .. c:member:: int write_bytecode 1151 1152 If equal to 0, Python won't try to write ``.pyc`` files on the import of 1153 source modules. 1154 1155 Set to ``0`` by the :option:`-B` command line option and the 1156 :envvar:`PYTHONDONTWRITEBYTECODE` environment variable. 1157 1158 :data:`sys.dont_write_bytecode` is initialized to the inverted value of 1159 :c:member:`~PyConfig.write_bytecode`. 1160 1161 Default: ``1``. 1162 1163 .. c:member:: PyWideStringList xoptions 1164 1165 Values of the :option:`-X` command line options: :data:`sys._xoptions`. 1166 1167 Default: empty list. 1168 1169If :c:member:`~PyConfig.parse_argv` is non-zero, :c:member:`~PyConfig.argv` 1170arguments are parsed the same way the regular Python parses :ref:`command line 1171arguments <using-on-cmdline>`, and Python arguments are stripped from 1172:c:member:`~PyConfig.argv`. 1173 1174The :c:member:`~PyConfig.xoptions` options are parsed to set other options: see 1175the :option:`-X` command line option. 1176 1177.. versionchanged:: 3.9 1178 1179 The ``show_alloc_count`` field has been removed. 1180 1181 1182Initialization with PyConfig 1183============================ 1184 1185Function to initialize Python: 1186 1187.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config) 1188 1189 Initialize Python from *config* configuration. 1190 1191The caller is responsible to handle exceptions (error or exit) using 1192:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`. 1193 1194If :c:func:`PyImport_FrozenModules`, :c:func:`PyImport_AppendInittab` or 1195:c:func:`PyImport_ExtendInittab` are used, they must be set or called after 1196Python preinitialization and before the Python initialization. If Python is 1197initialized multiple times, :c:func:`PyImport_AppendInittab` or 1198:c:func:`PyImport_ExtendInittab` must be called before each Python 1199initialization. 1200 1201The current configuration (``PyConfig`` type) is stored in 1202``PyInterpreterState.config``. 1203 1204Example setting the program name:: 1205 1206 void init_python(void) 1207 { 1208 PyStatus status; 1209 1210 PyConfig config; 1211 PyConfig_InitPythonConfig(&config); 1212 1213 /* Set the program name. Implicitly preinitialize Python. */ 1214 status = PyConfig_SetString(&config, &config.program_name, 1215 L"/path/to/my_program"); 1216 if (PyStatus_Exception(status)) { 1217 goto exception; 1218 } 1219 1220 status = Py_InitializeFromConfig(&config); 1221 if (PyStatus_Exception(status)) { 1222 goto exception; 1223 } 1224 PyConfig_Clear(&config); 1225 return; 1226 1227 exception: 1228 PyConfig_Clear(&config); 1229 Py_ExitStatusException(status); 1230 } 1231 1232More complete example modifying the default configuration, read the 1233configuration, and then override some parameters:: 1234 1235 PyStatus init_python(const char *program_name) 1236 { 1237 PyStatus status; 1238 1239 PyConfig config; 1240 PyConfig_InitPythonConfig(&config); 1241 1242 /* Set the program name before reading the configuration 1243 (decode byte string from the locale encoding). 1244 1245 Implicitly preinitialize Python. */ 1246 status = PyConfig_SetBytesString(&config, &config.program_name, 1247 program_name); 1248 if (PyStatus_Exception(status)) { 1249 goto done; 1250 } 1251 1252 /* Read all configuration at once */ 1253 status = PyConfig_Read(&config); 1254 if (PyStatus_Exception(status)) { 1255 goto done; 1256 } 1257 1258 /* Append our custom search path to sys.path */ 1259 status = PyWideStringList_Append(&config.module_search_paths, 1260 L"/path/to/more/modules"); 1261 if (PyStatus_Exception(status)) { 1262 goto done; 1263 } 1264 1265 /* Override executable computed by PyConfig_Read() */ 1266 status = PyConfig_SetString(&config, &config.executable, 1267 L"/path/to/my_executable"); 1268 if (PyStatus_Exception(status)) { 1269 goto done; 1270 } 1271 1272 status = Py_InitializeFromConfig(&config); 1273 1274 done: 1275 PyConfig_Clear(&config); 1276 return status; 1277 } 1278 1279 1280.. _init-isolated-conf: 1281 1282Isolated Configuration 1283====================== 1284 1285:c:func:`PyPreConfig_InitIsolatedConfig` and 1286:c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to 1287isolate Python from the system. For example, to embed Python into an 1288application. 1289 1290This configuration ignores global configuration variables, environments 1291variables, command line arguments (:c:member:`PyConfig.argv` is not parsed) 1292and user site directory. The C standard streams (ex: ``stdout``) and the 1293LC_CTYPE locale are left unchanged. Signal handlers are not installed. 1294 1295Configuration files are still used with this configuration. Set the 1296:ref:`Python Path Configuration <init-path-config>` ("output fields") to ignore these 1297configuration files and avoid the function computing the default path 1298configuration. 1299 1300 1301.. _init-python-config: 1302 1303Python Configuration 1304==================== 1305 1306:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig` 1307functions create a configuration to build a customized Python which behaves as 1308the regular Python. 1309 1310Environments variables and command line arguments are used to configure 1311Python, whereas global configuration variables are ignored. 1312 1313This function enables C locale coercion (:pep:`538`) 1314and :ref:`Python UTF-8 Mode <utf8-mode>` 1315(:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and 1316:envvar:`PYTHONCOERCECLOCALE` environment variables. 1317 1318 1319.. _init-path-config: 1320 1321Python Path Configuration 1322========================= 1323 1324:c:type:`PyConfig` contains multiple fields for the path configuration: 1325 1326* Path configuration inputs: 1327 1328 * :c:member:`PyConfig.home` 1329 * :c:member:`PyConfig.platlibdir` 1330 * :c:member:`PyConfig.pathconfig_warnings` 1331 * :c:member:`PyConfig.program_name` 1332 * :c:member:`PyConfig.pythonpath_env` 1333 * current working directory: to get absolute paths 1334 * ``PATH`` environment variable to get the program full path 1335 (from :c:member:`PyConfig.program_name`) 1336 * ``__PYVENV_LAUNCHER__`` environment variable 1337 * (Windows only) Application paths in the registry under 1338 "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and 1339 HKEY_LOCAL_MACHINE (where X.Y is the Python version). 1340 1341* Path configuration output fields: 1342 1343 * :c:member:`PyConfig.base_exec_prefix` 1344 * :c:member:`PyConfig.base_executable` 1345 * :c:member:`PyConfig.base_prefix` 1346 * :c:member:`PyConfig.exec_prefix` 1347 * :c:member:`PyConfig.executable` 1348 * :c:member:`PyConfig.module_search_paths_set`, 1349 :c:member:`PyConfig.module_search_paths` 1350 * :c:member:`PyConfig.prefix` 1351 1352If at least one "output field" is not set, Python calculates the path 1353configuration to fill unset fields. If 1354:c:member:`~PyConfig.module_search_paths_set` is equal to 0, 1355:c:member:`~PyConfig.module_search_paths` is overridden and 1356:c:member:`~PyConfig.module_search_paths_set` is set to 1. 1357 1358It is possible to completely ignore the function calculating the default 1359path configuration by setting explicitly all path configuration output 1360fields listed above. A string is considered as set even if it is non-empty. 1361``module_search_paths`` is considered as set if 1362``module_search_paths_set`` is set to 1. In this case, path 1363configuration input fields are ignored as well. 1364 1365Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when 1366calculating the path configuration (Unix only, Windows does not log any warning). 1367 1368If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix` 1369fields are not set, they inherit their value from :c:member:`~PyConfig.prefix` 1370and :c:member:`~PyConfig.exec_prefix` respectively. 1371 1372:c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`: 1373 1374* If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a 1375 ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to 1376 :data:`sys.path`. 1377* If :c:member:`~PyConfig.isolated` is zero: 1378 1379 * If :c:member:`~PyConfig.run_module` is set, prepend the current directory 1380 to :data:`sys.path`. Do nothing if the current directory cannot be read. 1381 * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the 1382 filename to :data:`sys.path`. 1383 * Otherwise, prepend an empty string to :data:`sys.path`. 1384 1385If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be 1386modified by the :mod:`site` module. If 1387:c:member:`~PyConfig.user_site_directory` is non-zero and the user's 1388site-package directory exists, the :mod:`site` module appends the user's 1389site-package directory to :data:`sys.path`. 1390 1391The following configuration files are used by the path configuration: 1392 1393* ``pyvenv.cfg`` 1394* ``python._pth`` (Windows only) 1395* ``pybuilddir.txt`` (Unix only) 1396 1397The ``__PYVENV_LAUNCHER__`` environment variable is used to set 1398:c:member:`PyConfig.base_executable` 1399 1400 1401Py_RunMain() 1402============ 1403 1404.. c:function:: int Py_RunMain(void) 1405 1406 Execute the command (:c:member:`PyConfig.run_command`), the script 1407 (:c:member:`PyConfig.run_filename`) or the module 1408 (:c:member:`PyConfig.run_module`) specified on the command line or in the 1409 configuration. 1410 1411 By default and when if :option:`-i` option is used, run the REPL. 1412 1413 Finally, finalizes Python and returns an exit status that can be passed to 1414 the ``exit()`` function. 1415 1416See :ref:`Python Configuration <init-python-config>` for an example of 1417customized Python always running in isolated mode using 1418:c:func:`Py_RunMain`. 1419 1420 1421Py_GetArgcArgv() 1422================ 1423 1424.. c:function:: void Py_GetArgcArgv(int *argc, wchar_t ***argv) 1425 1426 Get the original command line arguments, before Python modified them. 1427 1428 See also :c:member:`PyConfig.orig_argv` member. 1429 1430 1431Multi-Phase Initialization Private Provisional API 1432================================================== 1433 1434This section is a private provisional API introducing multi-phase 1435initialization, the core feature of the :pep:`432`: 1436 1437* "Core" initialization phase, "bare minimum Python": 1438 1439 * Builtin types; 1440 * Builtin exceptions; 1441 * Builtin and frozen modules; 1442 * The :mod:`sys` module is only partially initialized 1443 (ex: :data:`sys.path` doesn't exist yet). 1444 1445* "Main" initialization phase, Python is fully initialized: 1446 1447 * Install and configure :mod:`importlib`; 1448 * Apply the :ref:`Path Configuration <init-path-config>`; 1449 * Install signal handlers; 1450 * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout` 1451 and :data:`sys.path`); 1452 * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`; 1453 * Import the :mod:`site` module; 1454 * etc. 1455 1456Private provisional API: 1457 1458* :c:member:`PyConfig._init_main`: if set to 0, 1459 :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase. 1460* :c:member:`PyConfig._isolated_interpreter`: if non-zero, 1461 disallow threads, subprocesses and fork. 1462 1463.. c:function:: PyStatus _Py_InitializeMain(void) 1464 1465 Move to the "Main" initialization phase, finish the Python initialization. 1466 1467No module is imported during the "Core" phase and the ``importlib`` module is 1468not configured: the :ref:`Path Configuration <init-path-config>` is only 1469applied during the "Main" phase. It may allow to customize Python in Python to 1470override or tune the :ref:`Path Configuration <init-path-config>`, maybe 1471install a custom :data:`sys.meta_path` importer or an import hook, etc. 1472 1473It may become possible to calculatin the :ref:`Path Configuration 1474<init-path-config>` in Python, after the Core phase and before the Main phase, 1475which is one of the :pep:`432` motivation. 1476 1477The "Core" phase is not properly defined: what should be and what should 1478not be available at this phase is not specified yet. The API is marked 1479as private and provisional: the API can be modified or even be removed 1480anytime until a proper public API is designed. 1481 1482Example running Python code between "Core" and "Main" initialization 1483phases:: 1484 1485 void init_python(void) 1486 { 1487 PyStatus status; 1488 1489 PyConfig config; 1490 PyConfig_InitPythonConfig(&config); 1491 config._init_main = 0; 1492 1493 /* ... customize 'config' configuration ... */ 1494 1495 status = Py_InitializeFromConfig(&config); 1496 PyConfig_Clear(&config); 1497 if (PyStatus_Exception(status)) { 1498 Py_ExitStatusException(status); 1499 } 1500 1501 /* Use sys.stderr because sys.stdout is only created 1502 by _Py_InitializeMain() */ 1503 int res = PyRun_SimpleString( 1504 "import sys; " 1505 "print('Run Python code before _Py_InitializeMain', " 1506 "file=sys.stderr)"); 1507 if (res < 0) { 1508 exit(1); 1509 } 1510 1511 /* ... put more configuration code here ... */ 1512 1513 status = _Py_InitializeMain(); 1514 if (PyStatus_Exception(status)) { 1515 Py_ExitStatusException(status); 1516 } 1517 } 1518