• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. highlight:: c
2
3.. _init-config:
4
5***********************************
6Python Initialization Configuration
7***********************************
8
9.. versionadded:: 3.8
10
11Structures:
12
13* :c:type:`PyConfig`
14* :c:type:`PyPreConfig`
15* :c:type:`PyStatus`
16* :c:type:`PyWideStringList`
17
18Functions:
19
20* :c:func:`PyConfig_Clear`
21* :c:func:`PyConfig_InitIsolatedConfig`
22* :c:func:`PyConfig_InitPythonConfig`
23* :c:func:`PyConfig_Read`
24* :c:func:`PyConfig_SetArgv`
25* :c:func:`PyConfig_SetBytesArgv`
26* :c:func:`PyConfig_SetBytesString`
27* :c:func:`PyConfig_SetString`
28* :c:func:`PyConfig_SetWideStringList`
29* :c:func:`PyPreConfig_InitIsolatedConfig`
30* :c:func:`PyPreConfig_InitPythonConfig`
31* :c:func:`PyStatus_Error`
32* :c:func:`PyStatus_Exception`
33* :c:func:`PyStatus_Exit`
34* :c:func:`PyStatus_IsError`
35* :c:func:`PyStatus_IsExit`
36* :c:func:`PyStatus_NoMemory`
37* :c:func:`PyStatus_Ok`
38* :c:func:`PyWideStringList_Append`
39* :c:func:`PyWideStringList_Insert`
40* :c:func:`Py_ExitStatusException`
41* :c:func:`Py_InitializeFromConfig`
42* :c:func:`Py_PreInitialize`
43* :c:func:`Py_PreInitializeFromArgs`
44* :c:func:`Py_PreInitializeFromBytesArgs`
45* :c:func:`Py_RunMain`
46
47The preconfiguration (``PyPreConfig`` type) is stored in
48``_PyRuntime.preconfig`` and the configuration (``PyConfig`` type) is stored in
49``PyInterpreterState.config``.
50
51See also :ref:`Initialization, Finalization, and Threads <initialization>`.
52
53.. seealso::
54   :pep:`587` "Python Initialization Configuration".
55
56
57PyWideStringList
58----------------
59
60.. c:type:: PyWideStringList
61
62   List of ``wchar_t*`` strings.
63
64   If *length* is non-zero, *items* must be non-``NULL`` and all strings must be
65   non-``NULL``.
66
67   Methods:
68
69   .. c:function:: PyStatus PyWideStringList_Append(PyWideStringList *list, const wchar_t *item)
70
71      Append *item* to *list*.
72
73      Python must be preinitialized to call this function.
74
75   .. c:function:: PyStatus PyWideStringList_Insert(PyWideStringList *list, Py_ssize_t index, const wchar_t *item)
76
77      Insert *item* into *list* at *index*.
78
79      If *index* is greater than or equal to *list* length, append *item* to
80      *list*.
81
82      *index* must be greater than or equal to 0.
83
84      Python must be preinitialized to call this function.
85
86   Structure fields:
87
88   .. c:member:: Py_ssize_t length
89
90      List length.
91
92   .. c:member:: wchar_t** items
93
94      List items.
95
96PyStatus
97--------
98
99.. c:type:: PyStatus
100
101   Structure to store an initialization function status: success, error
102   or exit.
103
104   For an error, it can store the C function name which created the error.
105
106   Structure fields:
107
108   .. c:member:: int exitcode
109
110      Exit code. Argument passed to ``exit()``.
111
112   .. c:member:: const char *err_msg
113
114      Error message.
115
116   .. c:member:: const char *func
117
118      Name of the function which created an error, can be ``NULL``.
119
120   Functions to create a status:
121
122   .. c:function:: PyStatus PyStatus_Ok(void)
123
124      Success.
125
126   .. c:function:: PyStatus PyStatus_Error(const char *err_msg)
127
128      Initialization error with a message.
129
130   .. c:function:: PyStatus PyStatus_NoMemory(void)
131
132      Memory allocation failure (out of memory).
133
134   .. c:function:: PyStatus PyStatus_Exit(int exitcode)
135
136      Exit Python with the specified exit code.
137
138   Functions to handle a status:
139
140   .. c:function:: int PyStatus_Exception(PyStatus status)
141
142      Is the status an error or an exit? If true, the exception must be
143      handled; by calling :c:func:`Py_ExitStatusException` for example.
144
145   .. c:function:: int PyStatus_IsError(PyStatus status)
146
147      Is the result an error?
148
149   .. c:function:: int PyStatus_IsExit(PyStatus status)
150
151      Is the result an exit?
152
153   .. c:function:: void Py_ExitStatusException(PyStatus status)
154
155      Call ``exit(exitcode)`` if *status* is an exit. Print the error
156      message and exit with a non-zero exit code if *status* is an error.  Must
157      only be called if ``PyStatus_Exception(status)`` is non-zero.
158
159.. note::
160   Internally, Python uses macros which set ``PyStatus.func``,
161   whereas functions to create a status set ``func`` to ``NULL``.
162
163Example::
164
165    PyStatus alloc(void **ptr, size_t size)
166    {
167        *ptr = PyMem_RawMalloc(size);
168        if (*ptr == NULL) {
169            return PyStatus_NoMemory();
170        }
171        return PyStatus_Ok();
172    }
173
174    int main(int argc, char **argv)
175    {
176        void *ptr;
177        PyStatus status = alloc(&ptr, 16);
178        if (PyStatus_Exception(status)) {
179            Py_ExitStatusException(status);
180        }
181        PyMem_Free(ptr);
182        return 0;
183    }
184
185
186PyPreConfig
187-----------
188
189.. c:type:: PyPreConfig
190
191   Structure used to preinitialize Python:
192
193   * Set the Python memory allocator
194   * Configure the LC_CTYPE locale
195   * Set the UTF-8 mode
196
197   Function to initialize a preconfiguration:
198
199   .. c:function:: void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
200
201      Initialize the preconfiguration with :ref:`Python Configuration
202      <init-python-config>`.
203
204   .. c:function:: void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
205
206      Initialize the preconfiguration with :ref:`Isolated Configuration
207      <init-isolated-conf>`.
208
209   Structure fields:
210
211   .. c:member:: int allocator
212
213      Name of the memory allocator:
214
215      * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators
216        (use defaults)
217      * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators
218      * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with
219        debug hooks
220      * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()``
221      * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of
222        ``malloc()`` with debug hooks
223      * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory
224        allocator <pymalloc>`
225      * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc
226        memory allocator <pymalloc>` with debug hooks
227
228      ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG``
229      are not supported if Python is configured using ``--without-pymalloc``
230
231      See :ref:`Memory Management <memory>`.
232
233   .. c:member:: int configure_locale
234
235      Set the LC_CTYPE locale to the user preferred locale? If equals to 0, set
236      :c:member:`coerce_c_locale` and :c:member:`coerce_c_locale_warn` to 0.
237
238   .. c:member:: int coerce_c_locale
239
240      If equals to 2, coerce the C locale; if equals to 1, read the LC_CTYPE
241      locale to decide if it should be coerced.
242
243   .. c:member:: int coerce_c_locale_warn
244
245      If non-zero, emit a warning if the C locale is coerced.
246
247   .. c:member:: int dev_mode
248
249      See :c:member:`PyConfig.dev_mode`.
250
251   .. c:member:: int isolated
252
253      See :c:member:`PyConfig.isolated`.
254
255   .. c:member:: int legacy_windows_fs_encoding (Windows only)
256
257      If non-zero, disable UTF-8 Mode, set the Python filesystem encoding to
258      ``mbcs``, set the filesystem error handler to ``replace``.
259
260      Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
261      Windows specific code.
262
263   .. c:member:: int parse_argv
264
265      If non-zero, :c:func:`Py_PreInitializeFromArgs` and
266      :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the
267      same way the regular Python parses command line arguments: see
268      :ref:`Command Line Arguments <using-on-cmdline>`.
269
270   .. c:member:: int use_environment
271
272      See :c:member:`PyConfig.use_environment`.
273
274   .. c:member:: int utf8_mode
275
276      If non-zero, enable the UTF-8 mode.
277
278Preinitialization with PyPreConfig
279----------------------------------
280
281Functions to preinitialize Python:
282
283.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
284
285   Preinitialize Python from *preconfig* preconfiguration.
286
287.. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv)
288
289   Preinitialize Python from *preconfig* preconfiguration and command line
290   arguments (bytes strings).
291
292.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)
293
294   Preinitialize Python from *preconfig* preconfiguration and command line
295   arguments (wide strings).
296
297The caller is responsible to handle exceptions (error or exit) using
298:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
299
300For :ref:`Python Configuration <init-python-config>`
301(:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with
302command line arguments, the command line arguments must also be passed to
303preinitialize Python, since they have an effect on the pre-configuration
304like encodings. For example, the :option:`-X utf8 <-X>` command line option
305enables the UTF-8 Mode.
306
307``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and
308before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator.
309It can be called before :c:func:`Py_PreInitialize` if
310:c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``.
311
312Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be
313used before Python preinitialization, whereas calling directly ``malloc()`` and
314``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called before
315the preinitialization.
316
317Example using the preinitialization to enable the UTF-8 Mode::
318
319    PyStatus status;
320    PyPreConfig preconfig;
321    PyPreConfig_InitPythonConfig(&preconfig);
322
323    preconfig.utf8_mode = 1;
324
325    status = Py_PreInitialize(&preconfig);
326    if (PyStatus_Exception(status)) {
327        Py_ExitStatusException(status);
328    }
329
330    /* at this point, Python will speak UTF-8 */
331
332    Py_Initialize();
333    /* ... use Python API here ... */
334    Py_Finalize();
335
336
337PyConfig
338--------
339
340.. c:type:: PyConfig
341
342   Structure containing most parameters to configure Python.
343
344   Structure methods:
345
346   .. c:function:: void PyConfig_InitPythonConfig(PyConfig *config)
347
348      Initialize configuration with :ref:`Python Configuration
349      <init-python-config>`.
350
351   .. c:function:: void PyConfig_InitIsolatedConfig(PyConfig *config)
352
353      Initialize configuration with :ref:`Isolated Configuration
354      <init-isolated-conf>`.
355
356   .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str)
357
358      Copy the wide character string *str* into ``*config_str``.
359
360      Preinitialize Python if needed.
361
362   .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str)
363
364      Decode *str* using ``Py_DecodeLocale()`` and set the result into ``*config_str``.
365
366      Preinitialize Python if needed.
367
368   .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv)
369
370      Set command line arguments from wide character strings.
371
372      Preinitialize Python if needed.
373
374   .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv)
375
376      Set command line arguments: decode bytes using :c:func:`Py_DecodeLocale`.
377
378      Preinitialize Python if needed.
379
380   .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items)
381
382      Set the list of wide strings *list* to *length* and *items*.
383
384      Preinitialize Python if needed.
385
386   .. c:function:: PyStatus PyConfig_Read(PyConfig *config)
387
388      Read all Python configuration.
389
390      Fields which are already initialized are left unchanged.
391
392      Preinitialize Python if needed.
393
394   .. c:function:: void PyConfig_Clear(PyConfig *config)
395
396      Release configuration memory.
397
398   Most ``PyConfig`` methods preinitialize Python if needed. In that case, the
399   Python preinitialization configuration in based on the :c:type:`PyConfig`.
400   If configuration fields which are in common with :c:type:`PyPreConfig` are
401   tuned, they must be set before calling a :c:type:`PyConfig` method:
402
403   * :c:member:`~PyConfig.dev_mode`
404   * :c:member:`~PyConfig.isolated`
405   * :c:member:`~PyConfig.parse_argv`
406   * :c:member:`~PyConfig.use_environment`
407
408   Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv`
409   is used, this method must be called first, before other methods, since the
410   preinitialization configuration depends on command line arguments (if
411   :c:member:`parse_argv` is non-zero).
412
413   The caller of these methods is responsible to handle exceptions (error or
414   exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``.
415
416   Structure fields:
417
418   .. c:member:: PyWideStringList argv
419
420      Command line arguments, :data:`sys.argv`. See
421      :c:member:`~PyConfig.parse_argv` to parse :c:member:`~PyConfig.argv` the
422      same way the regular Python parses Python command line arguments. If
423      :c:member:`~PyConfig.argv` is empty, an empty string is added to ensure
424      that :data:`sys.argv` always exists and is never empty.
425
426   .. c:member:: wchar_t* base_exec_prefix
427
428      :data:`sys.base_exec_prefix`.
429
430   .. c:member:: wchar_t* base_executable
431
432      :data:`sys._base_executable`: ``__PYVENV_LAUNCHER__`` environment
433      variable value, or copy of :c:member:`PyConfig.executable`.
434
435   .. c:member:: wchar_t* base_prefix
436
437      :data:`sys.base_prefix`.
438
439   .. c:member:: int buffered_stdio
440
441      If equals to 0, enable unbuffered mode, making the stdout and stderr
442      streams unbuffered.
443
444      stdin is always opened in buffered mode.
445
446   .. c:member:: int bytes_warning
447
448      If equals to 1, issue a warning when comparing :class:`bytes` or
449      :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with
450      :class:`int`. If equal or greater to 2, raise a :exc:`BytesWarning`
451      exception.
452
453   .. c:member:: wchar_t* check_hash_pycs_mode
454
455      Control the validation behavior of hash-based ``.pyc`` files (see
456      :pep:`552`): :option:`--check-hash-based-pycs` command line option value.
457
458      Valid values: ``always``, ``never`` and ``default``.
459
460      The default value is: ``default``.
461
462   .. c:member:: int configure_c_stdio
463
464      If non-zero, configure C standard streams (``stdio``, ``stdout``,
465      ``stdout``). For example, set their mode to ``O_BINARY`` on Windows.
466
467   .. c:member:: int dev_mode
468
469      Development mode: see :option:`-X dev <-X>`.
470
471   .. c:member:: int dump_refs
472
473      If non-zero, dump all objects which are still alive at exit.
474
475      Require a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
476
477   .. c:member:: wchar_t* exec_prefix
478
479      :data:`sys.exec_prefix`.
480
481   .. c:member:: wchar_t* executable
482
483      :data:`sys.executable`.
484
485   .. c:member:: int faulthandler
486
487      If non-zero, call :func:`faulthandler.enable` at startup.
488
489   .. c:member:: wchar_t* filesystem_encoding
490
491      Filesystem encoding, :func:`sys.getfilesystemencoding`.
492
493   .. c:member:: wchar_t* filesystem_errors
494
495      Filesystem encoding errors, :func:`sys.getfilesystemencodeerrors`.
496
497   .. c:member:: unsigned long hash_seed
498   .. c:member:: int use_hash_seed
499
500      Randomized hash function seed.
501
502      If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly
503      at Pythonstartup, and :c:member:`~PyConfig.hash_seed` is ignored.
504
505   .. c:member:: wchar_t* home
506
507      Python home directory.
508
509      Initialized from :envvar:`PYTHONHOME` environment variable value by
510      default.
511
512   .. c:member:: int import_time
513
514      If non-zero, profile import time.
515
516   .. c:member:: int inspect
517
518      Enter interactive mode after executing a script or a command.
519
520   .. c:member:: int install_signal_handlers
521
522      Install signal handlers?
523
524   .. c:member:: int interactive
525
526      Interactive mode.
527
528   .. c:member:: int isolated
529
530      If greater than 0, enable isolated mode:
531
532      * :data:`sys.path` contains neither the script's directory (computed from
533        ``argv[0]`` or the current directory) nor the user's site-packages
534        directory.
535      * Python REPL doesn't import :mod:`readline` nor enable default readline
536        configuration on interactive prompts.
537      * Set :c:member:`~PyConfig.use_environment` and
538        :c:member:`~PyConfig.user_site_directory` to 0.
539
540   .. c:member:: int legacy_windows_stdio
541
542      If non-zero, use :class:`io.FileIO` instead of
543      :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout`
544      and :data:`sys.stderr`.
545
546      Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
547      Windows specific code.
548
549   .. c:member:: int malloc_stats
550
551      If non-zero, dump statistics on :ref:`Python pymalloc memory allocator
552      <pymalloc>` at exit.
553
554      The option is ignored if Python is built using ``--without-pymalloc``.
555
556   .. c:member:: wchar_t* pythonpath_env
557
558      Module search paths as a string separated by ``DELIM``
559      (:data:`os.path.pathsep`).
560
561      Initialized from :envvar:`PYTHONPATH` environment variable value by
562      default.
563
564   .. c:member:: PyWideStringList module_search_paths
565   .. c:member:: int module_search_paths_set
566
567      :data:`sys.path`. If :c:member:`~PyConfig.module_search_paths_set` is
568      equal to 0, the :c:member:`~PyConfig.module_search_paths` is overridden
569      by the function calculating the :ref:`Path Configuration
570      <init-path-config>`.
571
572   .. c:member:: int optimization_level
573
574      Compilation optimization level:
575
576      * 0: Peephole optimizer (and ``__debug__`` is set to ``True``)
577      * 1: Remove assertions, set ``__debug__`` to ``False``
578      * 2: Strip docstrings
579
580   .. c:member:: int parse_argv
581
582      If non-zero, parse :c:member:`~PyConfig.argv` the same way the regular
583      Python command line arguments, and strip Python arguments from
584      :c:member:`~PyConfig.argv`: see :ref:`Command Line Arguments
585      <using-on-cmdline>`.
586
587   .. c:member:: int parser_debug
588
589      If non-zero, turn on parser debugging output (for expert only, depending
590      on compilation options).
591
592   .. c:member:: int pathconfig_warnings
593
594      If equal to 0, suppress warnings when calculating the :ref:`Path
595      Configuration <init-path-config>` (Unix only, Windows does not log any
596      warning). Otherwise, warnings are written into ``stderr``.
597
598   .. c:member:: wchar_t* prefix
599
600      :data:`sys.prefix`.
601
602   .. c:member:: wchar_t* program_name
603
604      Program name. Used to initialize :c:member:`~PyConfig.executable`, and in
605      early error messages.
606
607   .. c:member:: wchar_t* pycache_prefix
608
609      :data:`sys.pycache_prefix`: ``.pyc`` cache prefix.
610
611      If ``NULL``, :data:`sys.pycache_prefix` is set to ``None``.
612
613   .. c:member:: int quiet
614
615      Quiet mode. For example, don't display the copyright and version messages
616      in interactive mode.
617
618   .. c:member:: wchar_t* run_command
619
620      ``python3 -c COMMAND`` argument. Used by :c:func:`Py_RunMain`.
621
622   .. c:member:: wchar_t* run_filename
623
624      ``python3 FILENAME`` argument. Used by :c:func:`Py_RunMain`.
625
626   .. c:member:: wchar_t* run_module
627
628      ``python3 -m MODULE`` argument. Used by :c:func:`Py_RunMain`.
629
630   .. c:member:: int show_alloc_count
631
632      Show allocation counts at exit?
633
634      Set to 1 by :option:`-X showalloccount <-X>` command line option.
635
636      Need a special Python build with ``COUNT_ALLOCS`` macro defined.
637
638   .. c:member:: int show_ref_count
639
640      Show total reference count at exit?
641
642      Set to 1 by :option:`-X showrefcount <-X>` command line option.
643
644      Need a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
645
646   .. c:member:: int site_import
647
648      Import the :mod:`site` module at startup?
649
650   .. c:member:: int skip_source_first_line
651
652      Skip the first line of the source?
653
654   .. c:member:: wchar_t* stdio_encoding
655   .. c:member:: wchar_t* stdio_errors
656
657      Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
658      :data:`sys.stderr`.
659
660   .. c:member:: int tracemalloc
661
662      If non-zero, call :func:`tracemalloc.start` at startup.
663
664   .. c:member:: int use_environment
665
666      If greater than 0, use :ref:`environment variables <using-on-envvars>`.
667
668   .. c:member:: int user_site_directory
669
670      If non-zero, add user site directory to :data:`sys.path`.
671
672   .. c:member:: int verbose
673
674      If non-zero, enable verbose mode.
675
676   .. c:member:: PyWideStringList warnoptions
677
678      :data:`sys.warnoptions`: options of the :mod:`warnings` module to build
679      warnings filters: lowest to highest priority.
680
681      The :mod:`warnings` module adds :data:`sys.warnoptions` in the reverse
682      order: the last :c:member:`PyConfig.warnoptions` item becomes the first
683      item of :data:`warnings.filters` which is checked first (highest
684      priority).
685
686   .. c:member:: int write_bytecode
687
688      If non-zero, write ``.pyc`` files.
689
690      :data:`sys.dont_write_bytecode` is initialized to the inverted value of
691      :c:member:`~PyConfig.write_bytecode`.
692
693   .. c:member:: PyWideStringList xoptions
694
695      :data:`sys._xoptions`.
696
697If ``parse_argv`` is non-zero, ``argv`` arguments are parsed the same
698way the regular Python parses command line arguments, and Python
699arguments are stripped from ``argv``: see :ref:`Command Line Arguments
700<using-on-cmdline>`.
701
702The ``xoptions`` options are parsed to set other options: see :option:`-X`
703option.
704
705
706Initialization with PyConfig
707----------------------------
708
709Function to initialize Python:
710
711.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
712
713   Initialize Python from *config* configuration.
714
715The caller is responsible to handle exceptions (error or exit) using
716:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
717
718If ``PyImport_FrozenModules``, ``PyImport_AppendInittab()`` or
719``PyImport_ExtendInittab()`` are used, they must be set or called after Python
720preinitialization and before the Python initialization.
721
722Example setting the program name::
723
724    void init_python(void)
725    {
726        PyStatus status;
727
728        PyConfig config;
729        PyConfig_InitPythonConfig(&config);
730
731        /* Set the program name. Implicitly preinitialize Python. */
732        status = PyConfig_SetString(&config, &config.program_name,
733                                    L"/path/to/my_program");
734        if (PyStatus_Exception(status)) {
735            goto fail;
736        }
737
738        status = Py_InitializeFromConfig(&config);
739        if (PyStatus_Exception(status)) {
740            goto fail;
741        }
742        PyConfig_Clear(&config);
743        return;
744
745    fail:
746        PyConfig_Clear(&config);
747        Py_ExitStatusException(status);
748    }
749
750More complete example modifying the default configuration, read the
751configuration, and then override some parameters::
752
753    PyStatus init_python(const char *program_name)
754    {
755        PyStatus status;
756
757        PyConfig config;
758        PyConfig_InitPythonConfig(&config);
759
760        /* Set the program name before reading the configuraton
761           (decode byte string from the locale encoding).
762
763           Implicitly preinitialize Python. */
764        status = PyConfig_SetBytesString(&config, &config.program_name,
765                                      program_name);
766        if (PyStatus_Exception(status)) {
767            goto done;
768        }
769
770        /* Read all configuration at once */
771        status = PyConfig_Read(&config);
772        if (PyStatus_Exception(status)) {
773            goto done;
774        }
775
776        /* Append our custom search path to sys.path */
777        status = PyWideStringList_Append(&config.module_search_paths,
778                                         L"/path/to/more/modules");
779        if (PyStatus_Exception(status)) {
780            goto done;
781        }
782
783        /* Override executable computed by PyConfig_Read() */
784        status = PyConfig_SetString(&config, &config.executable,
785                                    L"/path/to/my_executable");
786        if (PyStatus_Exception(status)) {
787            goto done;
788        }
789
790        status = Py_InitializeFromConfig(&config);
791
792    done:
793        PyConfig_Clear(&config);
794        return status;
795    }
796
797
798.. _init-isolated-conf:
799
800Isolated Configuration
801----------------------
802
803:c:func:`PyPreConfig_InitIsolatedConfig` and
804:c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to
805isolate Python from the system. For example, to embed Python into an
806application.
807
808This configuration ignores global configuration variables, environments
809variables, command line arguments (:c:member:`PyConfig.argv` is not parsed)
810and user site directory. The C standard streams (ex: ``stdout``) and the
811LC_CTYPE locale are left unchanged. Signal handlers are not installed.
812
813Configuration files are still used with this configuration. Set the
814:ref:`Path Configuration <init-path-config>` ("output fields") to ignore these
815configuration files and avoid the function computing the default path
816configuration.
817
818
819.. _init-python-config:
820
821Python Configuration
822--------------------
823
824:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
825functions create a configuration to build a customized Python which behaves as
826the regular Python.
827
828Environments variables and command line arguments are used to configure
829Python, whereas global configuration variables are ignored.
830
831This function enables C locale coercion (:pep:`538`) and UTF-8 Mode
832(:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and
833:envvar:`PYTHONCOERCECLOCALE` environment variables.
834
835Example of customized Python always running in isolated mode::
836
837    int main(int argc, char **argv)
838    {
839        PyStatus status;
840
841        PyConfig config;
842        PyConfig_InitPythonConfig(&config);
843        config.isolated = 1;
844
845        /* Decode command line arguments.
846           Implicitly preinitialize Python (in isolated mode). */
847        status = PyConfig_SetBytesArgv(&config, argc, argv);
848        if (PyStatus_Exception(status)) {
849            goto fail;
850        }
851
852        status = Py_InitializeFromConfig(&config);
853        if (PyStatus_Exception(status)) {
854            goto fail;
855        }
856        PyConfig_Clear(&config);
857
858        return Py_RunMain();
859
860    fail:
861        PyConfig_Clear(&config);
862        if (PyStatus_IsExit(status)) {
863            return status.exitcode;
864        }
865        /* Display the error message and exit the process with
866           non-zero exit code */
867        Py_ExitStatusException(status);
868    }
869
870
871.. _init-path-config:
872
873Path Configuration
874------------------
875
876:c:type:`PyConfig` contains multiple fields for the path configuration:
877
878* Path configuration inputs:
879
880  * :c:member:`PyConfig.home`
881  * :c:member:`PyConfig.pathconfig_warnings`
882  * :c:member:`PyConfig.program_name`
883  * :c:member:`PyConfig.pythonpath_env`
884  * current working directory: to get absolute paths
885  * ``PATH`` environment variable to get the program full path
886    (from :c:member:`PyConfig.program_name`)
887  * ``__PYVENV_LAUNCHER__`` environment variable
888  * (Windows only) Application paths in the registry under
889    "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and
890    HKEY_LOCAL_MACHINE (where X.Y is the Python version).
891
892* Path configuration output fields:
893
894  * :c:member:`PyConfig.base_exec_prefix`
895  * :c:member:`PyConfig.base_executable`
896  * :c:member:`PyConfig.base_prefix`
897  * :c:member:`PyConfig.exec_prefix`
898  * :c:member:`PyConfig.executable`
899  * :c:member:`PyConfig.module_search_paths_set`,
900    :c:member:`PyConfig.module_search_paths`
901  * :c:member:`PyConfig.prefix`
902
903If at least one "output field" is not set, Python calculates the path
904configuration to fill unset fields. If
905:c:member:`~PyConfig.module_search_paths_set` is equal to 0,
906:c:member:`~PyConfig.module_search_paths` is overridden and
907:c:member:`~PyConfig.module_search_paths_set` is set to 1.
908
909It is possible to completely ignore the function calculating the default
910path configuration by setting explicitly all path configuration output
911fields listed above. A string is considered as set even if it is non-empty.
912``module_search_paths`` is considered as set if
913``module_search_paths_set`` is set to 1. In this case, path
914configuration input fields are ignored as well.
915
916Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when
917calculating the path configuration (Unix only, Windows does not log any warning).
918
919If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix`
920fields are not set, they inherit their value from :c:member:`~PyConfig.prefix`
921and :c:member:`~PyConfig.exec_prefix` respectively.
922
923:c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`:
924
925* If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a
926  ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to
927  :data:`sys.path`.
928* If :c:member:`~PyConfig.isolated` is zero:
929
930  * If :c:member:`~PyConfig.run_module` is set, prepend the current directory
931    to :data:`sys.path`. Do nothing if the current directory cannot be read.
932  * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the
933    filename to :data:`sys.path`.
934  * Otherwise, prepend an empty string to :data:`sys.path`.
935
936If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be
937modified by the :mod:`site` module. If
938:c:member:`~PyConfig.user_site_directory` is non-zero and the user's
939site-package directory exists, the :mod:`site` module appends the user's
940site-package directory to :data:`sys.path`.
941
942The following configuration files are used by the path configuration:
943
944* ``pyvenv.cfg``
945* ``python._pth`` (Windows only)
946* ``pybuilddir.txt`` (Unix only)
947
948The ``__PYVENV_LAUNCHER__`` environment variable is used to set
949:c:member:`PyConfig.base_executable`
950
951
952Py_RunMain()
953------------
954
955.. c:function:: int Py_RunMain(void)
956
957   Execute the command (:c:member:`PyConfig.run_command`), the script
958   (:c:member:`PyConfig.run_filename`) or the module
959   (:c:member:`PyConfig.run_module`) specified on the command line or in the
960   configuration.
961
962   By default and when if :option:`-i` option is used, run the REPL.
963
964   Finally, finalizes Python and returns an exit status that can be passed to
965   the ``exit()`` function.
966
967See :ref:`Python Configuration <init-python-config>` for an example of
968customized Python always running in isolated mode using
969:c:func:`Py_RunMain`.
970
971
972Multi-Phase Initialization Private Provisional API
973--------------------------------------------------
974
975This section is a private provisional API introducing multi-phase
976initialization, the core feature of the :pep:`432`:
977
978* "Core" initialization phase, "bare minimum Python":
979
980  * Builtin types;
981  * Builtin exceptions;
982  * Builtin and frozen modules;
983  * The :mod:`sys` module is only partially initialized
984    (ex: :data:`sys.path` doesn't exist yet).
985
986* "Main" initialization phase, Python is fully initialized:
987
988  * Install and configure :mod:`importlib`;
989  * Apply the :ref:`Path Configuration <init-path-config>`;
990  * Install signal handlers;
991  * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout`
992    and :data:`sys.path`);
993  * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`;
994  * Import the :mod:`site` module;
995  * etc.
996
997Private provisional API:
998
999* :c:member:`PyConfig._init_main`: if set to 0,
1000  :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase.
1001
1002.. c:function:: PyStatus _Py_InitializeMain(void)
1003
1004   Move to the "Main" initialization phase, finish the Python initialization.
1005
1006No module is imported during the "Core" phase and the ``importlib`` module is
1007not configured: the :ref:`Path Configuration <init-path-config>` is only
1008applied during the "Main" phase. It may allow to customize Python in Python to
1009override or tune the :ref:`Path Configuration <init-path-config>`, maybe
1010install a custom :data:`sys.meta_path` importer or an import hook, etc.
1011
1012It may become possible to calculatin the :ref:`Path Configuration
1013<init-path-config>` in Python, after the Core phase and before the Main phase,
1014which is one of the :pep:`432` motivation.
1015
1016The "Core" phase is not properly defined: what should be and what should
1017not be available at this phase is not specified yet. The API is marked
1018as private and provisional: the API can be modified or even be removed
1019anytime until a proper public API is designed.
1020
1021Example running Python code between "Core" and "Main" initialization
1022phases::
1023
1024    void init_python(void)
1025    {
1026        PyStatus status;
1027
1028        PyConfig config;
1029        PyConfig_InitPythonConfig(&config);
1030        config._init_main = 0;
1031
1032        /* ... customize 'config' configuration ... */
1033
1034        status = Py_InitializeFromConfig(&config);
1035        PyConfig_Clear(&config);
1036        if (PyStatus_Exception(status)) {
1037            Py_ExitStatusException(status);
1038        }
1039
1040        /* Use sys.stderr because sys.stdout is only created
1041           by _Py_InitializeMain() */
1042        int res = PyRun_SimpleString(
1043            "import sys; "
1044            "print('Run Python code before _Py_InitializeMain', "
1045                   "file=sys.stderr)");
1046        if (res < 0) {
1047            exit(1);
1048        }
1049
1050        /* ... put more configuration code here ... */
1051
1052        status = _Py_InitializeMain();
1053        if (PyStatus_Exception(status)) {
1054            Py_ExitStatusException(status);
1055        }
1056    }
1057