blob: db7c1f43765785faf5846e995cde79eb219b099a [file] [log] [blame]
Victor Stinner331a6a52019-05-27 16:39:22 +02001.. highlight:: c
2
3.. _init-config:
4
5***********************************
6Python Initialization Configuration
7***********************************
8
9.. versionadded:: 3.8
10
Victor Stinnerdc42af82020-11-05 18:58:07 +010011Python 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.
Victor Stinner331a6a52019-05-27 16:39:22 +020014
Victor Stinnerdc42af82020-11-05 18:58:07 +010015There are two kinds of configuration:
Victor Stinner331a6a52019-05-27 16:39:22 +020016
Victor Stinnerdc42af82020-11-05 18:58:07 +010017* 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.
Victor Stinner331a6a52019-05-27 16:39:22 +020021
Victor Stinnerdc42af82020-11-05 18:58:07 +010022* 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 registred.
Victor Stinner331a6a52019-05-27 16:39:22 +020026
Victor Stinnerace3f9a2020-11-10 21:10:22 +010027The :c:func:`Py_RunMain` function can be used to write a customized Python
28program.
29
Victor Stinner1beb7c32019-08-23 17:59:12 +010030See also :ref:`Initialization, Finalization, and Threads <initialization>`.
31
Victor Stinner331a6a52019-05-27 16:39:22 +020032.. seealso::
33 :pep:`587` "Python Initialization Configuration".
34
Victor Stinnerace3f9a2020-11-10 21:10:22 +010035
Victor Stinnerdc42af82020-11-05 18:58:07 +010036Example
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
Victor Stinner331a6a52019-05-27 16:39:22 +020074
75PyWideStringList
Victor Stinnerdc42af82020-11-05 18:58:07 +010076================
Victor Stinner331a6a52019-05-27 16:39:22 +020077
78.. c:type:: PyWideStringList
79
80 List of ``wchar_t*`` strings.
81
Serhiy Storchakae835b312019-10-30 21:37:16 +020082 If *length* is non-zero, *items* must be non-``NULL`` and all strings must be
83 non-``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +020084
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
Victor Stinner3842f292019-08-23 16:57:54 +010095 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.
Victor Stinner331a6a52019-05-27 16:39:22 +0200101
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
Victor Stinnerdc42af82020-11-05 18:58:07 +0100115========
Victor Stinner331a6a52019-05-27 16:39:22 +0200116
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
Victor Stinner048a3562020-11-05 00:45:56 +0100148 *err_msg* must not be ``NULL``.
149
Victor Stinner331a6a52019-05-27 16:39:22 +0200150 .. 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
Victor Stinnerdc42af82020-11-05 18:58:07 +0100207===========
Victor Stinner331a6a52019-05-27 16:39:22 +0200208
209.. c:type:: PyPreConfig
210
Victor Stinner4b9aad42020-11-02 16:49:54 +0100211 Structure used to preinitialize Python.
Victor Stinner331a6a52019-05-27 16:39:22 +0200212
213 Function to initialize a preconfiguration:
214
tomerv741008a2020-07-01 12:32:54 +0300215 .. c:function:: void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200216
217 Initialize the preconfiguration with :ref:`Python Configuration
218 <init-python-config>`.
219
tomerv741008a2020-07-01 12:32:54 +0300220 .. c:function:: void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200221
222 Initialize the preconfiguration with :ref:`Isolated Configuration
223 <init-isolated-conf>`.
224
225 Structure fields:
226
227 .. c:member:: int allocator
228
Victor Stinner4b9aad42020-11-02 16:49:54 +0100229 Name of the Python memory allocators:
Victor Stinner331a6a52019-05-27 16:39:22 +0200230
231 * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators
232 (use defaults)
233 * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators
234 * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with
235 debug hooks
236 * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()``
237 * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of
238 ``malloc()`` with debug hooks
239 * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory
240 allocator <pymalloc>`
241 * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc
242 memory allocator <pymalloc>` with debug hooks
243
244 ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG``
245 are not supported if Python is configured using ``--without-pymalloc``
246
247 See :ref:`Memory Management <memory>`.
248
Victor Stinner4b9aad42020-11-02 16:49:54 +0100249 Default: ``PYMEM_ALLOCATOR_NOT_SET``.
250
Victor Stinner331a6a52019-05-27 16:39:22 +0200251 .. c:member:: int configure_locale
252
Victor Stinner4b9aad42020-11-02 16:49:54 +0100253 Set the LC_CTYPE locale to the user preferred locale?
254
255 If equals to 0, set :c:member:`~PyPreConfig.coerce_c_locale` and
256 :c:member:`~PyPreConfig.coerce_c_locale_warn` members to 0.
257
258 See the :term:`locale encoding`.
259
260 Default: ``1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200261
262 .. c:member:: int coerce_c_locale
263
Victor Stinner4b9aad42020-11-02 16:49:54 +0100264 If equals to 2, coerce the C locale.
265
266 If equals to 1, read the LC_CTYPE locale to decide if it should be
267 coerced.
268
269 See the :term:`locale encoding`.
270
271 Default: ``-1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200272
273 .. c:member:: int coerce_c_locale_warn
Victor Stinner88feaec2019-09-26 03:15:07 +0200274
Victor Stinner331a6a52019-05-27 16:39:22 +0200275 If non-zero, emit a warning if the C locale is coerced.
276
Victor Stinner4b9aad42020-11-02 16:49:54 +0100277 Default: ``-1`` in Python config, ``0`` in isolated config.
278
Victor Stinner331a6a52019-05-27 16:39:22 +0200279 .. c:member:: int dev_mode
280
Victor Stinner4b9aad42020-11-02 16:49:54 +0100281 If non-zero, enables the :ref:`Python Development Mode <devmode>`:
282 see :c:member:`PyConfig.dev_mode`.
283
284 Default: ``-1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200285
286 .. c:member:: int isolated
287
Victor Stinner4b9aad42020-11-02 16:49:54 +0100288 Isolated mode: see :c:member:`PyConfig.isolated`.
289
290 Default: ``0`` in Python mode, ``1`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200291
Victor Stinnere662c392020-11-01 23:07:23 +0100292 .. c:member:: int legacy_windows_fs_encoding
Victor Stinner331a6a52019-05-27 16:39:22 +0200293
Victor Stinnere662c392020-11-01 23:07:23 +0100294 If non-zero:
295
296 * Set :c:member:`PyPreConfig.utf8_mode` to ``0``,
297 * Set :c:member:`PyConfig.filesystem_encoding` to ``"mbcs"``,
298 * Set :c:member:`PyConfig.filesystem_errors` to ``"replace"``.
299
300 Initialized the from :envvar:`PYTHONLEGACYWINDOWSFSENCODING` environment
301 variable value.
Victor Stinner331a6a52019-05-27 16:39:22 +0200302
303 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
304 Windows specific code.
305
Victor Stinner4b9aad42020-11-02 16:49:54 +0100306 Default: ``0``.
307
Victor Stinner331a6a52019-05-27 16:39:22 +0200308 .. c:member:: int parse_argv
309
310 If non-zero, :c:func:`Py_PreInitializeFromArgs` and
311 :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the
312 same way the regular Python parses command line arguments: see
313 :ref:`Command Line Arguments <using-on-cmdline>`.
314
Victor Stinner4b9aad42020-11-02 16:49:54 +0100315 Default: ``1`` in Python config, ``0`` in isolated config.
316
Victor Stinner331a6a52019-05-27 16:39:22 +0200317 .. c:member:: int use_environment
318
Victor Stinner4b9aad42020-11-02 16:49:54 +0100319 Use :ref:`environment variables <using-on-envvars>`? See
320 :c:member:`PyConfig.use_environment`.
321
322 Default: ``1`` in Python config and ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200323
324 .. c:member:: int utf8_mode
325
Victor Stinner4b9aad42020-11-02 16:49:54 +0100326 If non-zero, enable the :ref:`Python UTF-8 Mode <utf8-mode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200327
Victor Stinner4b9aad42020-11-02 16:49:54 +0100328 Set by the :option:`-X utf8 <-X>` command line option and the
329 :envvar:`PYTHONUTF8` environment variable.
330
331 Default: ``-1`` in Python config and ``0`` in isolated config.
332
333
334.. _c-preinit:
335
336Preinitialize Python with PyPreConfig
Victor Stinnerdc42af82020-11-05 18:58:07 +0100337=====================================
Victor Stinner4b9aad42020-11-02 16:49:54 +0100338
339The preinitialization of Python:
340
341* Set the Python memory allocators (:c:member:`PyPreConfig.allocator`)
342* Configure the LC_CTYPE locale (:term:`locale encoding`)
343* Set the :ref:`Python UTF-8 Mode <utf8-mode>`
344 (:c:member:`PyPreConfig.utf8_mode`)
Victor Stinner331a6a52019-05-27 16:39:22 +0200345
Victor Stinnerdc42af82020-11-05 18:58:07 +0100346The current preconfiguration (``PyPreConfig`` type) is stored in
347``_PyRuntime.preconfig``.
348
Victor Stinner331a6a52019-05-27 16:39:22 +0200349Functions to preinitialize Python:
350
351.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
352
353 Preinitialize Python from *preconfig* preconfiguration.
354
Victor Stinnerdc42af82020-11-05 18:58:07 +0100355 *preconfig* must not be ``NULL``.
356
Victor Stinner331a6a52019-05-27 16:39:22 +0200357.. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv)
358
Victor Stinner4b9aad42020-11-02 16:49:54 +0100359 Preinitialize Python from *preconfig* preconfiguration.
360
361 Parse *argv* command line arguments (bytes strings) if
362 :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero.
Victor Stinner331a6a52019-05-27 16:39:22 +0200363
Victor Stinnerdc42af82020-11-05 18:58:07 +0100364 *preconfig* must not be ``NULL``.
365
Victor Stinner331a6a52019-05-27 16:39:22 +0200366.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)
367
Victor Stinner4b9aad42020-11-02 16:49:54 +0100368 Preinitialize Python from *preconfig* preconfiguration.
369
370 Parse *argv* command line arguments (wide strings) if
371 :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero.
Victor Stinner331a6a52019-05-27 16:39:22 +0200372
Victor Stinnerdc42af82020-11-05 18:58:07 +0100373 *preconfig* must not be ``NULL``.
374
Victor Stinner331a6a52019-05-27 16:39:22 +0200375The caller is responsible to handle exceptions (error or exit) using
376:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
377
378For :ref:`Python Configuration <init-python-config>`
379(:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with
380command line arguments, the command line arguments must also be passed to
381preinitialize Python, since they have an effect on the pre-configuration
Victor Stinner88feaec2019-09-26 03:15:07 +0200382like encodings. For example, the :option:`-X utf8 <-X>` command line option
Victor Stinner4b9aad42020-11-02 16:49:54 +0100383enables the :ref:`Python UTF-8 Mode <utf8-mode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200384
385``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and
386before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator.
387It can be called before :c:func:`Py_PreInitialize` if
388:c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``.
389
390Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be
Victor Stinner4b9aad42020-11-02 16:49:54 +0100391used before the Python preinitialization, whereas calling directly ``malloc()``
392and ``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called
393before the Python preinitialization.
Victor Stinner331a6a52019-05-27 16:39:22 +0200394
Victor Stinner4b9aad42020-11-02 16:49:54 +0100395Example using the preinitialization to enable
396the :ref:`Python UTF-8 Mode <utf8-mode>`::
Victor Stinner331a6a52019-05-27 16:39:22 +0200397
Victor Stinner441b10c2019-09-28 04:28:35 +0200398 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +0200399 PyPreConfig preconfig;
Victor Stinner3c30a762019-10-01 10:56:37 +0200400 PyPreConfig_InitPythonConfig(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200401
402 preconfig.utf8_mode = 1;
403
Victor Stinner441b10c2019-09-28 04:28:35 +0200404 status = Py_PreInitialize(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200405 if (PyStatus_Exception(status)) {
406 Py_ExitStatusException(status);
407 }
408
Victor Stinner4b9aad42020-11-02 16:49:54 +0100409 /* at this point, Python speaks UTF-8 */
Victor Stinner331a6a52019-05-27 16:39:22 +0200410
411 Py_Initialize();
412 /* ... use Python API here ... */
413 Py_Finalize();
414
415
416PyConfig
Victor Stinnerdc42af82020-11-05 18:58:07 +0100417========
Victor Stinner331a6a52019-05-27 16:39:22 +0200418
419.. c:type:: PyConfig
420
421 Structure containing most parameters to configure Python.
422
Victor Stinner4b9aad42020-11-02 16:49:54 +0100423 When done, the :c:func:`PyConfig_Clear` function must be used to release the
424 configuration memory.
425
Victor Stinner331a6a52019-05-27 16:39:22 +0200426 Structure methods:
427
Victor Stinner8462a492019-10-01 12:06:16 +0200428 .. c:function:: void PyConfig_InitPythonConfig(PyConfig *config)
Victor Stinner331a6a52019-05-27 16:39:22 +0200429
Victor Stinner4b9aad42020-11-02 16:49:54 +0100430 Initialize configuration with the :ref:`Python Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200431 <init-python-config>`.
432
Victor Stinner8462a492019-10-01 12:06:16 +0200433 .. c:function:: void PyConfig_InitIsolatedConfig(PyConfig *config)
Victor Stinner331a6a52019-05-27 16:39:22 +0200434
Victor Stinner4b9aad42020-11-02 16:49:54 +0100435 Initialize configuration with the :ref:`Isolated Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200436 <init-isolated-conf>`.
437
438 .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str)
439
440 Copy the wide character string *str* into ``*config_str``.
441
Victor Stinner4b9aad42020-11-02 16:49:54 +0100442 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200443
444 .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str)
445
Victor Stinner4b9aad42020-11-02 16:49:54 +0100446 Decode *str* using :c:func:`Py_DecodeLocale` and set the result into
447 ``*config_str``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200448
Victor Stinner4b9aad42020-11-02 16:49:54 +0100449 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200450
451 .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv)
452
Victor Stinner4b9aad42020-11-02 16:49:54 +0100453 Set command line arguments (:c:member:`~PyConfig.argv` member of
454 *config*) from the *argv* list of wide character strings.
Victor Stinner331a6a52019-05-27 16:39:22 +0200455
Victor Stinner4b9aad42020-11-02 16:49:54 +0100456 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200457
458 .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv)
459
Victor Stinner4b9aad42020-11-02 16:49:54 +0100460 Set command line arguments (:c:member:`~PyConfig.argv` member of
461 *config*) from the *argv* list of bytes strings. Decode bytes using
462 :c:func:`Py_DecodeLocale`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200463
Victor Stinner4b9aad42020-11-02 16:49:54 +0100464 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200465
Victor Stinner36242fd2019-07-01 19:13:50 +0200466 .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items)
467
468 Set the list of wide strings *list* to *length* and *items*.
469
Victor Stinner4b9aad42020-11-02 16:49:54 +0100470 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner36242fd2019-07-01 19:13:50 +0200471
Victor Stinner331a6a52019-05-27 16:39:22 +0200472 .. c:function:: PyStatus PyConfig_Read(PyConfig *config)
473
474 Read all Python configuration.
475
476 Fields which are already initialized are left unchanged.
477
Victor Stinnerdc42af82020-11-05 18:58:07 +0100478 The :c:func:`PyConfig_Read` function only parses
479 :c:member:`PyConfig.argv` arguments once: :c:member:`PyConfig.parse_argv`
480 is set to ``2`` after arguments are parsed. Since Python arguments are
481 strippped from :c:member:`PyConfig.argv`, parsing arguments twice would
482 parse the application options as Python options.
483
Victor Stinner4b9aad42020-11-02 16:49:54 +0100484 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200485
Victor Stinnerdc42af82020-11-05 18:58:07 +0100486 .. versionchanged:: 3.10
487 The :c:member:`PyConfig.argv` arguments are now only parsed once,
488 :c:member:`PyConfig.parse_argv` is set to ``2`` after arguments are
489 parsed, and arguments are only parsed if
490 :c:member:`PyConfig.parse_argv` equals ``1``.
491
Victor Stinner331a6a52019-05-27 16:39:22 +0200492 .. c:function:: void PyConfig_Clear(PyConfig *config)
493
494 Release configuration memory.
495
Victor Stinner4b9aad42020-11-02 16:49:54 +0100496 Most ``PyConfig`` methods :ref:`preinitialize Python <c-preinit>` if needed.
497 In that case, the Python preinitialization configuration
498 (:c:type:`PyPreConfig`) in based on the :c:type:`PyConfig`. If configuration
499 fields which are in common with :c:type:`PyPreConfig` are tuned, they must
500 be set before calling a :c:type:`PyConfig` method:
Victor Stinner331a6a52019-05-27 16:39:22 +0200501
Victor Stinner4b9aad42020-11-02 16:49:54 +0100502 * :c:member:`PyConfig.dev_mode`
503 * :c:member:`PyConfig.isolated`
504 * :c:member:`PyConfig.parse_argv`
505 * :c:member:`PyConfig.use_environment`
Victor Stinner331a6a52019-05-27 16:39:22 +0200506
507 Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv`
Victor Stinner4b9aad42020-11-02 16:49:54 +0100508 is used, this method must be called before other methods, since the
Victor Stinner331a6a52019-05-27 16:39:22 +0200509 preinitialization configuration depends on command line arguments (if
510 :c:member:`parse_argv` is non-zero).
511
512 The caller of these methods is responsible to handle exceptions (error or
513 exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``.
514
515 Structure fields:
516
517 .. c:member:: PyWideStringList argv
518
Victor Stinner4b9aad42020-11-02 16:49:54 +0100519 Command line arguments: :data:`sys.argv`.
520
521 Set :c:member:`~PyConfig.parse_argv` to ``1`` to parse
522 :c:member:`~PyConfig.argv` the same way the regular Python parses Python
523 command line arguments and then to strip Python arguments from
524 :c:member:`~PyConfig.argv`.
525
526 If :c:member:`~PyConfig.argv` is empty, an empty string is added to
527 ensure that :data:`sys.argv` always exists and is never empty.
528
529 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200530
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200531 See also the :c:member:`~PyConfig.orig_argv` member.
532
Victor Stinner331a6a52019-05-27 16:39:22 +0200533 .. c:member:: wchar_t* base_exec_prefix
534
535 :data:`sys.base_exec_prefix`.
536
Victor Stinner4b9aad42020-11-02 16:49:54 +0100537 Default: ``NULL``.
538
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100539 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Victor Stinner4b9aad42020-11-02 16:49:54 +0100540
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200541 .. c:member:: wchar_t* base_executable
542
Victor Stinner4b9aad42020-11-02 16:49:54 +0100543 Python base executable: :data:`sys._base_executable`.
544
545 Set by the :envvar:`__PYVENV_LAUNCHER__` environment variable.
546
547 Set from :c:member:`PyConfig.executable` if ``NULL``.
548
549 Default: ``NULL``.
550
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100551 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200552
Victor Stinner331a6a52019-05-27 16:39:22 +0200553 .. c:member:: wchar_t* base_prefix
554
555 :data:`sys.base_prefix`.
556
Victor Stinner4b9aad42020-11-02 16:49:54 +0100557 Default: ``NULL``.
Sandro Mani8f023a22020-06-08 17:28:11 +0200558
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100559 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Sandro Mani8f023a22020-06-08 17:28:11 +0200560
Victor Stinner331a6a52019-05-27 16:39:22 +0200561 .. c:member:: int buffered_stdio
562
Victor Stinner4b9aad42020-11-02 16:49:54 +0100563 If equals to 0 and :c:member:`~PyConfig.configure_c_stdio` is non-zero,
564 disable buffering on the C streams stdout and stderr.
565
566 Set to 0 by the :option:`-u` command line option and the
567 :envvar:`PYTHONUNBUFFERED` environment variable.
Victor Stinner331a6a52019-05-27 16:39:22 +0200568
569 stdin is always opened in buffered mode.
570
Victor Stinner4b9aad42020-11-02 16:49:54 +0100571 Default: ``1``.
572
Victor Stinner331a6a52019-05-27 16:39:22 +0200573 .. c:member:: int bytes_warning
574
575 If equals to 1, issue a warning when comparing :class:`bytes` or
576 :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with
Victor Stinner4b9aad42020-11-02 16:49:54 +0100577 :class:`int`.
578
579 If equal or greater to 2, raise a :exc:`BytesWarning` exception in these
580 cases.
581
582 Incremented by the :option:`-b` command line option.
583
584 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200585
586 .. c:member:: wchar_t* check_hash_pycs_mode
587
Victor Stinner4b9aad42020-11-02 16:49:54 +0100588 Control the validation behavior of hash-based ``.pyc`` files:
589 value of the :option:`--check-hash-based-pycs` command line option.
Victor Stinner331a6a52019-05-27 16:39:22 +0200590
Victor Stinner4b9aad42020-11-02 16:49:54 +0100591 Valid values:
Victor Stinner331a6a52019-05-27 16:39:22 +0200592
Victor Stinner4b9aad42020-11-02 16:49:54 +0100593 - ``L"always"``: Hash the source file for invalidation regardless of
594 value of the 'check_source' flag.
595 - ``L"never"``: Assume that hash-based pycs always are valid.
596 - ``L"default"``: The 'check_source' flag in hash-based pycs
597 determines invalidation.
598
599 Default: ``L"default"``.
600
601 See also :pep:`552` "Deterministic pycs".
Victor Stinner331a6a52019-05-27 16:39:22 +0200602
603 .. c:member:: int configure_c_stdio
604
Victor Stinner4b9aad42020-11-02 16:49:54 +0100605 If non-zero, configure C standard streams:
606
607 * On Windows, set the binary mode (``O_BINARY``) on stdin, stdout and
608 stderr.
609 * If :c:member:`~PyConfig.buffered_stdio` equals zero, disable buffering
610 of stdin, stdout and stderr streams.
611 * If :c:member:`~PyConfig.interactive` is non-zero, enable stream
612 buffering on stdin and stdout (only stdout on Windows).
613
614 Default: ``1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200615
616 .. c:member:: int dev_mode
617
Victor Stinnerb9783d22020-01-24 10:22:18 +0100618 If non-zero, enable the :ref:`Python Development Mode <devmode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200619
Victor Stinner4b9aad42020-11-02 16:49:54 +0100620 Default: ``-1`` in Python mode, ``0`` in isolated mode.
621
Victor Stinner331a6a52019-05-27 16:39:22 +0200622 .. c:member:: int dump_refs
623
Victor Stinner4b9aad42020-11-02 16:49:54 +0100624 Dump Python refererences?
625
Victor Stinner331a6a52019-05-27 16:39:22 +0200626 If non-zero, dump all objects which are still alive at exit.
627
Victor Stinner4b9aad42020-11-02 16:49:54 +0100628 Set to ``1`` by the :envvar:`PYTHONDUMPREFS` environment variable.
629
630 Need a special build of Python with the ``Py_TRACE_REFS`` macro defined.
631
632 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200633
634 .. c:member:: wchar_t* exec_prefix
635
Victor Stinner4b9aad42020-11-02 16:49:54 +0100636 The site-specific directory prefix where the platform-dependent Python
637 files are installed: :data:`sys.exec_prefix`.
638
639 Default: ``NULL``.
640
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100641 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200642
643 .. c:member:: wchar_t* executable
644
Victor Stinner4b9aad42020-11-02 16:49:54 +0100645 The absolute path of the executable binary for the Python interpreter:
Victor Stinner331a6a52019-05-27 16:39:22 +0200646 :data:`sys.executable`.
647
Victor Stinner4b9aad42020-11-02 16:49:54 +0100648 Default: ``NULL``.
649
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100650 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Victor Stinner4b9aad42020-11-02 16:49:54 +0100651
Victor Stinner331a6a52019-05-27 16:39:22 +0200652 .. c:member:: int faulthandler
653
Victor Stinner4b9aad42020-11-02 16:49:54 +0100654 Enable faulthandler?
655
Victor Stinner88feaec2019-09-26 03:15:07 +0200656 If non-zero, call :func:`faulthandler.enable` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200657
Victor Stinner4b9aad42020-11-02 16:49:54 +0100658 Set to ``1`` by :option:`-X faulthandler <-X>` and the
659 :envvar:`PYTHONFAULTHANDLER` environment variable.
660
661 Default: ``-1`` in Python mode, ``0`` in isolated mode.
662
Victor Stinner331a6a52019-05-27 16:39:22 +0200663 .. c:member:: wchar_t* filesystem_encoding
664
Victor Stinner4b9aad42020-11-02 16:49:54 +0100665 :term:`Filesystem encoding <filesystem encoding and error handler>`:
666 :func:`sys.getfilesystemencoding`.
Victor Stinnere662c392020-11-01 23:07:23 +0100667
668 On macOS, Android and VxWorks: use ``"utf-8"`` by default.
669
670 On Windows: use ``"utf-8"`` by default, or ``"mbcs"`` if
671 :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of
672 :c:type:`PyPreConfig` is non-zero.
673
674 Default encoding on other platforms:
675
676 * ``"utf-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero.
677 * ``"ascii"`` if Python detects that ``nl_langinfo(CODESET)`` announces
678 the ASCII encoding (or Roman8 encoding on HP-UX), whereas the
679 ``mbstowcs()`` function decodes from a different encoding (usually
680 Latin1).
681 * ``"utf-8"`` if ``nl_langinfo(CODESET)`` returns an empty string.
Victor Stinner4b9aad42020-11-02 16:49:54 +0100682 * Otherwise, use the :term:`locale encoding`:
Victor Stinnere662c392020-11-01 23:07:23 +0100683 ``nl_langinfo(CODESET)`` result.
684
685 At Python statup, the encoding name is normalized to the Python codec
686 name. For example, ``"ANSI_X3.4-1968"`` is replaced with ``"ascii"``.
687
688 See also the :c:member:`~PyConfig.filesystem_errors` member.
Victor Stinner331a6a52019-05-27 16:39:22 +0200689
690 .. c:member:: wchar_t* filesystem_errors
691
Victor Stinner4b9aad42020-11-02 16:49:54 +0100692 :term:`Filesystem error handler <filesystem encoding and error handler>`:
693 :func:`sys.getfilesystemencodeerrors`.
Victor Stinnere662c392020-11-01 23:07:23 +0100694
695 On Windows: use ``"surrogatepass"`` by default, or ``"replace"`` if
696 :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of
697 :c:type:`PyPreConfig` is non-zero.
698
699 On other platforms: use ``"surrogateescape"`` by default.
700
701 Supported error handlers:
702
703 * ``"strict"``
704 * ``"surrogateescape"``
705 * ``"surrogatepass"`` (only supported with the UTF-8 encoding)
706
707 See also the :c:member:`~PyConfig.filesystem_encoding` member.
Victor Stinner331a6a52019-05-27 16:39:22 +0200708
709 .. c:member:: unsigned long hash_seed
710 .. c:member:: int use_hash_seed
711
712 Randomized hash function seed.
713
714 If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly
Victor Stinner4b9aad42020-11-02 16:49:54 +0100715 at Python startup, and :c:member:`~PyConfig.hash_seed` is ignored.
716
717 Set by the :envvar:`PYTHONHASHSEED` environment variable.
718
719 Default *use_hash_seed* value: ``-1`` in Python mode, ``0`` in isolated
720 mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200721
722 .. c:member:: wchar_t* home
723
724 Python home directory.
725
Victor Stinner4b9aad42020-11-02 16:49:54 +0100726 If :c:func:`Py_SetPythonHome` has been called, use its argument if it is
727 not ``NULL``.
728
729 Set by the :envvar:`PYTHONHOME` environment variable.
730
731 Default: ``NULL``.
732
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100733 Part of the :ref:`Python Path Configuration <init-path-config>` input.
Victor Stinner88feaec2019-09-26 03:15:07 +0200734
Victor Stinner331a6a52019-05-27 16:39:22 +0200735 .. c:member:: int import_time
736
737 If non-zero, profile import time.
738
Victor Stinner4b9aad42020-11-02 16:49:54 +0100739 Set the ``1`` by the :option:`-X importtime <-X>` option and the
740 :envvar:`PYTHONPROFILEIMPORTTIME` environment variable.
741
742 Default: ``0``.
743
Victor Stinner331a6a52019-05-27 16:39:22 +0200744 .. c:member:: int inspect
745
746 Enter interactive mode after executing a script or a command.
747
Victor Stinner4b9aad42020-11-02 16:49:54 +0100748 If greater than 0, enable inspect: when a script is passed as first
749 argument or the -c option is used, enter interactive mode after executing
750 the script or the command, even when :data:`sys.stdin` does not appear to
751 be a terminal.
752
753 Incremented by the :option:`-i` command line option. Set to ``1`` if the
754 :envvar:`PYTHONINSPECT` environment variable is non-empty.
755
756 Default: ``0``.
757
Victor Stinner331a6a52019-05-27 16:39:22 +0200758 .. c:member:: int install_signal_handlers
759
Victor Stinner4b9aad42020-11-02 16:49:54 +0100760 Install Python signal handlers?
761
762 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200763
764 .. c:member:: int interactive
765
Victor Stinner4b9aad42020-11-02 16:49:54 +0100766 If greater than 0, enable the interactive mode (REPL).
767
768 Incremented by the :option:`-i` command line option.
769
770 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200771
772 .. c:member:: int isolated
773
774 If greater than 0, enable isolated mode:
775
776 * :data:`sys.path` contains neither the script's directory (computed from
777 ``argv[0]`` or the current directory) nor the user's site-packages
778 directory.
779 * Python REPL doesn't import :mod:`readline` nor enable default readline
780 configuration on interactive prompts.
781 * Set :c:member:`~PyConfig.use_environment` and
782 :c:member:`~PyConfig.user_site_directory` to 0.
783
Victor Stinner4b9aad42020-11-02 16:49:54 +0100784 Default: ``0`` in Python mode, ``1`` in isolated mode.
785
786 See also :c:member:`PyPreConfig.isolated`.
787
Victor Stinner331a6a52019-05-27 16:39:22 +0200788 .. c:member:: int legacy_windows_stdio
789
790 If non-zero, use :class:`io.FileIO` instead of
791 :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout`
792 and :data:`sys.stderr`.
793
Victor Stinner4b9aad42020-11-02 16:49:54 +0100794 Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSSTDIO` environment
795 variable is set to a non-empty string.
796
Victor Stinner331a6a52019-05-27 16:39:22 +0200797 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
798 Windows specific code.
799
Victor Stinner4b9aad42020-11-02 16:49:54 +0100800 Default: ``0``.
801
802 See also the :pep:`528` (Change Windows console encoding to UTF-8).
803
Victor Stinner331a6a52019-05-27 16:39:22 +0200804 .. c:member:: int malloc_stats
805
806 If non-zero, dump statistics on :ref:`Python pymalloc memory allocator
807 <pymalloc>` at exit.
808
Victor Stinner4b9aad42020-11-02 16:49:54 +0100809 Set to ``1`` by the :envvar:`PYTHONMALLOCSTATS` environment variable.
810
Victor Stinner331a6a52019-05-27 16:39:22 +0200811 The option is ignored if Python is built using ``--without-pymalloc``.
812
Victor Stinner4b9aad42020-11-02 16:49:54 +0100813 Default: ``0``.
814
815 .. c:member:: wchar_t* platlibdir
816
817 Platform library directory name: :data:`sys.platlibdir`.
818
819 Set by the :envvar:`PYTHONPLATLIBDIR` environment variable.
820
821 Default: value of the ``PLATLIBDIR`` macro which is set at configure time
822 by ``--with-platlibdir`` (default: ``"lib"``).
823
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100824 Part of the :ref:`Python Path Configuration <init-path-config>` input.
Victor Stinner4b9aad42020-11-02 16:49:54 +0100825
826 .. versionadded:: 3.9
827
Victor Stinner331a6a52019-05-27 16:39:22 +0200828 .. c:member:: wchar_t* pythonpath_env
829
Victor Stinner4b9aad42020-11-02 16:49:54 +0100830 Module search paths (:data:`sys.path`) as a string separated by ``DELIM``
Victor Stinner331a6a52019-05-27 16:39:22 +0200831 (:data:`os.path.pathsep`).
832
Victor Stinner4b9aad42020-11-02 16:49:54 +0100833 Set by the :envvar:`PYTHONPATH` environment variable.
834
835 Default: ``NULL``.
836
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100837 Part of the :ref:`Python Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200838
839 .. c:member:: PyWideStringList module_search_paths
840 .. c:member:: int module_search_paths_set
841
Victor Stinner4b9aad42020-11-02 16:49:54 +0100842 Module search paths: :data:`sys.path`.
843
844 If :c:member:`~PyConfig.module_search_paths_set` is equal to 0, the
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100845 function calculating the :ref:`Python Path Configuration <init-path-config>`
Victor Stinner4b9aad42020-11-02 16:49:54 +0100846 overrides the :c:member:`~PyConfig.module_search_paths` and sets
847 :c:member:`~PyConfig.module_search_paths_set` to ``1``.
848
849 Default: empty list (``module_search_paths``) and ``0``
850 (``module_search_paths_set``).
851
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100852 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200853
854 .. c:member:: int optimization_level
855
856 Compilation optimization level:
857
Victor Stinner4b9aad42020-11-02 16:49:54 +0100858 * ``0``: Peephole optimizer, set ``__debug__`` to ``True``.
859 * ``1``: Level 0, remove assertions, set ``__debug__`` to ``False``.
860 * ``2``: Level 1, strip docstrings.
861
862 Incremented by the :option:`-O` command line option. Set to the
863 :envvar:`PYTHONOPTIMIZE` environment variable value.
864
865 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200866
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200867 .. c:member:: PyWideStringList orig_argv
868
869 The list of the original command line arguments passed to the Python
Victor Stinner4b9aad42020-11-02 16:49:54 +0100870 executable: :data:`sys.orig_argv`.
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200871
872 If :c:member:`~PyConfig.orig_argv` list is empty and
873 :c:member:`~PyConfig.argv` is not a list only containing an empty
Victor Stinnerdc42af82020-11-05 18:58:07 +0100874 string, :c:func:`PyConfig_Read` copies :c:member:`~PyConfig.argv` into
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200875 :c:member:`~PyConfig.orig_argv` before modifying
876 :c:member:`~PyConfig.argv` (if :c:member:`~PyConfig.parse_argv` is
877 non-zero).
878
879 See also the :c:member:`~PyConfig.argv` member and the
880 :c:func:`Py_GetArgcArgv` function.
881
Victor Stinner4b9aad42020-11-02 16:49:54 +0100882 Default: empty list.
883
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200884 .. versionadded:: 3.10
885
Victor Stinner331a6a52019-05-27 16:39:22 +0200886 .. c:member:: int parse_argv
887
Victor Stinner4b9aad42020-11-02 16:49:54 +0100888 Parse command line arguments?
889
Victor Stinnerdc42af82020-11-05 18:58:07 +0100890 If equals to ``1``, parse :c:member:`~PyConfig.argv` the same way the regular
Victor Stinner4b9aad42020-11-02 16:49:54 +0100891 Python parses :ref:`command line arguments <using-on-cmdline>`, and strip
892 Python arguments from :c:member:`~PyConfig.argv`.
893
Victor Stinnerdc42af82020-11-05 18:58:07 +0100894 The :c:func:`PyConfig_Read` function only parses
895 :c:member:`PyConfig.argv` arguments once: :c:member:`PyConfig.parse_argv`
896 is set to ``2`` after arguments are parsed. Since Python arguments are
897 strippped from :c:member:`PyConfig.argv`, parsing arguments twice would
898 parse the application options as Python options.
899
Victor Stinner4b9aad42020-11-02 16:49:54 +0100900 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200901
Victor Stinnerdc42af82020-11-05 18:58:07 +0100902 .. versionchanged:: 3.10
903 The :c:member:`PyConfig.argv` arguments are now only parsed if
904 :c:member:`PyConfig.parse_argv` equals to ``1``.
905
Victor Stinner331a6a52019-05-27 16:39:22 +0200906 .. c:member:: int parser_debug
907
Victor Stinner4b9aad42020-11-02 16:49:54 +0100908 Parser debug mode. If greater than 0, turn on parser debugging output (for expert only, depending
Victor Stinner331a6a52019-05-27 16:39:22 +0200909 on compilation options).
910
Victor Stinner4b9aad42020-11-02 16:49:54 +0100911 Incremented by the :option:`-d` command line option. Set to the
912 :envvar:`PYTHONDEBUG` environment variable value.
913
914 Default: ``0``.
915
Victor Stinner331a6a52019-05-27 16:39:22 +0200916 .. c:member:: int pathconfig_warnings
917
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100918 On Unix, if non-zero, calculating the :ref:`Python Path Configuration
Victor Stinner4b9aad42020-11-02 16:49:54 +0100919 <init-path-config>` can log warnings into ``stderr``. If equals to 0,
920 suppress these warnings.
921
922 It has no effect on Windows.
923
924 Default: ``1`` in Python mode, ``0`` in isolated mode.
925
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100926 Part of the :ref:`Python Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200927
928 .. c:member:: wchar_t* prefix
929
Victor Stinner4b9aad42020-11-02 16:49:54 +0100930 The site-specific directory prefix where the platform independent Python
931 files are installed: :data:`sys.prefix`.
932
933 Default: ``NULL``.
934
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100935 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200936
937 .. c:member:: wchar_t* program_name
938
Victor Stinner4b9aad42020-11-02 16:49:54 +0100939 Program name used to initialize :c:member:`~PyConfig.executable` and in
940 early error messages during Python initialization.
941
942 * If :func:`Py_SetProgramName` has been called, use its argument.
943 * On macOS, use :envvar:`PYTHONEXECUTABLE` environment variable if set.
944 * If the ``WITH_NEXT_FRAMEWORK`` macro is defined, use
945 :envvar:`__PYVENV_LAUNCHER__` environment variable if set.
946 * Use ``argv[0]`` of :c:member:`~PyConfig.argv` if available and
947 non-empty.
948 * Otherwise, use ``L"python"`` on Windows, or ``L"python3"`` on other
949 platforms.
950
951 Default: ``NULL``.
952
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100953 Part of the :ref:`Python Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200954
955 .. c:member:: wchar_t* pycache_prefix
956
Victor Stinner4b9aad42020-11-02 16:49:54 +0100957 Directory where cached ``.pyc`` files are written:
958 :data:`sys.pycache_prefix`.
959
960 Set by the :option:`-X pycache_prefix=PATH <-X>` command line option and
961 the :envvar:`PYTHONPYCACHEPREFIX` environment variable.
Victor Stinner88feaec2019-09-26 03:15:07 +0200962
Serhiy Storchakae835b312019-10-30 21:37:16 +0200963 If ``NULL``, :data:`sys.pycache_prefix` is set to ``None``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200964
Victor Stinner4b9aad42020-11-02 16:49:54 +0100965 Default: ``NULL``.
966
Victor Stinner331a6a52019-05-27 16:39:22 +0200967 .. c:member:: int quiet
968
Victor Stinner4b9aad42020-11-02 16:49:54 +0100969 Quiet mode. If greater than 0, don't display the copyright and version at
970 Python startup in interactive mode.
971
972 Incremented by the :option:`-q` command line option.
973
974 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200975
976 .. c:member:: wchar_t* run_command
977
Victor Stinner4b9aad42020-11-02 16:49:54 +0100978 Value of the :option:`-c` command line option.
979
980 Used by :c:func:`Py_RunMain`.
981
982 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200983
984 .. c:member:: wchar_t* run_filename
985
Victor Stinner4b9aad42020-11-02 16:49:54 +0100986 Filename passed on the command line: trailing command line argument
987 without :option:`-c` or :option:`-m`.
988
989 For example, it is set to ``script.py`` by the ``python3 script.py arg``
990 command.
991
992 Used by :c:func:`Py_RunMain`.
993
994 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200995
996 .. c:member:: wchar_t* run_module
997
Victor Stinner4b9aad42020-11-02 16:49:54 +0100998 Value of the :option:`-m` command line option.
999
1000 Used by :c:func:`Py_RunMain`.
1001
1002 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +02001003
Victor Stinner331a6a52019-05-27 16:39:22 +02001004 .. c:member:: int show_ref_count
1005
1006 Show total reference count at exit?
1007
Victor Stinner88feaec2019-09-26 03:15:07 +02001008 Set to 1 by :option:`-X showrefcount <-X>` command line option.
1009
Victor Stinner331a6a52019-05-27 16:39:22 +02001010 Need a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
1011
Victor Stinner4b9aad42020-11-02 16:49:54 +01001012 Default: ``0``.
1013
Victor Stinner331a6a52019-05-27 16:39:22 +02001014 .. c:member:: int site_import
1015
1016 Import the :mod:`site` module at startup?
1017
Victor Stinner4b9aad42020-11-02 16:49:54 +01001018 If equal to zero, disable the import of the module site and the
1019 site-dependent manipulations of :data:`sys.path` that it entails.
1020
1021 Also disable these manipulations if the :mod:`site` module is explicitly
1022 imported later (call :func:`site.main` if you want them to be triggered).
1023
1024 Set to ``0`` by the :option:`-S` command line option.
1025
1026 :data:`sys.flags.no_site` is set to the inverted value of
1027 :c:member:`~PyConfig.site_import`.
1028
1029 Default: ``1``.
1030
Victor Stinner331a6a52019-05-27 16:39:22 +02001031 .. c:member:: int skip_source_first_line
1032
Victor Stinner4b9aad42020-11-02 16:49:54 +01001033 If non-zero, skip the first line of the :c:member:`PyConfig.run_filename`
1034 source.
1035
1036 It allows the usage of non-Unix forms of ``#!cmd``. This is intended for
1037 a DOS specific hack only.
1038
1039 Set to ``1`` by the :option:`-x` command line option.
1040
1041 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +02001042
1043 .. c:member:: wchar_t* stdio_encoding
1044 .. c:member:: wchar_t* stdio_errors
1045
1046 Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
Victor Stinner4b9aad42020-11-02 16:49:54 +01001047 :data:`sys.stderr` (but :data:`sys.stderr` always uses
1048 ``"backslashreplace"`` error handler).
1049
1050 If :c:func:`Py_SetStandardStreamEncoding` has been called, use its
1051 *error* and *errors* arguments if they are not ``NULL``.
1052
1053 Use the :envvar:`PYTHONIOENCODING` environment variable if it is
1054 non-empty.
1055
1056 Default encoding:
1057
1058 * ``"UTF-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero.
1059 * Otherwise, use the :term:`locale encoding`.
1060
1061 Default error handler:
1062
1063 * On Windows: use ``"surrogateescape"``.
1064 * ``"surrogateescape"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero,
1065 or if the LC_CTYPE locale is "C" or "POSIX".
1066 * ``"strict"`` otherwise.
Victor Stinner331a6a52019-05-27 16:39:22 +02001067
1068 .. c:member:: int tracemalloc
1069
Victor Stinner4b9aad42020-11-02 16:49:54 +01001070 Enable tracemalloc?
1071
Victor Stinner88feaec2019-09-26 03:15:07 +02001072 If non-zero, call :func:`tracemalloc.start` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +02001073
Victor Stinner4b9aad42020-11-02 16:49:54 +01001074 Set by :option:`-X tracemalloc=N <-X>` command line option and by the
1075 :envvar:`PYTHONTRACEMALLOC` environment variable.
1076
1077 Default: ``-1`` in Python mode, ``0`` in isolated mode.
1078
Victor Stinner331a6a52019-05-27 16:39:22 +02001079 .. c:member:: int use_environment
1080
Victor Stinner4b9aad42020-11-02 16:49:54 +01001081 Use :ref:`environment variables <using-on-envvars>`?
1082
1083 If equals to zero, ignore the :ref:`environment variables
1084 <using-on-envvars>`.
1085
1086 Default: ``1`` in Python config and ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +02001087
1088 .. c:member:: int user_site_directory
1089
Victor Stinner4b9aad42020-11-02 16:49:54 +01001090 If non-zero, add the user site directory to :data:`sys.path`.
1091
1092 Set to ``0`` by the :option:`-s` and :option:`-I` command line options.
1093
1094 Set to ``0`` by the :envvar:`PYTHONNOUSERSITE` environment variable.
1095
1096 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +02001097
1098 .. c:member:: int verbose
1099
Victor Stinner4b9aad42020-11-02 16:49:54 +01001100 Verbose mode. If greater than 0, print a message each time a module is
1101 imported, showing the place (filename or built-in module) from which
1102 it is loaded.
1103
1104 If greater or equal to 2, print a message for each file that is checked
1105 for when searching for a module. Also provides information on module
1106 cleanup at exit.
1107
1108 Incremented by the :option:`-v` command line option.
1109
1110 Set to the :envvar:`PYTHONVERBOSE` environment variable value.
1111
1112 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +02001113
1114 .. c:member:: PyWideStringList warnoptions
1115
Victor Stinner4b9aad42020-11-02 16:49:54 +01001116 Options of the :mod:`warnings` module to build warnings filters, lowest
1117 to highest priority: :data:`sys.warnoptions`.
Victor Stinnerfb4ae152019-09-30 01:40:17 +02001118
1119 The :mod:`warnings` module adds :data:`sys.warnoptions` in the reverse
1120 order: the last :c:member:`PyConfig.warnoptions` item becomes the first
1121 item of :data:`warnings.filters` which is checked first (highest
1122 priority).
Victor Stinner331a6a52019-05-27 16:39:22 +02001123
Victor Stinner4b9aad42020-11-02 16:49:54 +01001124 Default: empty list.
1125
Victor Stinner331a6a52019-05-27 16:39:22 +02001126 .. c:member:: int write_bytecode
1127
Victor Stinner4b9aad42020-11-02 16:49:54 +01001128 If equal to 0, Python won't try to write ``.pyc`` files on the import of
1129 source modules.
1130
1131 Set to ``0`` by the :option:`-B` command line option and the
1132 :envvar:`PYTHONDONTWRITEBYTECODE` environment variable.
Victor Stinner331a6a52019-05-27 16:39:22 +02001133
Victor Stinner88feaec2019-09-26 03:15:07 +02001134 :data:`sys.dont_write_bytecode` is initialized to the inverted value of
1135 :c:member:`~PyConfig.write_bytecode`.
1136
Victor Stinner4b9aad42020-11-02 16:49:54 +01001137 Default: ``1``.
1138
Victor Stinner331a6a52019-05-27 16:39:22 +02001139 .. c:member:: PyWideStringList xoptions
1140
Victor Stinner4b9aad42020-11-02 16:49:54 +01001141 Values of the :option:`-X` command line options: :data:`sys._xoptions`.
Victor Stinner331a6a52019-05-27 16:39:22 +02001142
Victor Stinner4b9aad42020-11-02 16:49:54 +01001143 Default: empty list.
Victor Stinner331a6a52019-05-27 16:39:22 +02001144
Victor Stinner4b9aad42020-11-02 16:49:54 +01001145If :c:member:`~PyConfig.parse_argv` is non-zero, :c:member:`~PyConfig.argv`
1146arguments are parsed the same way the regular Python parses :ref:`command line
1147arguments <using-on-cmdline>`, and Python arguments are stripped from
1148:c:member:`~PyConfig.argv`.
1149
1150The :c:member:`~PyConfig.xoptions` options are parsed to set other options: see
1151the :option:`-X` command line option.
Victor Stinner331a6a52019-05-27 16:39:22 +02001152
Victor Stinnerc6e5c112020-02-03 15:17:15 +01001153.. versionchanged:: 3.9
1154
1155 The ``show_alloc_count`` field has been removed.
1156
Victor Stinner331a6a52019-05-27 16:39:22 +02001157
1158Initialization with PyConfig
Victor Stinnerdc42af82020-11-05 18:58:07 +01001159============================
Victor Stinner331a6a52019-05-27 16:39:22 +02001160
1161Function to initialize Python:
1162
1163.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
1164
1165 Initialize Python from *config* configuration.
1166
1167The caller is responsible to handle exceptions (error or exit) using
1168:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
1169
Victor Stinner4b9aad42020-11-02 16:49:54 +01001170If :c:func:`PyImport_FrozenModules`, :c:func:`PyImport_AppendInittab` or
1171:c:func:`PyImport_ExtendInittab` are used, they must be set or called after
1172Python preinitialization and before the Python initialization.
Victor Stinner331a6a52019-05-27 16:39:22 +02001173
Victor Stinnerdc42af82020-11-05 18:58:07 +01001174The current configuration (``PyConfig`` type) is stored in
1175``PyInterpreterState.config``.
1176
Victor Stinner331a6a52019-05-27 16:39:22 +02001177Example setting the program name::
1178
1179 void init_python(void)
1180 {
1181 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +02001182
Victor Stinner8462a492019-10-01 12:06:16 +02001183 PyConfig config;
1184 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001185
1186 /* Set the program name. Implicitly preinitialize Python. */
1187 status = PyConfig_SetString(&config, &config.program_name,
1188 L"/path/to/my_program");
1189 if (PyStatus_Exception(status)) {
Victor Stinnerdc42af82020-11-05 18:58:07 +01001190 goto exception;
Victor Stinner331a6a52019-05-27 16:39:22 +02001191 }
1192
1193 status = Py_InitializeFromConfig(&config);
1194 if (PyStatus_Exception(status)) {
Victor Stinnerdc42af82020-11-05 18:58:07 +01001195 goto exception;
Victor Stinner331a6a52019-05-27 16:39:22 +02001196 }
1197 PyConfig_Clear(&config);
1198 return;
1199
Victor Stinnerdc42af82020-11-05 18:58:07 +01001200 exception:
Victor Stinner331a6a52019-05-27 16:39:22 +02001201 PyConfig_Clear(&config);
1202 Py_ExitStatusException(status);
1203 }
1204
1205More complete example modifying the default configuration, read the
1206configuration, and then override some parameters::
1207
1208 PyStatus init_python(const char *program_name)
1209 {
1210 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +02001211
Victor Stinner8462a492019-10-01 12:06:16 +02001212 PyConfig config;
1213 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001214
Gurupad Hegde6c7bb382019-12-28 17:16:02 -05001215 /* Set the program name before reading the configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001216 (decode byte string from the locale encoding).
1217
1218 Implicitly preinitialize Python. */
1219 status = PyConfig_SetBytesString(&config, &config.program_name,
Victor Stinner4b9aad42020-11-02 16:49:54 +01001220 program_name);
Victor Stinner331a6a52019-05-27 16:39:22 +02001221 if (PyStatus_Exception(status)) {
1222 goto done;
1223 }
1224
1225 /* Read all configuration at once */
1226 status = PyConfig_Read(&config);
1227 if (PyStatus_Exception(status)) {
1228 goto done;
1229 }
1230
1231 /* Append our custom search path to sys.path */
1232 status = PyWideStringList_Append(&config.module_search_paths,
Victor Stinner88feaec2019-09-26 03:15:07 +02001233 L"/path/to/more/modules");
Victor Stinner331a6a52019-05-27 16:39:22 +02001234 if (PyStatus_Exception(status)) {
1235 goto done;
1236 }
1237
1238 /* Override executable computed by PyConfig_Read() */
1239 status = PyConfig_SetString(&config, &config.executable,
1240 L"/path/to/my_executable");
1241 if (PyStatus_Exception(status)) {
1242 goto done;
1243 }
1244
1245 status = Py_InitializeFromConfig(&config);
1246
1247 done:
1248 PyConfig_Clear(&config);
1249 return status;
1250 }
1251
1252
1253.. _init-isolated-conf:
1254
1255Isolated Configuration
Victor Stinnerdc42af82020-11-05 18:58:07 +01001256======================
Victor Stinner331a6a52019-05-27 16:39:22 +02001257
1258:c:func:`PyPreConfig_InitIsolatedConfig` and
1259:c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to
1260isolate Python from the system. For example, to embed Python into an
1261application.
1262
1263This configuration ignores global configuration variables, environments
Victor Stinner88feaec2019-09-26 03:15:07 +02001264variables, command line arguments (:c:member:`PyConfig.argv` is not parsed)
1265and user site directory. The C standard streams (ex: ``stdout``) and the
1266LC_CTYPE locale are left unchanged. Signal handlers are not installed.
Victor Stinner331a6a52019-05-27 16:39:22 +02001267
1268Configuration files are still used with this configuration. Set the
Victor Stinnerace3f9a2020-11-10 21:10:22 +01001269:ref:`Python Path Configuration <init-path-config>` ("output fields") to ignore these
Victor Stinner331a6a52019-05-27 16:39:22 +02001270configuration files and avoid the function computing the default path
1271configuration.
1272
1273
1274.. _init-python-config:
1275
1276Python Configuration
Victor Stinnerdc42af82020-11-05 18:58:07 +01001277====================
Victor Stinner331a6a52019-05-27 16:39:22 +02001278
1279:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
1280functions create a configuration to build a customized Python which behaves as
1281the regular Python.
1282
1283Environments variables and command line arguments are used to configure
1284Python, whereas global configuration variables are ignored.
1285
Victor Stinner4b9aad42020-11-02 16:49:54 +01001286This function enables C locale coercion (:pep:`538`)
1287and :ref:`Python UTF-8 Mode <utf8-mode>`
Victor Stinner331a6a52019-05-27 16:39:22 +02001288(:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and
1289:envvar:`PYTHONCOERCECLOCALE` environment variables.
1290
Victor Stinner331a6a52019-05-27 16:39:22 +02001291
1292.. _init-path-config:
1293
Victor Stinnerace3f9a2020-11-10 21:10:22 +01001294Python Path Configuration
1295=========================
Victor Stinner331a6a52019-05-27 16:39:22 +02001296
1297:c:type:`PyConfig` contains multiple fields for the path configuration:
1298
Victor Stinner8bf39b62019-09-26 02:22:35 +02001299* Path configuration inputs:
Victor Stinner331a6a52019-05-27 16:39:22 +02001300
1301 * :c:member:`PyConfig.home`
Sandro Mani8f023a22020-06-08 17:28:11 +02001302 * :c:member:`PyConfig.platlibdir`
Victor Stinner331a6a52019-05-27 16:39:22 +02001303 * :c:member:`PyConfig.pathconfig_warnings`
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001304 * :c:member:`PyConfig.program_name`
1305 * :c:member:`PyConfig.pythonpath_env`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001306 * current working directory: to get absolute paths
1307 * ``PATH`` environment variable to get the program full path
1308 (from :c:member:`PyConfig.program_name`)
1309 * ``__PYVENV_LAUNCHER__`` environment variable
1310 * (Windows only) Application paths in the registry under
1311 "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and
1312 HKEY_LOCAL_MACHINE (where X.Y is the Python version).
Victor Stinner331a6a52019-05-27 16:39:22 +02001313
1314* Path configuration output fields:
1315
Victor Stinner8bf39b62019-09-26 02:22:35 +02001316 * :c:member:`PyConfig.base_exec_prefix`
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001317 * :c:member:`PyConfig.base_executable`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001318 * :c:member:`PyConfig.base_prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +02001319 * :c:member:`PyConfig.exec_prefix`
1320 * :c:member:`PyConfig.executable`
Victor Stinner331a6a52019-05-27 16:39:22 +02001321 * :c:member:`PyConfig.module_search_paths_set`,
1322 :c:member:`PyConfig.module_search_paths`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001323 * :c:member:`PyConfig.prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +02001324
Victor Stinner8bf39b62019-09-26 02:22:35 +02001325If at least one "output field" is not set, Python calculates the path
Victor Stinner331a6a52019-05-27 16:39:22 +02001326configuration to fill unset fields. If
1327:c:member:`~PyConfig.module_search_paths_set` is equal to 0,
Min ho Kim39d87b52019-08-31 06:21:19 +10001328:c:member:`~PyConfig.module_search_paths` is overridden and
Victor Stinner331a6a52019-05-27 16:39:22 +02001329:c:member:`~PyConfig.module_search_paths_set` is set to 1.
1330
Victor Stinner8bf39b62019-09-26 02:22:35 +02001331It is possible to completely ignore the function calculating the default
Victor Stinner331a6a52019-05-27 16:39:22 +02001332path configuration by setting explicitly all path configuration output
1333fields listed above. A string is considered as set even if it is non-empty.
1334``module_search_paths`` is considered as set if
1335``module_search_paths_set`` is set to 1. In this case, path
1336configuration input fields are ignored as well.
1337
1338Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when
Victor Stinner8bf39b62019-09-26 02:22:35 +02001339calculating the path configuration (Unix only, Windows does not log any warning).
Victor Stinner331a6a52019-05-27 16:39:22 +02001340
1341If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix`
1342fields are not set, they inherit their value from :c:member:`~PyConfig.prefix`
1343and :c:member:`~PyConfig.exec_prefix` respectively.
1344
1345:c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`:
1346
1347* If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a
1348 ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to
1349 :data:`sys.path`.
1350* If :c:member:`~PyConfig.isolated` is zero:
1351
1352 * If :c:member:`~PyConfig.run_module` is set, prepend the current directory
1353 to :data:`sys.path`. Do nothing if the current directory cannot be read.
1354 * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the
1355 filename to :data:`sys.path`.
1356 * Otherwise, prepend an empty string to :data:`sys.path`.
1357
1358If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be
1359modified by the :mod:`site` module. If
1360:c:member:`~PyConfig.user_site_directory` is non-zero and the user's
1361site-package directory exists, the :mod:`site` module appends the user's
1362site-package directory to :data:`sys.path`.
1363
1364The following configuration files are used by the path configuration:
1365
1366* ``pyvenv.cfg``
1367* ``python._pth`` (Windows only)
1368* ``pybuilddir.txt`` (Unix only)
1369
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001370The ``__PYVENV_LAUNCHER__`` environment variable is used to set
1371:c:member:`PyConfig.base_executable`
1372
Victor Stinner331a6a52019-05-27 16:39:22 +02001373
1374Py_RunMain()
Victor Stinnerdc42af82020-11-05 18:58:07 +01001375============
Victor Stinner331a6a52019-05-27 16:39:22 +02001376
1377.. c:function:: int Py_RunMain(void)
1378
1379 Execute the command (:c:member:`PyConfig.run_command`), the script
1380 (:c:member:`PyConfig.run_filename`) or the module
1381 (:c:member:`PyConfig.run_module`) specified on the command line or in the
1382 configuration.
1383
1384 By default and when if :option:`-i` option is used, run the REPL.
1385
1386 Finally, finalizes Python and returns an exit status that can be passed to
1387 the ``exit()`` function.
1388
1389See :ref:`Python Configuration <init-python-config>` for an example of
1390customized Python always running in isolated mode using
1391:c:func:`Py_RunMain`.
1392
1393
Victor Stinnere81f6e62020-06-08 18:12:59 +02001394Py_GetArgcArgv()
Victor Stinnerdc42af82020-11-05 18:58:07 +01001395================
Victor Stinnere81f6e62020-06-08 18:12:59 +02001396
1397.. c:function:: void Py_GetArgcArgv(int *argc, wchar_t ***argv)
1398
1399 Get the original command line arguments, before Python modified them.
1400
Victor Stinnerdd8a93e2020-06-30 00:49:03 +02001401 See also :c:member:`PyConfig.orig_argv` member.
1402
Victor Stinnere81f6e62020-06-08 18:12:59 +02001403
Victor Stinner331a6a52019-05-27 16:39:22 +02001404Multi-Phase Initialization Private Provisional API
Victor Stinnerdc42af82020-11-05 18:58:07 +01001405==================================================
Victor Stinner331a6a52019-05-27 16:39:22 +02001406
1407This section is a private provisional API introducing multi-phase
1408initialization, the core feature of the :pep:`432`:
1409
1410* "Core" initialization phase, "bare minimum Python":
1411
1412 * Builtin types;
1413 * Builtin exceptions;
1414 * Builtin and frozen modules;
1415 * The :mod:`sys` module is only partially initialized
Victor Stinner88feaec2019-09-26 03:15:07 +02001416 (ex: :data:`sys.path` doesn't exist yet).
Victor Stinner331a6a52019-05-27 16:39:22 +02001417
1418* "Main" initialization phase, Python is fully initialized:
1419
1420 * Install and configure :mod:`importlib`;
1421 * Apply the :ref:`Path Configuration <init-path-config>`;
1422 * Install signal handlers;
1423 * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout`
1424 and :data:`sys.path`);
1425 * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`;
1426 * Import the :mod:`site` module;
1427 * etc.
1428
1429Private provisional API:
1430
1431* :c:member:`PyConfig._init_main`: if set to 0,
1432 :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase.
Victor Stinner252346a2020-05-01 11:33:44 +02001433* :c:member:`PyConfig._isolated_interpreter`: if non-zero,
1434 disallow threads, subprocesses and fork.
Victor Stinner331a6a52019-05-27 16:39:22 +02001435
1436.. c:function:: PyStatus _Py_InitializeMain(void)
1437
1438 Move to the "Main" initialization phase, finish the Python initialization.
1439
1440No module is imported during the "Core" phase and the ``importlib`` module is
1441not configured: the :ref:`Path Configuration <init-path-config>` is only
1442applied during the "Main" phase. It may allow to customize Python in Python to
1443override or tune the :ref:`Path Configuration <init-path-config>`, maybe
Victor Stinner88feaec2019-09-26 03:15:07 +02001444install a custom :data:`sys.meta_path` importer or an import hook, etc.
Victor Stinner331a6a52019-05-27 16:39:22 +02001445
Victor Stinner88feaec2019-09-26 03:15:07 +02001446It may become possible to calculatin the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001447<init-path-config>` in Python, after the Core phase and before the Main phase,
1448which is one of the :pep:`432` motivation.
1449
1450The "Core" phase is not properly defined: what should be and what should
1451not be available at this phase is not specified yet. The API is marked
1452as private and provisional: the API can be modified or even be removed
1453anytime until a proper public API is designed.
1454
1455Example running Python code between "Core" and "Main" initialization
1456phases::
1457
1458 void init_python(void)
1459 {
1460 PyStatus status;
Victor Stinner8462a492019-10-01 12:06:16 +02001461
Victor Stinner331a6a52019-05-27 16:39:22 +02001462 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001463 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001464 config._init_main = 0;
1465
1466 /* ... customize 'config' configuration ... */
1467
1468 status = Py_InitializeFromConfig(&config);
1469 PyConfig_Clear(&config);
1470 if (PyStatus_Exception(status)) {
1471 Py_ExitStatusException(status);
1472 }
1473
1474 /* Use sys.stderr because sys.stdout is only created
1475 by _Py_InitializeMain() */
1476 int res = PyRun_SimpleString(
1477 "import sys; "
1478 "print('Run Python code before _Py_InitializeMain', "
1479 "file=sys.stderr)");
1480 if (res < 0) {
1481 exit(1);
1482 }
1483
1484 /* ... put more configuration code here ... */
1485
1486 status = _Py_InitializeMain();
1487 if (PyStatus_Exception(status)) {
1488 Py_ExitStatusException(status);
1489 }
1490 }