blob: c957d6c0f723c25a1c732f7b9de2b770acba99a9 [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
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`
Victor Stinner36242fd2019-07-01 19:13:50 +020028* :c:func:`PyConfig_SetWideStringList`
Victor Stinner331a6a52019-05-27 16:39:22 +020029* :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`
Victor Stinnere81f6e62020-06-08 18:12:59 +020046* :c:func:`Py_GetArgcArgv`
Victor Stinner331a6a52019-05-27 16:39:22 +020047
48The preconfiguration (``PyPreConfig`` type) is stored in
49``_PyRuntime.preconfig`` and the configuration (``PyConfig`` type) is stored in
50``PyInterpreterState.config``.
51
Victor Stinner1beb7c32019-08-23 17:59:12 +010052See also :ref:`Initialization, Finalization, and Threads <initialization>`.
53
Victor Stinner331a6a52019-05-27 16:39:22 +020054.. seealso::
55 :pep:`587` "Python Initialization Configuration".
56
57
58PyWideStringList
59----------------
60
61.. c:type:: PyWideStringList
62
63 List of ``wchar_t*`` strings.
64
Serhiy Storchakae835b312019-10-30 21:37:16 +020065 If *length* is non-zero, *items* must be non-``NULL`` and all strings must be
66 non-``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +020067
68 Methods:
69
70 .. c:function:: PyStatus PyWideStringList_Append(PyWideStringList *list, const wchar_t *item)
71
72 Append *item* to *list*.
73
74 Python must be preinitialized to call this function.
75
76 .. c:function:: PyStatus PyWideStringList_Insert(PyWideStringList *list, Py_ssize_t index, const wchar_t *item)
77
Victor Stinner3842f292019-08-23 16:57:54 +010078 Insert *item* into *list* at *index*.
79
80 If *index* is greater than or equal to *list* length, append *item* to
81 *list*.
82
83 *index* must be greater than or equal to 0.
Victor Stinner331a6a52019-05-27 16:39:22 +020084
85 Python must be preinitialized to call this function.
86
87 Structure fields:
88
89 .. c:member:: Py_ssize_t length
90
91 List length.
92
93 .. c:member:: wchar_t** items
94
95 List items.
96
97PyStatus
98--------
99
100.. c:type:: PyStatus
101
102 Structure to store an initialization function status: success, error
103 or exit.
104
105 For an error, it can store the C function name which created the error.
106
107 Structure fields:
108
109 .. c:member:: int exitcode
110
111 Exit code. Argument passed to ``exit()``.
112
113 .. c:member:: const char *err_msg
114
115 Error message.
116
117 .. c:member:: const char *func
118
119 Name of the function which created an error, can be ``NULL``.
120
121 Functions to create a status:
122
123 .. c:function:: PyStatus PyStatus_Ok(void)
124
125 Success.
126
127 .. c:function:: PyStatus PyStatus_Error(const char *err_msg)
128
129 Initialization error with a message.
130
Victor Stinner048a3562020-11-05 00:45:56 +0100131 *err_msg* must not be ``NULL``.
132
Victor Stinner331a6a52019-05-27 16:39:22 +0200133 .. c:function:: PyStatus PyStatus_NoMemory(void)
134
135 Memory allocation failure (out of memory).
136
137 .. c:function:: PyStatus PyStatus_Exit(int exitcode)
138
139 Exit Python with the specified exit code.
140
141 Functions to handle a status:
142
143 .. c:function:: int PyStatus_Exception(PyStatus status)
144
145 Is the status an error or an exit? If true, the exception must be
146 handled; by calling :c:func:`Py_ExitStatusException` for example.
147
148 .. c:function:: int PyStatus_IsError(PyStatus status)
149
150 Is the result an error?
151
152 .. c:function:: int PyStatus_IsExit(PyStatus status)
153
154 Is the result an exit?
155
156 .. c:function:: void Py_ExitStatusException(PyStatus status)
157
158 Call ``exit(exitcode)`` if *status* is an exit. Print the error
159 message and exit with a non-zero exit code if *status* is an error. Must
160 only be called if ``PyStatus_Exception(status)`` is non-zero.
161
162.. note::
163 Internally, Python uses macros which set ``PyStatus.func``,
164 whereas functions to create a status set ``func`` to ``NULL``.
165
166Example::
167
168 PyStatus alloc(void **ptr, size_t size)
169 {
170 *ptr = PyMem_RawMalloc(size);
171 if (*ptr == NULL) {
172 return PyStatus_NoMemory();
173 }
174 return PyStatus_Ok();
175 }
176
177 int main(int argc, char **argv)
178 {
179 void *ptr;
180 PyStatus status = alloc(&ptr, 16);
181 if (PyStatus_Exception(status)) {
182 Py_ExitStatusException(status);
183 }
184 PyMem_Free(ptr);
185 return 0;
186 }
187
188
189PyPreConfig
190-----------
191
192.. c:type:: PyPreConfig
193
Victor Stinner4b9aad42020-11-02 16:49:54 +0100194 Structure used to preinitialize Python.
Victor Stinner331a6a52019-05-27 16:39:22 +0200195
196 Function to initialize a preconfiguration:
197
tomerv741008a2020-07-01 12:32:54 +0300198 .. c:function:: void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200199
200 Initialize the preconfiguration with :ref:`Python Configuration
201 <init-python-config>`.
202
tomerv741008a2020-07-01 12:32:54 +0300203 .. c:function:: void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200204
205 Initialize the preconfiguration with :ref:`Isolated Configuration
206 <init-isolated-conf>`.
207
208 Structure fields:
209
210 .. c:member:: int allocator
211
Victor Stinner4b9aad42020-11-02 16:49:54 +0100212 Name of the Python memory allocators:
Victor Stinner331a6a52019-05-27 16:39:22 +0200213
214 * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators
215 (use defaults)
216 * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators
217 * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with
218 debug hooks
219 * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()``
220 * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of
221 ``malloc()`` with debug hooks
222 * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory
223 allocator <pymalloc>`
224 * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc
225 memory allocator <pymalloc>` with debug hooks
226
227 ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG``
228 are not supported if Python is configured using ``--without-pymalloc``
229
230 See :ref:`Memory Management <memory>`.
231
Victor Stinner4b9aad42020-11-02 16:49:54 +0100232 Default: ``PYMEM_ALLOCATOR_NOT_SET``.
233
Victor Stinner331a6a52019-05-27 16:39:22 +0200234 .. c:member:: int configure_locale
235
Victor Stinner4b9aad42020-11-02 16:49:54 +0100236 Set the LC_CTYPE locale to the user preferred locale?
237
238 If equals to 0, set :c:member:`~PyPreConfig.coerce_c_locale` and
239 :c:member:`~PyPreConfig.coerce_c_locale_warn` members to 0.
240
241 See the :term:`locale encoding`.
242
243 Default: ``1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200244
245 .. c:member:: int coerce_c_locale
246
Victor Stinner4b9aad42020-11-02 16:49:54 +0100247 If equals to 2, coerce the C locale.
248
249 If equals to 1, read the LC_CTYPE locale to decide if it should be
250 coerced.
251
252 See the :term:`locale encoding`.
253
254 Default: ``-1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200255
256 .. c:member:: int coerce_c_locale_warn
Victor Stinner88feaec2019-09-26 03:15:07 +0200257
Victor Stinner331a6a52019-05-27 16:39:22 +0200258 If non-zero, emit a warning if the C locale is coerced.
259
Victor Stinner4b9aad42020-11-02 16:49:54 +0100260 Default: ``-1`` in Python config, ``0`` in isolated config.
261
Victor Stinner331a6a52019-05-27 16:39:22 +0200262 .. c:member:: int dev_mode
263
Victor Stinner4b9aad42020-11-02 16:49:54 +0100264 If non-zero, enables the :ref:`Python Development Mode <devmode>`:
265 see :c:member:`PyConfig.dev_mode`.
266
267 Default: ``-1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200268
269 .. c:member:: int isolated
270
Victor Stinner4b9aad42020-11-02 16:49:54 +0100271 Isolated mode: see :c:member:`PyConfig.isolated`.
272
273 Default: ``0`` in Python mode, ``1`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200274
Victor Stinnere662c392020-11-01 23:07:23 +0100275 .. c:member:: int legacy_windows_fs_encoding
Victor Stinner331a6a52019-05-27 16:39:22 +0200276
Victor Stinnere662c392020-11-01 23:07:23 +0100277 If non-zero:
278
279 * Set :c:member:`PyPreConfig.utf8_mode` to ``0``,
280 * Set :c:member:`PyConfig.filesystem_encoding` to ``"mbcs"``,
281 * Set :c:member:`PyConfig.filesystem_errors` to ``"replace"``.
282
283 Initialized the from :envvar:`PYTHONLEGACYWINDOWSFSENCODING` environment
284 variable value.
Victor Stinner331a6a52019-05-27 16:39:22 +0200285
286 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
287 Windows specific code.
288
Victor Stinner4b9aad42020-11-02 16:49:54 +0100289 Default: ``0``.
290
Victor Stinner331a6a52019-05-27 16:39:22 +0200291 .. c:member:: int parse_argv
292
293 If non-zero, :c:func:`Py_PreInitializeFromArgs` and
294 :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the
295 same way the regular Python parses command line arguments: see
296 :ref:`Command Line Arguments <using-on-cmdline>`.
297
Victor Stinner4b9aad42020-11-02 16:49:54 +0100298 Default: ``1`` in Python config, ``0`` in isolated config.
299
Victor Stinner331a6a52019-05-27 16:39:22 +0200300 .. c:member:: int use_environment
301
Victor Stinner4b9aad42020-11-02 16:49:54 +0100302 Use :ref:`environment variables <using-on-envvars>`? See
303 :c:member:`PyConfig.use_environment`.
304
305 Default: ``1`` in Python config and ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200306
307 .. c:member:: int utf8_mode
308
Victor Stinner4b9aad42020-11-02 16:49:54 +0100309 If non-zero, enable the :ref:`Python UTF-8 Mode <utf8-mode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200310
Victor Stinner4b9aad42020-11-02 16:49:54 +0100311 Set by the :option:`-X utf8 <-X>` command line option and the
312 :envvar:`PYTHONUTF8` environment variable.
313
314 Default: ``-1`` in Python config and ``0`` in isolated config.
315
316
317.. _c-preinit:
318
319Preinitialize Python with PyPreConfig
320-------------------------------------
321
322The preinitialization of Python:
323
324* Set the Python memory allocators (:c:member:`PyPreConfig.allocator`)
325* Configure the LC_CTYPE locale (:term:`locale encoding`)
326* Set the :ref:`Python UTF-8 Mode <utf8-mode>`
327 (:c:member:`PyPreConfig.utf8_mode`)
Victor Stinner331a6a52019-05-27 16:39:22 +0200328
329Functions to preinitialize Python:
330
331.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
332
333 Preinitialize Python from *preconfig* preconfiguration.
334
335.. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv)
336
Victor Stinner4b9aad42020-11-02 16:49:54 +0100337 Preinitialize Python from *preconfig* preconfiguration.
338
339 Parse *argv* command line arguments (bytes strings) if
340 :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero.
Victor Stinner331a6a52019-05-27 16:39:22 +0200341
342.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)
343
Victor Stinner4b9aad42020-11-02 16:49:54 +0100344 Preinitialize Python from *preconfig* preconfiguration.
345
346 Parse *argv* command line arguments (wide strings) if
347 :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero.
Victor Stinner331a6a52019-05-27 16:39:22 +0200348
349The caller is responsible to handle exceptions (error or exit) using
350:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
351
352For :ref:`Python Configuration <init-python-config>`
353(:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with
354command line arguments, the command line arguments must also be passed to
355preinitialize Python, since they have an effect on the pre-configuration
Victor Stinner88feaec2019-09-26 03:15:07 +0200356like encodings. For example, the :option:`-X utf8 <-X>` command line option
Victor Stinner4b9aad42020-11-02 16:49:54 +0100357enables the :ref:`Python UTF-8 Mode <utf8-mode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200358
359``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and
360before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator.
361It can be called before :c:func:`Py_PreInitialize` if
362:c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``.
363
364Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be
Victor Stinner4b9aad42020-11-02 16:49:54 +0100365used before the Python preinitialization, whereas calling directly ``malloc()``
366and ``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called
367before the Python preinitialization.
Victor Stinner331a6a52019-05-27 16:39:22 +0200368
Victor Stinner4b9aad42020-11-02 16:49:54 +0100369Example using the preinitialization to enable
370the :ref:`Python UTF-8 Mode <utf8-mode>`::
Victor Stinner331a6a52019-05-27 16:39:22 +0200371
Victor Stinner441b10c2019-09-28 04:28:35 +0200372 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +0200373 PyPreConfig preconfig;
Victor Stinner3c30a762019-10-01 10:56:37 +0200374 PyPreConfig_InitPythonConfig(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200375
376 preconfig.utf8_mode = 1;
377
Victor Stinner441b10c2019-09-28 04:28:35 +0200378 status = Py_PreInitialize(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200379 if (PyStatus_Exception(status)) {
380 Py_ExitStatusException(status);
381 }
382
Victor Stinner4b9aad42020-11-02 16:49:54 +0100383 /* at this point, Python speaks UTF-8 */
Victor Stinner331a6a52019-05-27 16:39:22 +0200384
385 Py_Initialize();
386 /* ... use Python API here ... */
387 Py_Finalize();
388
389
390PyConfig
391--------
392
393.. c:type:: PyConfig
394
395 Structure containing most parameters to configure Python.
396
Victor Stinner4b9aad42020-11-02 16:49:54 +0100397 When done, the :c:func:`PyConfig_Clear` function must be used to release the
398 configuration memory.
399
Victor Stinner331a6a52019-05-27 16:39:22 +0200400 Structure methods:
401
Victor Stinner8462a492019-10-01 12:06:16 +0200402 .. c:function:: void PyConfig_InitPythonConfig(PyConfig *config)
Victor Stinner331a6a52019-05-27 16:39:22 +0200403
Victor Stinner4b9aad42020-11-02 16:49:54 +0100404 Initialize configuration with the :ref:`Python Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200405 <init-python-config>`.
406
Victor Stinner8462a492019-10-01 12:06:16 +0200407 .. c:function:: void PyConfig_InitIsolatedConfig(PyConfig *config)
Victor Stinner331a6a52019-05-27 16:39:22 +0200408
Victor Stinner4b9aad42020-11-02 16:49:54 +0100409 Initialize configuration with the :ref:`Isolated Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200410 <init-isolated-conf>`.
411
412 .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str)
413
414 Copy the wide character string *str* into ``*config_str``.
415
Victor Stinner4b9aad42020-11-02 16:49:54 +0100416 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200417
418 .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str)
419
Victor Stinner4b9aad42020-11-02 16:49:54 +0100420 Decode *str* using :c:func:`Py_DecodeLocale` and set the result into
421 ``*config_str``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200422
Victor Stinner4b9aad42020-11-02 16:49:54 +0100423 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200424
425 .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv)
426
Victor Stinner4b9aad42020-11-02 16:49:54 +0100427 Set command line arguments (:c:member:`~PyConfig.argv` member of
428 *config*) from the *argv* list of wide character strings.
Victor Stinner331a6a52019-05-27 16:39:22 +0200429
Victor Stinner4b9aad42020-11-02 16:49:54 +0100430 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200431
432 .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv)
433
Victor Stinner4b9aad42020-11-02 16:49:54 +0100434 Set command line arguments (:c:member:`~PyConfig.argv` member of
435 *config*) from the *argv* list of bytes strings. Decode bytes using
436 :c:func:`Py_DecodeLocale`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200437
Victor Stinner4b9aad42020-11-02 16:49:54 +0100438 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200439
Victor Stinner36242fd2019-07-01 19:13:50 +0200440 .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items)
441
442 Set the list of wide strings *list* to *length* and *items*.
443
Victor Stinner4b9aad42020-11-02 16:49:54 +0100444 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner36242fd2019-07-01 19:13:50 +0200445
Victor Stinner331a6a52019-05-27 16:39:22 +0200446 .. c:function:: PyStatus PyConfig_Read(PyConfig *config)
447
448 Read all Python configuration.
449
450 Fields which are already initialized are left unchanged.
451
Victor Stinner4b9aad42020-11-02 16:49:54 +0100452 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200453
454 .. c:function:: void PyConfig_Clear(PyConfig *config)
455
456 Release configuration memory.
457
Victor Stinner4b9aad42020-11-02 16:49:54 +0100458 Most ``PyConfig`` methods :ref:`preinitialize Python <c-preinit>` if needed.
459 In that case, the Python preinitialization configuration
460 (:c:type:`PyPreConfig`) in based on the :c:type:`PyConfig`. If configuration
461 fields which are in common with :c:type:`PyPreConfig` are tuned, they must
462 be set before calling a :c:type:`PyConfig` method:
Victor Stinner331a6a52019-05-27 16:39:22 +0200463
Victor Stinner4b9aad42020-11-02 16:49:54 +0100464 * :c:member:`PyConfig.dev_mode`
465 * :c:member:`PyConfig.isolated`
466 * :c:member:`PyConfig.parse_argv`
467 * :c:member:`PyConfig.use_environment`
Victor Stinner331a6a52019-05-27 16:39:22 +0200468
469 Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv`
Victor Stinner4b9aad42020-11-02 16:49:54 +0100470 is used, this method must be called before other methods, since the
Victor Stinner331a6a52019-05-27 16:39:22 +0200471 preinitialization configuration depends on command line arguments (if
472 :c:member:`parse_argv` is non-zero).
473
474 The caller of these methods is responsible to handle exceptions (error or
475 exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``.
476
477 Structure fields:
478
479 .. c:member:: PyWideStringList argv
480
Victor Stinner4b9aad42020-11-02 16:49:54 +0100481 Command line arguments: :data:`sys.argv`.
482
483 Set :c:member:`~PyConfig.parse_argv` to ``1`` to parse
484 :c:member:`~PyConfig.argv` the same way the regular Python parses Python
485 command line arguments and then to strip Python arguments from
486 :c:member:`~PyConfig.argv`.
487
488 If :c:member:`~PyConfig.argv` is empty, an empty string is added to
489 ensure that :data:`sys.argv` always exists and is never empty.
490
491 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200492
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200493 See also the :c:member:`~PyConfig.orig_argv` member.
494
Victor Stinner331a6a52019-05-27 16:39:22 +0200495 .. c:member:: wchar_t* base_exec_prefix
496
497 :data:`sys.base_exec_prefix`.
498
Victor Stinner4b9aad42020-11-02 16:49:54 +0100499 Default: ``NULL``.
500
501 Part of the :ref:`Path Configuration <init-path-config>` output.
502
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200503 .. c:member:: wchar_t* base_executable
504
Victor Stinner4b9aad42020-11-02 16:49:54 +0100505 Python base executable: :data:`sys._base_executable`.
506
507 Set by the :envvar:`__PYVENV_LAUNCHER__` environment variable.
508
509 Set from :c:member:`PyConfig.executable` if ``NULL``.
510
511 Default: ``NULL``.
512
513 Part of the :ref:`Path Configuration <init-path-config>` output.
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200514
Victor Stinner331a6a52019-05-27 16:39:22 +0200515 .. c:member:: wchar_t* base_prefix
516
517 :data:`sys.base_prefix`.
518
Victor Stinner4b9aad42020-11-02 16:49:54 +0100519 Default: ``NULL``.
Sandro Mani8f023a22020-06-08 17:28:11 +0200520
Victor Stinner4b9aad42020-11-02 16:49:54 +0100521 Part of the :ref:`Path Configuration <init-path-config>` output.
Sandro Mani8f023a22020-06-08 17:28:11 +0200522
Victor Stinner331a6a52019-05-27 16:39:22 +0200523 .. c:member:: int buffered_stdio
524
Victor Stinner4b9aad42020-11-02 16:49:54 +0100525 If equals to 0 and :c:member:`~PyConfig.configure_c_stdio` is non-zero,
526 disable buffering on the C streams stdout and stderr.
527
528 Set to 0 by the :option:`-u` command line option and the
529 :envvar:`PYTHONUNBUFFERED` environment variable.
Victor Stinner331a6a52019-05-27 16:39:22 +0200530
531 stdin is always opened in buffered mode.
532
Victor Stinner4b9aad42020-11-02 16:49:54 +0100533 Default: ``1``.
534
Victor Stinner331a6a52019-05-27 16:39:22 +0200535 .. c:member:: int bytes_warning
536
537 If equals to 1, issue a warning when comparing :class:`bytes` or
538 :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with
Victor Stinner4b9aad42020-11-02 16:49:54 +0100539 :class:`int`.
540
541 If equal or greater to 2, raise a :exc:`BytesWarning` exception in these
542 cases.
543
544 Incremented by the :option:`-b` command line option.
545
546 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200547
548 .. c:member:: wchar_t* check_hash_pycs_mode
549
Victor Stinner4b9aad42020-11-02 16:49:54 +0100550 Control the validation behavior of hash-based ``.pyc`` files:
551 value of the :option:`--check-hash-based-pycs` command line option.
Victor Stinner331a6a52019-05-27 16:39:22 +0200552
Victor Stinner4b9aad42020-11-02 16:49:54 +0100553 Valid values:
Victor Stinner331a6a52019-05-27 16:39:22 +0200554
Victor Stinner4b9aad42020-11-02 16:49:54 +0100555 - ``L"always"``: Hash the source file for invalidation regardless of
556 value of the 'check_source' flag.
557 - ``L"never"``: Assume that hash-based pycs always are valid.
558 - ``L"default"``: The 'check_source' flag in hash-based pycs
559 determines invalidation.
560
561 Default: ``L"default"``.
562
563 See also :pep:`552` "Deterministic pycs".
Victor Stinner331a6a52019-05-27 16:39:22 +0200564
565 .. c:member:: int configure_c_stdio
566
Victor Stinner4b9aad42020-11-02 16:49:54 +0100567 If non-zero, configure C standard streams:
568
569 * On Windows, set the binary mode (``O_BINARY``) on stdin, stdout and
570 stderr.
571 * If :c:member:`~PyConfig.buffered_stdio` equals zero, disable buffering
572 of stdin, stdout and stderr streams.
573 * If :c:member:`~PyConfig.interactive` is non-zero, enable stream
574 buffering on stdin and stdout (only stdout on Windows).
575
576 Default: ``1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200577
578 .. c:member:: int dev_mode
579
Victor Stinnerb9783d22020-01-24 10:22:18 +0100580 If non-zero, enable the :ref:`Python Development Mode <devmode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200581
Victor Stinner4b9aad42020-11-02 16:49:54 +0100582 Default: ``-1`` in Python mode, ``0`` in isolated mode.
583
Victor Stinner331a6a52019-05-27 16:39:22 +0200584 .. c:member:: int dump_refs
585
Victor Stinner4b9aad42020-11-02 16:49:54 +0100586 Dump Python refererences?
587
Victor Stinner331a6a52019-05-27 16:39:22 +0200588 If non-zero, dump all objects which are still alive at exit.
589
Victor Stinner4b9aad42020-11-02 16:49:54 +0100590 Set to ``1`` by the :envvar:`PYTHONDUMPREFS` environment variable.
591
592 Need a special build of Python with the ``Py_TRACE_REFS`` macro defined.
593
594 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200595
596 .. c:member:: wchar_t* exec_prefix
597
Victor Stinner4b9aad42020-11-02 16:49:54 +0100598 The site-specific directory prefix where the platform-dependent Python
599 files are installed: :data:`sys.exec_prefix`.
600
601 Default: ``NULL``.
602
603 Part of the :ref:`Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200604
605 .. c:member:: wchar_t* executable
606
Victor Stinner4b9aad42020-11-02 16:49:54 +0100607 The absolute path of the executable binary for the Python interpreter:
Victor Stinner331a6a52019-05-27 16:39:22 +0200608 :data:`sys.executable`.
609
Victor Stinner4b9aad42020-11-02 16:49:54 +0100610 Default: ``NULL``.
611
612 Part of the :ref:`Path Configuration <init-path-config>` output.
613
Victor Stinner331a6a52019-05-27 16:39:22 +0200614 .. c:member:: int faulthandler
615
Victor Stinner4b9aad42020-11-02 16:49:54 +0100616 Enable faulthandler?
617
Victor Stinner88feaec2019-09-26 03:15:07 +0200618 If non-zero, call :func:`faulthandler.enable` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200619
Victor Stinner4b9aad42020-11-02 16:49:54 +0100620 Set to ``1`` by :option:`-X faulthandler <-X>` and the
621 :envvar:`PYTHONFAULTHANDLER` environment variable.
622
623 Default: ``-1`` in Python mode, ``0`` in isolated mode.
624
Victor Stinner331a6a52019-05-27 16:39:22 +0200625 .. c:member:: wchar_t* filesystem_encoding
626
Victor Stinner4b9aad42020-11-02 16:49:54 +0100627 :term:`Filesystem encoding <filesystem encoding and error handler>`:
628 :func:`sys.getfilesystemencoding`.
Victor Stinnere662c392020-11-01 23:07:23 +0100629
630 On macOS, Android and VxWorks: use ``"utf-8"`` by default.
631
632 On Windows: use ``"utf-8"`` by default, or ``"mbcs"`` if
633 :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of
634 :c:type:`PyPreConfig` is non-zero.
635
636 Default encoding on other platforms:
637
638 * ``"utf-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero.
639 * ``"ascii"`` if Python detects that ``nl_langinfo(CODESET)`` announces
640 the ASCII encoding (or Roman8 encoding on HP-UX), whereas the
641 ``mbstowcs()`` function decodes from a different encoding (usually
642 Latin1).
643 * ``"utf-8"`` if ``nl_langinfo(CODESET)`` returns an empty string.
Victor Stinner4b9aad42020-11-02 16:49:54 +0100644 * Otherwise, use the :term:`locale encoding`:
Victor Stinnere662c392020-11-01 23:07:23 +0100645 ``nl_langinfo(CODESET)`` result.
646
647 At Python statup, the encoding name is normalized to the Python codec
648 name. For example, ``"ANSI_X3.4-1968"`` is replaced with ``"ascii"``.
649
650 See also the :c:member:`~PyConfig.filesystem_errors` member.
Victor Stinner331a6a52019-05-27 16:39:22 +0200651
652 .. c:member:: wchar_t* filesystem_errors
653
Victor Stinner4b9aad42020-11-02 16:49:54 +0100654 :term:`Filesystem error handler <filesystem encoding and error handler>`:
655 :func:`sys.getfilesystemencodeerrors`.
Victor Stinnere662c392020-11-01 23:07:23 +0100656
657 On Windows: use ``"surrogatepass"`` by default, or ``"replace"`` if
658 :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of
659 :c:type:`PyPreConfig` is non-zero.
660
661 On other platforms: use ``"surrogateescape"`` by default.
662
663 Supported error handlers:
664
665 * ``"strict"``
666 * ``"surrogateescape"``
667 * ``"surrogatepass"`` (only supported with the UTF-8 encoding)
668
669 See also the :c:member:`~PyConfig.filesystem_encoding` member.
Victor Stinner331a6a52019-05-27 16:39:22 +0200670
671 .. c:member:: unsigned long hash_seed
672 .. c:member:: int use_hash_seed
673
674 Randomized hash function seed.
675
676 If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly
Victor Stinner4b9aad42020-11-02 16:49:54 +0100677 at Python startup, and :c:member:`~PyConfig.hash_seed` is ignored.
678
679 Set by the :envvar:`PYTHONHASHSEED` environment variable.
680
681 Default *use_hash_seed* value: ``-1`` in Python mode, ``0`` in isolated
682 mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200683
684 .. c:member:: wchar_t* home
685
686 Python home directory.
687
Victor Stinner4b9aad42020-11-02 16:49:54 +0100688 If :c:func:`Py_SetPythonHome` has been called, use its argument if it is
689 not ``NULL``.
690
691 Set by the :envvar:`PYTHONHOME` environment variable.
692
693 Default: ``NULL``.
694
695 Part of the :ref:`Path Configuration <init-path-config>` input.
Victor Stinner88feaec2019-09-26 03:15:07 +0200696
Victor Stinner331a6a52019-05-27 16:39:22 +0200697 .. c:member:: int import_time
698
699 If non-zero, profile import time.
700
Victor Stinner4b9aad42020-11-02 16:49:54 +0100701 Set the ``1`` by the :option:`-X importtime <-X>` option and the
702 :envvar:`PYTHONPROFILEIMPORTTIME` environment variable.
703
704 Default: ``0``.
705
Victor Stinner331a6a52019-05-27 16:39:22 +0200706 .. c:member:: int inspect
707
708 Enter interactive mode after executing a script or a command.
709
Victor Stinner4b9aad42020-11-02 16:49:54 +0100710 If greater than 0, enable inspect: when a script is passed as first
711 argument or the -c option is used, enter interactive mode after executing
712 the script or the command, even when :data:`sys.stdin` does not appear to
713 be a terminal.
714
715 Incremented by the :option:`-i` command line option. Set to ``1`` if the
716 :envvar:`PYTHONINSPECT` environment variable is non-empty.
717
718 Default: ``0``.
719
Victor Stinner331a6a52019-05-27 16:39:22 +0200720 .. c:member:: int install_signal_handlers
721
Victor Stinner4b9aad42020-11-02 16:49:54 +0100722 Install Python signal handlers?
723
724 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200725
726 .. c:member:: int interactive
727
Victor Stinner4b9aad42020-11-02 16:49:54 +0100728 If greater than 0, enable the interactive mode (REPL).
729
730 Incremented by the :option:`-i` command line option.
731
732 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200733
734 .. c:member:: int isolated
735
736 If greater than 0, enable isolated mode:
737
738 * :data:`sys.path` contains neither the script's directory (computed from
739 ``argv[0]`` or the current directory) nor the user's site-packages
740 directory.
741 * Python REPL doesn't import :mod:`readline` nor enable default readline
742 configuration on interactive prompts.
743 * Set :c:member:`~PyConfig.use_environment` and
744 :c:member:`~PyConfig.user_site_directory` to 0.
745
Victor Stinner4b9aad42020-11-02 16:49:54 +0100746 Default: ``0`` in Python mode, ``1`` in isolated mode.
747
748 See also :c:member:`PyPreConfig.isolated`.
749
Victor Stinner331a6a52019-05-27 16:39:22 +0200750 .. c:member:: int legacy_windows_stdio
751
752 If non-zero, use :class:`io.FileIO` instead of
753 :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout`
754 and :data:`sys.stderr`.
755
Victor Stinner4b9aad42020-11-02 16:49:54 +0100756 Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSSTDIO` environment
757 variable is set to a non-empty string.
758
Victor Stinner331a6a52019-05-27 16:39:22 +0200759 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
760 Windows specific code.
761
Victor Stinner4b9aad42020-11-02 16:49:54 +0100762 Default: ``0``.
763
764 See also the :pep:`528` (Change Windows console encoding to UTF-8).
765
Victor Stinner331a6a52019-05-27 16:39:22 +0200766 .. c:member:: int malloc_stats
767
768 If non-zero, dump statistics on :ref:`Python pymalloc memory allocator
769 <pymalloc>` at exit.
770
Victor Stinner4b9aad42020-11-02 16:49:54 +0100771 Set to ``1`` by the :envvar:`PYTHONMALLOCSTATS` environment variable.
772
Victor Stinner331a6a52019-05-27 16:39:22 +0200773 The option is ignored if Python is built using ``--without-pymalloc``.
774
Victor Stinner4b9aad42020-11-02 16:49:54 +0100775 Default: ``0``.
776
777 .. c:member:: wchar_t* platlibdir
778
779 Platform library directory name: :data:`sys.platlibdir`.
780
781 Set by the :envvar:`PYTHONPLATLIBDIR` environment variable.
782
783 Default: value of the ``PLATLIBDIR`` macro which is set at configure time
784 by ``--with-platlibdir`` (default: ``"lib"``).
785
786 Part of the :ref:`Path Configuration <init-path-config>` input.
787
788 .. versionadded:: 3.9
789
Victor Stinner331a6a52019-05-27 16:39:22 +0200790 .. c:member:: wchar_t* pythonpath_env
791
Victor Stinner4b9aad42020-11-02 16:49:54 +0100792 Module search paths (:data:`sys.path`) as a string separated by ``DELIM``
Victor Stinner331a6a52019-05-27 16:39:22 +0200793 (:data:`os.path.pathsep`).
794
Victor Stinner4b9aad42020-11-02 16:49:54 +0100795 Set by the :envvar:`PYTHONPATH` environment variable.
796
797 Default: ``NULL``.
798
799 Part of the :ref:`Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200800
801 .. c:member:: PyWideStringList module_search_paths
802 .. c:member:: int module_search_paths_set
803
Victor Stinner4b9aad42020-11-02 16:49:54 +0100804 Module search paths: :data:`sys.path`.
805
806 If :c:member:`~PyConfig.module_search_paths_set` is equal to 0, the
807 function calculating the :ref:`Path Configuration <init-path-config>`
808 overrides the :c:member:`~PyConfig.module_search_paths` and sets
809 :c:member:`~PyConfig.module_search_paths_set` to ``1``.
810
811 Default: empty list (``module_search_paths``) and ``0``
812 (``module_search_paths_set``).
813
814 Part of the :ref:`Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200815
816 .. c:member:: int optimization_level
817
818 Compilation optimization level:
819
Victor Stinner4b9aad42020-11-02 16:49:54 +0100820 * ``0``: Peephole optimizer, set ``__debug__`` to ``True``.
821 * ``1``: Level 0, remove assertions, set ``__debug__`` to ``False``.
822 * ``2``: Level 1, strip docstrings.
823
824 Incremented by the :option:`-O` command line option. Set to the
825 :envvar:`PYTHONOPTIMIZE` environment variable value.
826
827 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200828
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200829 .. c:member:: PyWideStringList orig_argv
830
831 The list of the original command line arguments passed to the Python
Victor Stinner4b9aad42020-11-02 16:49:54 +0100832 executable: :data:`sys.orig_argv`.
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200833
834 If :c:member:`~PyConfig.orig_argv` list is empty and
835 :c:member:`~PyConfig.argv` is not a list only containing an empty
836 string, :c:func:`PyConfig_Read()` copies :c:member:`~PyConfig.argv` into
837 :c:member:`~PyConfig.orig_argv` before modifying
838 :c:member:`~PyConfig.argv` (if :c:member:`~PyConfig.parse_argv` is
839 non-zero).
840
841 See also the :c:member:`~PyConfig.argv` member and the
842 :c:func:`Py_GetArgcArgv` function.
843
Victor Stinner4b9aad42020-11-02 16:49:54 +0100844 Default: empty list.
845
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200846 .. versionadded:: 3.10
847
Victor Stinner331a6a52019-05-27 16:39:22 +0200848 .. c:member:: int parse_argv
849
Victor Stinner4b9aad42020-11-02 16:49:54 +0100850 Parse command line arguments?
851
Victor Stinner331a6a52019-05-27 16:39:22 +0200852 If non-zero, parse :c:member:`~PyConfig.argv` the same way the regular
Victor Stinner4b9aad42020-11-02 16:49:54 +0100853 Python parses :ref:`command line arguments <using-on-cmdline>`, and strip
854 Python arguments from :c:member:`~PyConfig.argv`.
855
856 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200857
858 .. c:member:: int parser_debug
859
Victor Stinner4b9aad42020-11-02 16:49:54 +0100860 Parser debug mode. If greater than 0, turn on parser debugging output (for expert only, depending
Victor Stinner331a6a52019-05-27 16:39:22 +0200861 on compilation options).
862
Victor Stinner4b9aad42020-11-02 16:49:54 +0100863 Incremented by the :option:`-d` command line option. Set to the
864 :envvar:`PYTHONDEBUG` environment variable value.
865
866 Default: ``0``.
867
Victor Stinner331a6a52019-05-27 16:39:22 +0200868 .. c:member:: int pathconfig_warnings
869
Victor Stinner4b9aad42020-11-02 16:49:54 +0100870 On Unix, if non-zero, calculating the :ref:`Path Configuration
871 <init-path-config>` can log warnings into ``stderr``. If equals to 0,
872 suppress these warnings.
873
874 It has no effect on Windows.
875
876 Default: ``1`` in Python mode, ``0`` in isolated mode.
877
878 Part of the :ref:`Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200879
880 .. c:member:: wchar_t* prefix
881
Victor Stinner4b9aad42020-11-02 16:49:54 +0100882 The site-specific directory prefix where the platform independent Python
883 files are installed: :data:`sys.prefix`.
884
885 Default: ``NULL``.
886
887 Part of the :ref:`Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200888
889 .. c:member:: wchar_t* program_name
890
Victor Stinner4b9aad42020-11-02 16:49:54 +0100891 Program name used to initialize :c:member:`~PyConfig.executable` and in
892 early error messages during Python initialization.
893
894 * If :func:`Py_SetProgramName` has been called, use its argument.
895 * On macOS, use :envvar:`PYTHONEXECUTABLE` environment variable if set.
896 * If the ``WITH_NEXT_FRAMEWORK`` macro is defined, use
897 :envvar:`__PYVENV_LAUNCHER__` environment variable if set.
898 * Use ``argv[0]`` of :c:member:`~PyConfig.argv` if available and
899 non-empty.
900 * Otherwise, use ``L"python"`` on Windows, or ``L"python3"`` on other
901 platforms.
902
903 Default: ``NULL``.
904
905 Part of the :ref:`Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200906
907 .. c:member:: wchar_t* pycache_prefix
908
Victor Stinner4b9aad42020-11-02 16:49:54 +0100909 Directory where cached ``.pyc`` files are written:
910 :data:`sys.pycache_prefix`.
911
912 Set by the :option:`-X pycache_prefix=PATH <-X>` command line option and
913 the :envvar:`PYTHONPYCACHEPREFIX` environment variable.
Victor Stinner88feaec2019-09-26 03:15:07 +0200914
Serhiy Storchakae835b312019-10-30 21:37:16 +0200915 If ``NULL``, :data:`sys.pycache_prefix` is set to ``None``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200916
Victor Stinner4b9aad42020-11-02 16:49:54 +0100917 Default: ``NULL``.
918
Victor Stinner331a6a52019-05-27 16:39:22 +0200919 .. c:member:: int quiet
920
Victor Stinner4b9aad42020-11-02 16:49:54 +0100921 Quiet mode. If greater than 0, don't display the copyright and version at
922 Python startup in interactive mode.
923
924 Incremented by the :option:`-q` command line option.
925
926 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200927
928 .. c:member:: wchar_t* run_command
929
Victor Stinner4b9aad42020-11-02 16:49:54 +0100930 Value of the :option:`-c` command line option.
931
932 Used by :c:func:`Py_RunMain`.
933
934 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200935
936 .. c:member:: wchar_t* run_filename
937
Victor Stinner4b9aad42020-11-02 16:49:54 +0100938 Filename passed on the command line: trailing command line argument
939 without :option:`-c` or :option:`-m`.
940
941 For example, it is set to ``script.py`` by the ``python3 script.py arg``
942 command.
943
944 Used by :c:func:`Py_RunMain`.
945
946 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200947
948 .. c:member:: wchar_t* run_module
949
Victor Stinner4b9aad42020-11-02 16:49:54 +0100950 Value of the :option:`-m` command line option.
951
952 Used by :c:func:`Py_RunMain`.
953
954 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200955
Victor Stinner331a6a52019-05-27 16:39:22 +0200956 .. c:member:: int show_ref_count
957
958 Show total reference count at exit?
959
Victor Stinner88feaec2019-09-26 03:15:07 +0200960 Set to 1 by :option:`-X showrefcount <-X>` command line option.
961
Victor Stinner331a6a52019-05-27 16:39:22 +0200962 Need a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
963
Victor Stinner4b9aad42020-11-02 16:49:54 +0100964 Default: ``0``.
965
Victor Stinner331a6a52019-05-27 16:39:22 +0200966 .. c:member:: int site_import
967
968 Import the :mod:`site` module at startup?
969
Victor Stinner4b9aad42020-11-02 16:49:54 +0100970 If equal to zero, disable the import of the module site and the
971 site-dependent manipulations of :data:`sys.path` that it entails.
972
973 Also disable these manipulations if the :mod:`site` module is explicitly
974 imported later (call :func:`site.main` if you want them to be triggered).
975
976 Set to ``0`` by the :option:`-S` command line option.
977
978 :data:`sys.flags.no_site` is set to the inverted value of
979 :c:member:`~PyConfig.site_import`.
980
981 Default: ``1``.
982
Victor Stinner331a6a52019-05-27 16:39:22 +0200983 .. c:member:: int skip_source_first_line
984
Victor Stinner4b9aad42020-11-02 16:49:54 +0100985 If non-zero, skip the first line of the :c:member:`PyConfig.run_filename`
986 source.
987
988 It allows the usage of non-Unix forms of ``#!cmd``. This is intended for
989 a DOS specific hack only.
990
991 Set to ``1`` by the :option:`-x` command line option.
992
993 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200994
995 .. c:member:: wchar_t* stdio_encoding
996 .. c:member:: wchar_t* stdio_errors
997
998 Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
Victor Stinner4b9aad42020-11-02 16:49:54 +0100999 :data:`sys.stderr` (but :data:`sys.stderr` always uses
1000 ``"backslashreplace"`` error handler).
1001
1002 If :c:func:`Py_SetStandardStreamEncoding` has been called, use its
1003 *error* and *errors* arguments if they are not ``NULL``.
1004
1005 Use the :envvar:`PYTHONIOENCODING` environment variable if it is
1006 non-empty.
1007
1008 Default encoding:
1009
1010 * ``"UTF-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero.
1011 * Otherwise, use the :term:`locale encoding`.
1012
1013 Default error handler:
1014
1015 * On Windows: use ``"surrogateescape"``.
1016 * ``"surrogateescape"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero,
1017 or if the LC_CTYPE locale is "C" or "POSIX".
1018 * ``"strict"`` otherwise.
Victor Stinner331a6a52019-05-27 16:39:22 +02001019
1020 .. c:member:: int tracemalloc
1021
Victor Stinner4b9aad42020-11-02 16:49:54 +01001022 Enable tracemalloc?
1023
Victor Stinner88feaec2019-09-26 03:15:07 +02001024 If non-zero, call :func:`tracemalloc.start` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +02001025
Victor Stinner4b9aad42020-11-02 16:49:54 +01001026 Set by :option:`-X tracemalloc=N <-X>` command line option and by the
1027 :envvar:`PYTHONTRACEMALLOC` environment variable.
1028
1029 Default: ``-1`` in Python mode, ``0`` in isolated mode.
1030
Victor Stinner331a6a52019-05-27 16:39:22 +02001031 .. c:member:: int use_environment
1032
Victor Stinner4b9aad42020-11-02 16:49:54 +01001033 Use :ref:`environment variables <using-on-envvars>`?
1034
1035 If equals to zero, ignore the :ref:`environment variables
1036 <using-on-envvars>`.
1037
1038 Default: ``1`` in Python config and ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +02001039
1040 .. c:member:: int user_site_directory
1041
Victor Stinner4b9aad42020-11-02 16:49:54 +01001042 If non-zero, add the user site directory to :data:`sys.path`.
1043
1044 Set to ``0`` by the :option:`-s` and :option:`-I` command line options.
1045
1046 Set to ``0`` by the :envvar:`PYTHONNOUSERSITE` environment variable.
1047
1048 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +02001049
1050 .. c:member:: int verbose
1051
Victor Stinner4b9aad42020-11-02 16:49:54 +01001052 Verbose mode. If greater than 0, print a message each time a module is
1053 imported, showing the place (filename or built-in module) from which
1054 it is loaded.
1055
1056 If greater or equal to 2, print a message for each file that is checked
1057 for when searching for a module. Also provides information on module
1058 cleanup at exit.
1059
1060 Incremented by the :option:`-v` command line option.
1061
1062 Set to the :envvar:`PYTHONVERBOSE` environment variable value.
1063
1064 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +02001065
1066 .. c:member:: PyWideStringList warnoptions
1067
Victor Stinner4b9aad42020-11-02 16:49:54 +01001068 Options of the :mod:`warnings` module to build warnings filters, lowest
1069 to highest priority: :data:`sys.warnoptions`.
Victor Stinnerfb4ae152019-09-30 01:40:17 +02001070
1071 The :mod:`warnings` module adds :data:`sys.warnoptions` in the reverse
1072 order: the last :c:member:`PyConfig.warnoptions` item becomes the first
1073 item of :data:`warnings.filters` which is checked first (highest
1074 priority).
Victor Stinner331a6a52019-05-27 16:39:22 +02001075
Victor Stinner4b9aad42020-11-02 16:49:54 +01001076 Default: empty list.
1077
Victor Stinner331a6a52019-05-27 16:39:22 +02001078 .. c:member:: int write_bytecode
1079
Victor Stinner4b9aad42020-11-02 16:49:54 +01001080 If equal to 0, Python won't try to write ``.pyc`` files on the import of
1081 source modules.
1082
1083 Set to ``0`` by the :option:`-B` command line option and the
1084 :envvar:`PYTHONDONTWRITEBYTECODE` environment variable.
Victor Stinner331a6a52019-05-27 16:39:22 +02001085
Victor Stinner88feaec2019-09-26 03:15:07 +02001086 :data:`sys.dont_write_bytecode` is initialized to the inverted value of
1087 :c:member:`~PyConfig.write_bytecode`.
1088
Victor Stinner4b9aad42020-11-02 16:49:54 +01001089 Default: ``1``.
1090
Victor Stinner331a6a52019-05-27 16:39:22 +02001091 .. c:member:: PyWideStringList xoptions
1092
Victor Stinner4b9aad42020-11-02 16:49:54 +01001093 Values of the :option:`-X` command line options: :data:`sys._xoptions`.
Victor Stinner331a6a52019-05-27 16:39:22 +02001094
Victor Stinner4b9aad42020-11-02 16:49:54 +01001095 Default: empty list.
Victor Stinner331a6a52019-05-27 16:39:22 +02001096
Victor Stinner4b9aad42020-11-02 16:49:54 +01001097If :c:member:`~PyConfig.parse_argv` is non-zero, :c:member:`~PyConfig.argv`
1098arguments are parsed the same way the regular Python parses :ref:`command line
1099arguments <using-on-cmdline>`, and Python arguments are stripped from
1100:c:member:`~PyConfig.argv`.
1101
1102The :c:member:`~PyConfig.xoptions` options are parsed to set other options: see
1103the :option:`-X` command line option.
Victor Stinner331a6a52019-05-27 16:39:22 +02001104
Victor Stinnerc6e5c112020-02-03 15:17:15 +01001105.. versionchanged:: 3.9
1106
1107 The ``show_alloc_count`` field has been removed.
1108
Victor Stinner331a6a52019-05-27 16:39:22 +02001109
1110Initialization with PyConfig
1111----------------------------
1112
1113Function to initialize Python:
1114
1115.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
1116
1117 Initialize Python from *config* configuration.
1118
1119The caller is responsible to handle exceptions (error or exit) using
1120:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
1121
Victor Stinner4b9aad42020-11-02 16:49:54 +01001122If :c:func:`PyImport_FrozenModules`, :c:func:`PyImport_AppendInittab` or
1123:c:func:`PyImport_ExtendInittab` are used, they must be set or called after
1124Python preinitialization and before the Python initialization.
Victor Stinner331a6a52019-05-27 16:39:22 +02001125
1126Example setting the program name::
1127
1128 void init_python(void)
1129 {
1130 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +02001131
Victor Stinner8462a492019-10-01 12:06:16 +02001132 PyConfig config;
1133 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001134
1135 /* Set the program name. Implicitly preinitialize Python. */
1136 status = PyConfig_SetString(&config, &config.program_name,
1137 L"/path/to/my_program");
1138 if (PyStatus_Exception(status)) {
1139 goto fail;
1140 }
1141
1142 status = Py_InitializeFromConfig(&config);
1143 if (PyStatus_Exception(status)) {
1144 goto fail;
1145 }
1146 PyConfig_Clear(&config);
1147 return;
1148
1149 fail:
1150 PyConfig_Clear(&config);
1151 Py_ExitStatusException(status);
1152 }
1153
1154More complete example modifying the default configuration, read the
1155configuration, and then override some parameters::
1156
1157 PyStatus init_python(const char *program_name)
1158 {
1159 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +02001160
Victor Stinner8462a492019-10-01 12:06:16 +02001161 PyConfig config;
1162 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001163
Gurupad Hegde6c7bb382019-12-28 17:16:02 -05001164 /* Set the program name before reading the configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001165 (decode byte string from the locale encoding).
1166
1167 Implicitly preinitialize Python. */
1168 status = PyConfig_SetBytesString(&config, &config.program_name,
Victor Stinner4b9aad42020-11-02 16:49:54 +01001169 program_name);
Victor Stinner331a6a52019-05-27 16:39:22 +02001170 if (PyStatus_Exception(status)) {
1171 goto done;
1172 }
1173
1174 /* Read all configuration at once */
1175 status = PyConfig_Read(&config);
1176 if (PyStatus_Exception(status)) {
1177 goto done;
1178 }
1179
1180 /* Append our custom search path to sys.path */
1181 status = PyWideStringList_Append(&config.module_search_paths,
Victor Stinner88feaec2019-09-26 03:15:07 +02001182 L"/path/to/more/modules");
Victor Stinner331a6a52019-05-27 16:39:22 +02001183 if (PyStatus_Exception(status)) {
1184 goto done;
1185 }
1186
1187 /* Override executable computed by PyConfig_Read() */
1188 status = PyConfig_SetString(&config, &config.executable,
1189 L"/path/to/my_executable");
1190 if (PyStatus_Exception(status)) {
1191 goto done;
1192 }
1193
1194 status = Py_InitializeFromConfig(&config);
1195
1196 done:
1197 PyConfig_Clear(&config);
1198 return status;
1199 }
1200
1201
1202.. _init-isolated-conf:
1203
1204Isolated Configuration
1205----------------------
1206
1207:c:func:`PyPreConfig_InitIsolatedConfig` and
1208:c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to
1209isolate Python from the system. For example, to embed Python into an
1210application.
1211
1212This configuration ignores global configuration variables, environments
Victor Stinner88feaec2019-09-26 03:15:07 +02001213variables, command line arguments (:c:member:`PyConfig.argv` is not parsed)
1214and user site directory. The C standard streams (ex: ``stdout``) and the
1215LC_CTYPE locale are left unchanged. Signal handlers are not installed.
Victor Stinner331a6a52019-05-27 16:39:22 +02001216
1217Configuration files are still used with this configuration. Set the
1218:ref:`Path Configuration <init-path-config>` ("output fields") to ignore these
1219configuration files and avoid the function computing the default path
1220configuration.
1221
1222
1223.. _init-python-config:
1224
1225Python Configuration
1226--------------------
1227
1228:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
1229functions create a configuration to build a customized Python which behaves as
1230the regular Python.
1231
1232Environments variables and command line arguments are used to configure
1233Python, whereas global configuration variables are ignored.
1234
Victor Stinner4b9aad42020-11-02 16:49:54 +01001235This function enables C locale coercion (:pep:`538`)
1236and :ref:`Python UTF-8 Mode <utf8-mode>`
Victor Stinner331a6a52019-05-27 16:39:22 +02001237(:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and
1238:envvar:`PYTHONCOERCECLOCALE` environment variables.
1239
1240Example of customized Python always running in isolated mode::
1241
1242 int main(int argc, char **argv)
1243 {
Victor Stinner331a6a52019-05-27 16:39:22 +02001244 PyStatus status;
Victor Stinner8462a492019-10-01 12:06:16 +02001245
Victor Stinner441b10c2019-09-28 04:28:35 +02001246 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001247 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001248 config.isolated = 1;
1249
1250 /* Decode command line arguments.
1251 Implicitly preinitialize Python (in isolated mode). */
1252 status = PyConfig_SetBytesArgv(&config, argc, argv);
1253 if (PyStatus_Exception(status)) {
1254 goto fail;
1255 }
1256
1257 status = Py_InitializeFromConfig(&config);
1258 if (PyStatus_Exception(status)) {
1259 goto fail;
1260 }
1261 PyConfig_Clear(&config);
1262
1263 return Py_RunMain();
1264
1265 fail:
1266 PyConfig_Clear(&config);
1267 if (PyStatus_IsExit(status)) {
1268 return status.exitcode;
1269 }
1270 /* Display the error message and exit the process with
1271 non-zero exit code */
1272 Py_ExitStatusException(status);
1273 }
1274
1275
1276.. _init-path-config:
1277
1278Path Configuration
1279------------------
1280
1281:c:type:`PyConfig` contains multiple fields for the path configuration:
1282
Victor Stinner8bf39b62019-09-26 02:22:35 +02001283* Path configuration inputs:
Victor Stinner331a6a52019-05-27 16:39:22 +02001284
1285 * :c:member:`PyConfig.home`
Sandro Mani8f023a22020-06-08 17:28:11 +02001286 * :c:member:`PyConfig.platlibdir`
Victor Stinner331a6a52019-05-27 16:39:22 +02001287 * :c:member:`PyConfig.pathconfig_warnings`
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001288 * :c:member:`PyConfig.program_name`
1289 * :c:member:`PyConfig.pythonpath_env`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001290 * current working directory: to get absolute paths
1291 * ``PATH`` environment variable to get the program full path
1292 (from :c:member:`PyConfig.program_name`)
1293 * ``__PYVENV_LAUNCHER__`` environment variable
1294 * (Windows only) Application paths in the registry under
1295 "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and
1296 HKEY_LOCAL_MACHINE (where X.Y is the Python version).
Victor Stinner331a6a52019-05-27 16:39:22 +02001297
1298* Path configuration output fields:
1299
Victor Stinner8bf39b62019-09-26 02:22:35 +02001300 * :c:member:`PyConfig.base_exec_prefix`
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001301 * :c:member:`PyConfig.base_executable`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001302 * :c:member:`PyConfig.base_prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +02001303 * :c:member:`PyConfig.exec_prefix`
1304 * :c:member:`PyConfig.executable`
Victor Stinner331a6a52019-05-27 16:39:22 +02001305 * :c:member:`PyConfig.module_search_paths_set`,
1306 :c:member:`PyConfig.module_search_paths`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001307 * :c:member:`PyConfig.prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +02001308
Victor Stinner8bf39b62019-09-26 02:22:35 +02001309If at least one "output field" is not set, Python calculates the path
Victor Stinner331a6a52019-05-27 16:39:22 +02001310configuration to fill unset fields. If
1311:c:member:`~PyConfig.module_search_paths_set` is equal to 0,
Min ho Kim39d87b52019-08-31 06:21:19 +10001312:c:member:`~PyConfig.module_search_paths` is overridden and
Victor Stinner331a6a52019-05-27 16:39:22 +02001313:c:member:`~PyConfig.module_search_paths_set` is set to 1.
1314
Victor Stinner8bf39b62019-09-26 02:22:35 +02001315It is possible to completely ignore the function calculating the default
Victor Stinner331a6a52019-05-27 16:39:22 +02001316path configuration by setting explicitly all path configuration output
1317fields listed above. A string is considered as set even if it is non-empty.
1318``module_search_paths`` is considered as set if
1319``module_search_paths_set`` is set to 1. In this case, path
1320configuration input fields are ignored as well.
1321
1322Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when
Victor Stinner8bf39b62019-09-26 02:22:35 +02001323calculating the path configuration (Unix only, Windows does not log any warning).
Victor Stinner331a6a52019-05-27 16:39:22 +02001324
1325If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix`
1326fields are not set, they inherit their value from :c:member:`~PyConfig.prefix`
1327and :c:member:`~PyConfig.exec_prefix` respectively.
1328
1329:c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`:
1330
1331* If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a
1332 ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to
1333 :data:`sys.path`.
1334* If :c:member:`~PyConfig.isolated` is zero:
1335
1336 * If :c:member:`~PyConfig.run_module` is set, prepend the current directory
1337 to :data:`sys.path`. Do nothing if the current directory cannot be read.
1338 * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the
1339 filename to :data:`sys.path`.
1340 * Otherwise, prepend an empty string to :data:`sys.path`.
1341
1342If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be
1343modified by the :mod:`site` module. If
1344:c:member:`~PyConfig.user_site_directory` is non-zero and the user's
1345site-package directory exists, the :mod:`site` module appends the user's
1346site-package directory to :data:`sys.path`.
1347
1348The following configuration files are used by the path configuration:
1349
1350* ``pyvenv.cfg``
1351* ``python._pth`` (Windows only)
1352* ``pybuilddir.txt`` (Unix only)
1353
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001354The ``__PYVENV_LAUNCHER__`` environment variable is used to set
1355:c:member:`PyConfig.base_executable`
1356
Victor Stinner331a6a52019-05-27 16:39:22 +02001357
1358Py_RunMain()
1359------------
1360
1361.. c:function:: int Py_RunMain(void)
1362
1363 Execute the command (:c:member:`PyConfig.run_command`), the script
1364 (:c:member:`PyConfig.run_filename`) or the module
1365 (:c:member:`PyConfig.run_module`) specified on the command line or in the
1366 configuration.
1367
1368 By default and when if :option:`-i` option is used, run the REPL.
1369
1370 Finally, finalizes Python and returns an exit status that can be passed to
1371 the ``exit()`` function.
1372
1373See :ref:`Python Configuration <init-python-config>` for an example of
1374customized Python always running in isolated mode using
1375:c:func:`Py_RunMain`.
1376
1377
Victor Stinnere81f6e62020-06-08 18:12:59 +02001378Py_GetArgcArgv()
1379----------------
1380
1381.. c:function:: void Py_GetArgcArgv(int *argc, wchar_t ***argv)
1382
1383 Get the original command line arguments, before Python modified them.
1384
Victor Stinnerdd8a93e2020-06-30 00:49:03 +02001385 See also :c:member:`PyConfig.orig_argv` member.
1386
Victor Stinnere81f6e62020-06-08 18:12:59 +02001387
Victor Stinner331a6a52019-05-27 16:39:22 +02001388Multi-Phase Initialization Private Provisional API
1389--------------------------------------------------
1390
1391This section is a private provisional API introducing multi-phase
1392initialization, the core feature of the :pep:`432`:
1393
1394* "Core" initialization phase, "bare minimum Python":
1395
1396 * Builtin types;
1397 * Builtin exceptions;
1398 * Builtin and frozen modules;
1399 * The :mod:`sys` module is only partially initialized
Victor Stinner88feaec2019-09-26 03:15:07 +02001400 (ex: :data:`sys.path` doesn't exist yet).
Victor Stinner331a6a52019-05-27 16:39:22 +02001401
1402* "Main" initialization phase, Python is fully initialized:
1403
1404 * Install and configure :mod:`importlib`;
1405 * Apply the :ref:`Path Configuration <init-path-config>`;
1406 * Install signal handlers;
1407 * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout`
1408 and :data:`sys.path`);
1409 * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`;
1410 * Import the :mod:`site` module;
1411 * etc.
1412
1413Private provisional API:
1414
1415* :c:member:`PyConfig._init_main`: if set to 0,
1416 :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase.
Victor Stinner252346a2020-05-01 11:33:44 +02001417* :c:member:`PyConfig._isolated_interpreter`: if non-zero,
1418 disallow threads, subprocesses and fork.
Victor Stinner331a6a52019-05-27 16:39:22 +02001419
1420.. c:function:: PyStatus _Py_InitializeMain(void)
1421
1422 Move to the "Main" initialization phase, finish the Python initialization.
1423
1424No module is imported during the "Core" phase and the ``importlib`` module is
1425not configured: the :ref:`Path Configuration <init-path-config>` is only
1426applied during the "Main" phase. It may allow to customize Python in Python to
1427override or tune the :ref:`Path Configuration <init-path-config>`, maybe
Victor Stinner88feaec2019-09-26 03:15:07 +02001428install a custom :data:`sys.meta_path` importer or an import hook, etc.
Victor Stinner331a6a52019-05-27 16:39:22 +02001429
Victor Stinner88feaec2019-09-26 03:15:07 +02001430It may become possible to calculatin the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001431<init-path-config>` in Python, after the Core phase and before the Main phase,
1432which is one of the :pep:`432` motivation.
1433
1434The "Core" phase is not properly defined: what should be and what should
1435not be available at this phase is not specified yet. The API is marked
1436as private and provisional: the API can be modified or even be removed
1437anytime until a proper public API is designed.
1438
1439Example running Python code between "Core" and "Main" initialization
1440phases::
1441
1442 void init_python(void)
1443 {
1444 PyStatus status;
Victor Stinner8462a492019-10-01 12:06:16 +02001445
Victor Stinner331a6a52019-05-27 16:39:22 +02001446 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001447 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001448 config._init_main = 0;
1449
1450 /* ... customize 'config' configuration ... */
1451
1452 status = Py_InitializeFromConfig(&config);
1453 PyConfig_Clear(&config);
1454 if (PyStatus_Exception(status)) {
1455 Py_ExitStatusException(status);
1456 }
1457
1458 /* Use sys.stderr because sys.stdout is only created
1459 by _Py_InitializeMain() */
1460 int res = PyRun_SimpleString(
1461 "import sys; "
1462 "print('Run Python code before _Py_InitializeMain', "
1463 "file=sys.stderr)");
1464 if (res < 0) {
1465 exit(1);
1466 }
1467
1468 /* ... put more configuration code here ... */
1469
1470 status = _Py_InitializeMain();
1471 if (PyStatus_Exception(status)) {
1472 Py_ExitStatusException(status);
1473 }
1474 }