blob: dad1f90bea548b94182a0b9003d26ad68c43970a [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
131 .. c:function:: PyStatus PyStatus_NoMemory(void)
132
133 Memory allocation failure (out of memory).
134
135 .. c:function:: PyStatus PyStatus_Exit(int exitcode)
136
137 Exit Python with the specified exit code.
138
139 Functions to handle a status:
140
141 .. c:function:: int PyStatus_Exception(PyStatus status)
142
143 Is the status an error or an exit? If true, the exception must be
144 handled; by calling :c:func:`Py_ExitStatusException` for example.
145
146 .. c:function:: int PyStatus_IsError(PyStatus status)
147
148 Is the result an error?
149
150 .. c:function:: int PyStatus_IsExit(PyStatus status)
151
152 Is the result an exit?
153
154 .. c:function:: void Py_ExitStatusException(PyStatus status)
155
156 Call ``exit(exitcode)`` if *status* is an exit. Print the error
157 message and exit with a non-zero exit code if *status* is an error. Must
158 only be called if ``PyStatus_Exception(status)`` is non-zero.
159
160.. note::
161 Internally, Python uses macros which set ``PyStatus.func``,
162 whereas functions to create a status set ``func`` to ``NULL``.
163
164Example::
165
166 PyStatus alloc(void **ptr, size_t size)
167 {
168 *ptr = PyMem_RawMalloc(size);
169 if (*ptr == NULL) {
170 return PyStatus_NoMemory();
171 }
172 return PyStatus_Ok();
173 }
174
175 int main(int argc, char **argv)
176 {
177 void *ptr;
178 PyStatus status = alloc(&ptr, 16);
179 if (PyStatus_Exception(status)) {
180 Py_ExitStatusException(status);
181 }
182 PyMem_Free(ptr);
183 return 0;
184 }
185
186
187PyPreConfig
188-----------
189
190.. c:type:: PyPreConfig
191
Victor Stinner4b9aad42020-11-02 16:49:54 +0100192 Structure used to preinitialize Python.
Victor Stinner331a6a52019-05-27 16:39:22 +0200193
194 Function to initialize a preconfiguration:
195
tomerv741008a2020-07-01 12:32:54 +0300196 .. c:function:: void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200197
198 Initialize the preconfiguration with :ref:`Python Configuration
199 <init-python-config>`.
200
tomerv741008a2020-07-01 12:32:54 +0300201 .. c:function:: void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200202
203 Initialize the preconfiguration with :ref:`Isolated Configuration
204 <init-isolated-conf>`.
205
206 Structure fields:
207
208 .. c:member:: int allocator
209
Victor Stinner4b9aad42020-11-02 16:49:54 +0100210 Name of the Python memory allocators:
Victor Stinner331a6a52019-05-27 16:39:22 +0200211
212 * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators
213 (use defaults)
214 * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators
215 * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with
216 debug hooks
217 * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()``
218 * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of
219 ``malloc()`` with debug hooks
220 * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory
221 allocator <pymalloc>`
222 * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc
223 memory allocator <pymalloc>` with debug hooks
224
225 ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG``
226 are not supported if Python is configured using ``--without-pymalloc``
227
228 See :ref:`Memory Management <memory>`.
229
Victor Stinner4b9aad42020-11-02 16:49:54 +0100230 Default: ``PYMEM_ALLOCATOR_NOT_SET``.
231
Victor Stinner331a6a52019-05-27 16:39:22 +0200232 .. c:member:: int configure_locale
233
Victor Stinner4b9aad42020-11-02 16:49:54 +0100234 Set the LC_CTYPE locale to the user preferred locale?
235
236 If equals to 0, set :c:member:`~PyPreConfig.coerce_c_locale` and
237 :c:member:`~PyPreConfig.coerce_c_locale_warn` members to 0.
238
239 See the :term:`locale encoding`.
240
241 Default: ``1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200242
243 .. c:member:: int coerce_c_locale
244
Victor Stinner4b9aad42020-11-02 16:49:54 +0100245 If equals to 2, coerce the C locale.
246
247 If equals to 1, read the LC_CTYPE locale to decide if it should be
248 coerced.
249
250 See the :term:`locale encoding`.
251
252 Default: ``-1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200253
254 .. c:member:: int coerce_c_locale_warn
Victor Stinner88feaec2019-09-26 03:15:07 +0200255
Victor Stinner331a6a52019-05-27 16:39:22 +0200256 If non-zero, emit a warning if the C locale is coerced.
257
Victor Stinner4b9aad42020-11-02 16:49:54 +0100258 Default: ``-1`` in Python config, ``0`` in isolated config.
259
Victor Stinner331a6a52019-05-27 16:39:22 +0200260 .. c:member:: int dev_mode
261
Victor Stinner4b9aad42020-11-02 16:49:54 +0100262 If non-zero, enables the :ref:`Python Development Mode <devmode>`:
263 see :c:member:`PyConfig.dev_mode`.
264
265 Default: ``-1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200266
267 .. c:member:: int isolated
268
Victor Stinner4b9aad42020-11-02 16:49:54 +0100269 Isolated mode: see :c:member:`PyConfig.isolated`.
270
271 Default: ``0`` in Python mode, ``1`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200272
Victor Stinnere662c392020-11-01 23:07:23 +0100273 .. c:member:: int legacy_windows_fs_encoding
Victor Stinner331a6a52019-05-27 16:39:22 +0200274
Victor Stinnere662c392020-11-01 23:07:23 +0100275 If non-zero:
276
277 * Set :c:member:`PyPreConfig.utf8_mode` to ``0``,
278 * Set :c:member:`PyConfig.filesystem_encoding` to ``"mbcs"``,
279 * Set :c:member:`PyConfig.filesystem_errors` to ``"replace"``.
280
281 Initialized the from :envvar:`PYTHONLEGACYWINDOWSFSENCODING` environment
282 variable value.
Victor Stinner331a6a52019-05-27 16:39:22 +0200283
284 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
285 Windows specific code.
286
Victor Stinner4b9aad42020-11-02 16:49:54 +0100287 Default: ``0``.
288
Victor Stinner331a6a52019-05-27 16:39:22 +0200289 .. c:member:: int parse_argv
290
291 If non-zero, :c:func:`Py_PreInitializeFromArgs` and
292 :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the
293 same way the regular Python parses command line arguments: see
294 :ref:`Command Line Arguments <using-on-cmdline>`.
295
Victor Stinner4b9aad42020-11-02 16:49:54 +0100296 Default: ``1`` in Python config, ``0`` in isolated config.
297
Victor Stinner331a6a52019-05-27 16:39:22 +0200298 .. c:member:: int use_environment
299
Victor Stinner4b9aad42020-11-02 16:49:54 +0100300 Use :ref:`environment variables <using-on-envvars>`? See
301 :c:member:`PyConfig.use_environment`.
302
303 Default: ``1`` in Python config and ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200304
305 .. c:member:: int utf8_mode
306
Victor Stinner4b9aad42020-11-02 16:49:54 +0100307 If non-zero, enable the :ref:`Python UTF-8 Mode <utf8-mode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200308
Victor Stinner4b9aad42020-11-02 16:49:54 +0100309 Set by the :option:`-X utf8 <-X>` command line option and the
310 :envvar:`PYTHONUTF8` environment variable.
311
312 Default: ``-1`` in Python config and ``0`` in isolated config.
313
314
315.. _c-preinit:
316
317Preinitialize Python with PyPreConfig
318-------------------------------------
319
320The preinitialization of Python:
321
322* Set the Python memory allocators (:c:member:`PyPreConfig.allocator`)
323* Configure the LC_CTYPE locale (:term:`locale encoding`)
324* Set the :ref:`Python UTF-8 Mode <utf8-mode>`
325 (:c:member:`PyPreConfig.utf8_mode`)
Victor Stinner331a6a52019-05-27 16:39:22 +0200326
327Functions to preinitialize Python:
328
329.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
330
331 Preinitialize Python from *preconfig* preconfiguration.
332
333.. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv)
334
Victor Stinner4b9aad42020-11-02 16:49:54 +0100335 Preinitialize Python from *preconfig* preconfiguration.
336
337 Parse *argv* command line arguments (bytes strings) if
338 :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero.
Victor Stinner331a6a52019-05-27 16:39:22 +0200339
340.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)
341
Victor Stinner4b9aad42020-11-02 16:49:54 +0100342 Preinitialize Python from *preconfig* preconfiguration.
343
344 Parse *argv* command line arguments (wide strings) if
345 :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero.
Victor Stinner331a6a52019-05-27 16:39:22 +0200346
347The caller is responsible to handle exceptions (error or exit) using
348:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
349
350For :ref:`Python Configuration <init-python-config>`
351(:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with
352command line arguments, the command line arguments must also be passed to
353preinitialize Python, since they have an effect on the pre-configuration
Victor Stinner88feaec2019-09-26 03:15:07 +0200354like encodings. For example, the :option:`-X utf8 <-X>` command line option
Victor Stinner4b9aad42020-11-02 16:49:54 +0100355enables the :ref:`Python UTF-8 Mode <utf8-mode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200356
357``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and
358before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator.
359It can be called before :c:func:`Py_PreInitialize` if
360:c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``.
361
362Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be
Victor Stinner4b9aad42020-11-02 16:49:54 +0100363used before the Python preinitialization, whereas calling directly ``malloc()``
364and ``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called
365before the Python preinitialization.
Victor Stinner331a6a52019-05-27 16:39:22 +0200366
Victor Stinner4b9aad42020-11-02 16:49:54 +0100367Example using the preinitialization to enable
368the :ref:`Python UTF-8 Mode <utf8-mode>`::
Victor Stinner331a6a52019-05-27 16:39:22 +0200369
Victor Stinner441b10c2019-09-28 04:28:35 +0200370 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +0200371 PyPreConfig preconfig;
Victor Stinner3c30a762019-10-01 10:56:37 +0200372 PyPreConfig_InitPythonConfig(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200373
374 preconfig.utf8_mode = 1;
375
Victor Stinner441b10c2019-09-28 04:28:35 +0200376 status = Py_PreInitialize(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200377 if (PyStatus_Exception(status)) {
378 Py_ExitStatusException(status);
379 }
380
Victor Stinner4b9aad42020-11-02 16:49:54 +0100381 /* at this point, Python speaks UTF-8 */
Victor Stinner331a6a52019-05-27 16:39:22 +0200382
383 Py_Initialize();
384 /* ... use Python API here ... */
385 Py_Finalize();
386
387
388PyConfig
389--------
390
391.. c:type:: PyConfig
392
393 Structure containing most parameters to configure Python.
394
Victor Stinner4b9aad42020-11-02 16:49:54 +0100395 When done, the :c:func:`PyConfig_Clear` function must be used to release the
396 configuration memory.
397
Victor Stinner331a6a52019-05-27 16:39:22 +0200398 Structure methods:
399
Victor Stinner8462a492019-10-01 12:06:16 +0200400 .. c:function:: void PyConfig_InitPythonConfig(PyConfig *config)
Victor Stinner331a6a52019-05-27 16:39:22 +0200401
Victor Stinner4b9aad42020-11-02 16:49:54 +0100402 Initialize configuration with the :ref:`Python Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200403 <init-python-config>`.
404
Victor Stinner8462a492019-10-01 12:06:16 +0200405 .. c:function:: void PyConfig_InitIsolatedConfig(PyConfig *config)
Victor Stinner331a6a52019-05-27 16:39:22 +0200406
Victor Stinner4b9aad42020-11-02 16:49:54 +0100407 Initialize configuration with the :ref:`Isolated Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200408 <init-isolated-conf>`.
409
410 .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str)
411
412 Copy the wide character string *str* into ``*config_str``.
413
Victor Stinner4b9aad42020-11-02 16:49:54 +0100414 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200415
416 .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str)
417
Victor Stinner4b9aad42020-11-02 16:49:54 +0100418 Decode *str* using :c:func:`Py_DecodeLocale` and set the result into
419 ``*config_str``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200420
Victor Stinner4b9aad42020-11-02 16:49:54 +0100421 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200422
423 .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv)
424
Victor Stinner4b9aad42020-11-02 16:49:54 +0100425 Set command line arguments (:c:member:`~PyConfig.argv` member of
426 *config*) from the *argv* list of wide character strings.
Victor Stinner331a6a52019-05-27 16:39:22 +0200427
Victor Stinner4b9aad42020-11-02 16:49:54 +0100428 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200429
430 .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv)
431
Victor Stinner4b9aad42020-11-02 16:49:54 +0100432 Set command line arguments (:c:member:`~PyConfig.argv` member of
433 *config*) from the *argv* list of bytes strings. Decode bytes using
434 :c:func:`Py_DecodeLocale`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200435
Victor Stinner4b9aad42020-11-02 16:49:54 +0100436 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200437
Victor Stinner36242fd2019-07-01 19:13:50 +0200438 .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items)
439
440 Set the list of wide strings *list* to *length* and *items*.
441
Victor Stinner4b9aad42020-11-02 16:49:54 +0100442 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner36242fd2019-07-01 19:13:50 +0200443
Victor Stinner331a6a52019-05-27 16:39:22 +0200444 .. c:function:: PyStatus PyConfig_Read(PyConfig *config)
445
446 Read all Python configuration.
447
448 Fields which are already initialized are left unchanged.
449
Victor Stinner4b9aad42020-11-02 16:49:54 +0100450 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200451
452 .. c:function:: void PyConfig_Clear(PyConfig *config)
453
454 Release configuration memory.
455
Victor Stinner4b9aad42020-11-02 16:49:54 +0100456 Most ``PyConfig`` methods :ref:`preinitialize Python <c-preinit>` if needed.
457 In that case, the Python preinitialization configuration
458 (:c:type:`PyPreConfig`) in based on the :c:type:`PyConfig`. If configuration
459 fields which are in common with :c:type:`PyPreConfig` are tuned, they must
460 be set before calling a :c:type:`PyConfig` method:
Victor Stinner331a6a52019-05-27 16:39:22 +0200461
Victor Stinner4b9aad42020-11-02 16:49:54 +0100462 * :c:member:`PyConfig.dev_mode`
463 * :c:member:`PyConfig.isolated`
464 * :c:member:`PyConfig.parse_argv`
465 * :c:member:`PyConfig.use_environment`
Victor Stinner331a6a52019-05-27 16:39:22 +0200466
467 Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv`
Victor Stinner4b9aad42020-11-02 16:49:54 +0100468 is used, this method must be called before other methods, since the
Victor Stinner331a6a52019-05-27 16:39:22 +0200469 preinitialization configuration depends on command line arguments (if
470 :c:member:`parse_argv` is non-zero).
471
472 The caller of these methods is responsible to handle exceptions (error or
473 exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``.
474
475 Structure fields:
476
477 .. c:member:: PyWideStringList argv
478
Victor Stinner4b9aad42020-11-02 16:49:54 +0100479 Command line arguments: :data:`sys.argv`.
480
481 Set :c:member:`~PyConfig.parse_argv` to ``1`` to parse
482 :c:member:`~PyConfig.argv` the same way the regular Python parses Python
483 command line arguments and then to strip Python arguments from
484 :c:member:`~PyConfig.argv`.
485
486 If :c:member:`~PyConfig.argv` is empty, an empty string is added to
487 ensure that :data:`sys.argv` always exists and is never empty.
488
489 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200490
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200491 See also the :c:member:`~PyConfig.orig_argv` member.
492
Victor Stinner331a6a52019-05-27 16:39:22 +0200493 .. c:member:: wchar_t* base_exec_prefix
494
495 :data:`sys.base_exec_prefix`.
496
Victor Stinner4b9aad42020-11-02 16:49:54 +0100497 Default: ``NULL``.
498
499 Part of the :ref:`Path Configuration <init-path-config>` output.
500
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200501 .. c:member:: wchar_t* base_executable
502
Victor Stinner4b9aad42020-11-02 16:49:54 +0100503 Python base executable: :data:`sys._base_executable`.
504
505 Set by the :envvar:`__PYVENV_LAUNCHER__` environment variable.
506
507 Set from :c:member:`PyConfig.executable` if ``NULL``.
508
509 Default: ``NULL``.
510
511 Part of the :ref:`Path Configuration <init-path-config>` output.
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200512
Victor Stinner331a6a52019-05-27 16:39:22 +0200513 .. c:member:: wchar_t* base_prefix
514
515 :data:`sys.base_prefix`.
516
Victor Stinner4b9aad42020-11-02 16:49:54 +0100517 Default: ``NULL``.
Sandro Mani8f023a22020-06-08 17:28:11 +0200518
Victor Stinner4b9aad42020-11-02 16:49:54 +0100519 Part of the :ref:`Path Configuration <init-path-config>` output.
Sandro Mani8f023a22020-06-08 17:28:11 +0200520
Victor Stinner331a6a52019-05-27 16:39:22 +0200521 .. c:member:: int buffered_stdio
522
Victor Stinner4b9aad42020-11-02 16:49:54 +0100523 If equals to 0 and :c:member:`~PyConfig.configure_c_stdio` is non-zero,
524 disable buffering on the C streams stdout and stderr.
525
526 Set to 0 by the :option:`-u` command line option and the
527 :envvar:`PYTHONUNBUFFERED` environment variable.
Victor Stinner331a6a52019-05-27 16:39:22 +0200528
529 stdin is always opened in buffered mode.
530
Victor Stinner4b9aad42020-11-02 16:49:54 +0100531 Default: ``1``.
532
Victor Stinner331a6a52019-05-27 16:39:22 +0200533 .. c:member:: int bytes_warning
534
535 If equals to 1, issue a warning when comparing :class:`bytes` or
536 :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with
Victor Stinner4b9aad42020-11-02 16:49:54 +0100537 :class:`int`.
538
539 If equal or greater to 2, raise a :exc:`BytesWarning` exception in these
540 cases.
541
542 Incremented by the :option:`-b` command line option.
543
544 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200545
546 .. c:member:: wchar_t* check_hash_pycs_mode
547
Victor Stinner4b9aad42020-11-02 16:49:54 +0100548 Control the validation behavior of hash-based ``.pyc`` files:
549 value of the :option:`--check-hash-based-pycs` command line option.
Victor Stinner331a6a52019-05-27 16:39:22 +0200550
Victor Stinner4b9aad42020-11-02 16:49:54 +0100551 Valid values:
Victor Stinner331a6a52019-05-27 16:39:22 +0200552
Victor Stinner4b9aad42020-11-02 16:49:54 +0100553 - ``L"always"``: Hash the source file for invalidation regardless of
554 value of the 'check_source' flag.
555 - ``L"never"``: Assume that hash-based pycs always are valid.
556 - ``L"default"``: The 'check_source' flag in hash-based pycs
557 determines invalidation.
558
559 Default: ``L"default"``.
560
561 See also :pep:`552` "Deterministic pycs".
Victor Stinner331a6a52019-05-27 16:39:22 +0200562
563 .. c:member:: int configure_c_stdio
564
Victor Stinner4b9aad42020-11-02 16:49:54 +0100565 If non-zero, configure C standard streams:
566
567 * On Windows, set the binary mode (``O_BINARY``) on stdin, stdout and
568 stderr.
569 * If :c:member:`~PyConfig.buffered_stdio` equals zero, disable buffering
570 of stdin, stdout and stderr streams.
571 * If :c:member:`~PyConfig.interactive` is non-zero, enable stream
572 buffering on stdin and stdout (only stdout on Windows).
573
574 Default: ``1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200575
576 .. c:member:: int dev_mode
577
Victor Stinnerb9783d22020-01-24 10:22:18 +0100578 If non-zero, enable the :ref:`Python Development Mode <devmode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200579
Victor Stinner4b9aad42020-11-02 16:49:54 +0100580 Default: ``-1`` in Python mode, ``0`` in isolated mode.
581
Victor Stinner331a6a52019-05-27 16:39:22 +0200582 .. c:member:: int dump_refs
583
Victor Stinner4b9aad42020-11-02 16:49:54 +0100584 Dump Python refererences?
585
Victor Stinner331a6a52019-05-27 16:39:22 +0200586 If non-zero, dump all objects which are still alive at exit.
587
Victor Stinner4b9aad42020-11-02 16:49:54 +0100588 Set to ``1`` by the :envvar:`PYTHONDUMPREFS` environment variable.
589
590 Need a special build of Python with the ``Py_TRACE_REFS`` macro defined.
591
592 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200593
594 .. c:member:: wchar_t* exec_prefix
595
Victor Stinner4b9aad42020-11-02 16:49:54 +0100596 The site-specific directory prefix where the platform-dependent Python
597 files are installed: :data:`sys.exec_prefix`.
598
599 Default: ``NULL``.
600
601 Part of the :ref:`Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200602
603 .. c:member:: wchar_t* executable
604
Victor Stinner4b9aad42020-11-02 16:49:54 +0100605 The absolute path of the executable binary for the Python interpreter:
Victor Stinner331a6a52019-05-27 16:39:22 +0200606 :data:`sys.executable`.
607
Victor Stinner4b9aad42020-11-02 16:49:54 +0100608 Default: ``NULL``.
609
610 Part of the :ref:`Path Configuration <init-path-config>` output.
611
Victor Stinner331a6a52019-05-27 16:39:22 +0200612 .. c:member:: int faulthandler
613
Victor Stinner4b9aad42020-11-02 16:49:54 +0100614 Enable faulthandler?
615
Victor Stinner88feaec2019-09-26 03:15:07 +0200616 If non-zero, call :func:`faulthandler.enable` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200617
Victor Stinner4b9aad42020-11-02 16:49:54 +0100618 Set to ``1`` by :option:`-X faulthandler <-X>` and the
619 :envvar:`PYTHONFAULTHANDLER` environment variable.
620
621 Default: ``-1`` in Python mode, ``0`` in isolated mode.
622
Victor Stinner331a6a52019-05-27 16:39:22 +0200623 .. c:member:: wchar_t* filesystem_encoding
624
Victor Stinner4b9aad42020-11-02 16:49:54 +0100625 :term:`Filesystem encoding <filesystem encoding and error handler>`:
626 :func:`sys.getfilesystemencoding`.
Victor Stinnere662c392020-11-01 23:07:23 +0100627
628 On macOS, Android and VxWorks: use ``"utf-8"`` by default.
629
630 On Windows: use ``"utf-8"`` by default, or ``"mbcs"`` if
631 :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of
632 :c:type:`PyPreConfig` is non-zero.
633
634 Default encoding on other platforms:
635
636 * ``"utf-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero.
637 * ``"ascii"`` if Python detects that ``nl_langinfo(CODESET)`` announces
638 the ASCII encoding (or Roman8 encoding on HP-UX), whereas the
639 ``mbstowcs()`` function decodes from a different encoding (usually
640 Latin1).
641 * ``"utf-8"`` if ``nl_langinfo(CODESET)`` returns an empty string.
Victor Stinner4b9aad42020-11-02 16:49:54 +0100642 * Otherwise, use the :term:`locale encoding`:
Victor Stinnere662c392020-11-01 23:07:23 +0100643 ``nl_langinfo(CODESET)`` result.
644
645 At Python statup, the encoding name is normalized to the Python codec
646 name. For example, ``"ANSI_X3.4-1968"`` is replaced with ``"ascii"``.
647
648 See also the :c:member:`~PyConfig.filesystem_errors` member.
Victor Stinner331a6a52019-05-27 16:39:22 +0200649
650 .. c:member:: wchar_t* filesystem_errors
651
Victor Stinner4b9aad42020-11-02 16:49:54 +0100652 :term:`Filesystem error handler <filesystem encoding and error handler>`:
653 :func:`sys.getfilesystemencodeerrors`.
Victor Stinnere662c392020-11-01 23:07:23 +0100654
655 On Windows: use ``"surrogatepass"`` by default, or ``"replace"`` if
656 :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of
657 :c:type:`PyPreConfig` is non-zero.
658
659 On other platforms: use ``"surrogateescape"`` by default.
660
661 Supported error handlers:
662
663 * ``"strict"``
664 * ``"surrogateescape"``
665 * ``"surrogatepass"`` (only supported with the UTF-8 encoding)
666
667 See also the :c:member:`~PyConfig.filesystem_encoding` member.
Victor Stinner331a6a52019-05-27 16:39:22 +0200668
669 .. c:member:: unsigned long hash_seed
670 .. c:member:: int use_hash_seed
671
672 Randomized hash function seed.
673
674 If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly
Victor Stinner4b9aad42020-11-02 16:49:54 +0100675 at Python startup, and :c:member:`~PyConfig.hash_seed` is ignored.
676
677 Set by the :envvar:`PYTHONHASHSEED` environment variable.
678
679 Default *use_hash_seed* value: ``-1`` in Python mode, ``0`` in isolated
680 mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200681
682 .. c:member:: wchar_t* home
683
684 Python home directory.
685
Victor Stinner4b9aad42020-11-02 16:49:54 +0100686 If :c:func:`Py_SetPythonHome` has been called, use its argument if it is
687 not ``NULL``.
688
689 Set by the :envvar:`PYTHONHOME` environment variable.
690
691 Default: ``NULL``.
692
693 Part of the :ref:`Path Configuration <init-path-config>` input.
Victor Stinner88feaec2019-09-26 03:15:07 +0200694
Victor Stinner331a6a52019-05-27 16:39:22 +0200695 .. c:member:: int import_time
696
697 If non-zero, profile import time.
698
Victor Stinner4b9aad42020-11-02 16:49:54 +0100699 Set the ``1`` by the :option:`-X importtime <-X>` option and the
700 :envvar:`PYTHONPROFILEIMPORTTIME` environment variable.
701
702 Default: ``0``.
703
Victor Stinner331a6a52019-05-27 16:39:22 +0200704 .. c:member:: int inspect
705
706 Enter interactive mode after executing a script or a command.
707
Victor Stinner4b9aad42020-11-02 16:49:54 +0100708 If greater than 0, enable inspect: when a script is passed as first
709 argument or the -c option is used, enter interactive mode after executing
710 the script or the command, even when :data:`sys.stdin` does not appear to
711 be a terminal.
712
713 Incremented by the :option:`-i` command line option. Set to ``1`` if the
714 :envvar:`PYTHONINSPECT` environment variable is non-empty.
715
716 Default: ``0``.
717
Victor Stinner331a6a52019-05-27 16:39:22 +0200718 .. c:member:: int install_signal_handlers
719
Victor Stinner4b9aad42020-11-02 16:49:54 +0100720 Install Python signal handlers?
721
722 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200723
724 .. c:member:: int interactive
725
Victor Stinner4b9aad42020-11-02 16:49:54 +0100726 If greater than 0, enable the interactive mode (REPL).
727
728 Incremented by the :option:`-i` command line option.
729
730 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200731
732 .. c:member:: int isolated
733
734 If greater than 0, enable isolated mode:
735
736 * :data:`sys.path` contains neither the script's directory (computed from
737 ``argv[0]`` or the current directory) nor the user's site-packages
738 directory.
739 * Python REPL doesn't import :mod:`readline` nor enable default readline
740 configuration on interactive prompts.
741 * Set :c:member:`~PyConfig.use_environment` and
742 :c:member:`~PyConfig.user_site_directory` to 0.
743
Victor Stinner4b9aad42020-11-02 16:49:54 +0100744 Default: ``0`` in Python mode, ``1`` in isolated mode.
745
746 See also :c:member:`PyPreConfig.isolated`.
747
Victor Stinner331a6a52019-05-27 16:39:22 +0200748 .. c:member:: int legacy_windows_stdio
749
750 If non-zero, use :class:`io.FileIO` instead of
751 :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout`
752 and :data:`sys.stderr`.
753
Victor Stinner4b9aad42020-11-02 16:49:54 +0100754 Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSSTDIO` environment
755 variable is set to a non-empty string.
756
Victor Stinner331a6a52019-05-27 16:39:22 +0200757 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
758 Windows specific code.
759
Victor Stinner4b9aad42020-11-02 16:49:54 +0100760 Default: ``0``.
761
762 See also the :pep:`528` (Change Windows console encoding to UTF-8).
763
Victor Stinner331a6a52019-05-27 16:39:22 +0200764 .. c:member:: int malloc_stats
765
766 If non-zero, dump statistics on :ref:`Python pymalloc memory allocator
767 <pymalloc>` at exit.
768
Victor Stinner4b9aad42020-11-02 16:49:54 +0100769 Set to ``1`` by the :envvar:`PYTHONMALLOCSTATS` environment variable.
770
Victor Stinner331a6a52019-05-27 16:39:22 +0200771 The option is ignored if Python is built using ``--without-pymalloc``.
772
Victor Stinner4b9aad42020-11-02 16:49:54 +0100773 Default: ``0``.
774
775 .. c:member:: wchar_t* platlibdir
776
777 Platform library directory name: :data:`sys.platlibdir`.
778
779 Set by the :envvar:`PYTHONPLATLIBDIR` environment variable.
780
781 Default: value of the ``PLATLIBDIR`` macro which is set at configure time
782 by ``--with-platlibdir`` (default: ``"lib"``).
783
784 Part of the :ref:`Path Configuration <init-path-config>` input.
785
786 .. versionadded:: 3.9
787
Victor Stinner331a6a52019-05-27 16:39:22 +0200788 .. c:member:: wchar_t* pythonpath_env
789
Victor Stinner4b9aad42020-11-02 16:49:54 +0100790 Module search paths (:data:`sys.path`) as a string separated by ``DELIM``
Victor Stinner331a6a52019-05-27 16:39:22 +0200791 (:data:`os.path.pathsep`).
792
Victor Stinner4b9aad42020-11-02 16:49:54 +0100793 Set by the :envvar:`PYTHONPATH` environment variable.
794
795 Default: ``NULL``.
796
797 Part of the :ref:`Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200798
799 .. c:member:: PyWideStringList module_search_paths
800 .. c:member:: int module_search_paths_set
801
Victor Stinner4b9aad42020-11-02 16:49:54 +0100802 Module search paths: :data:`sys.path`.
803
804 If :c:member:`~PyConfig.module_search_paths_set` is equal to 0, the
805 function calculating the :ref:`Path Configuration <init-path-config>`
806 overrides the :c:member:`~PyConfig.module_search_paths` and sets
807 :c:member:`~PyConfig.module_search_paths_set` to ``1``.
808
809 Default: empty list (``module_search_paths``) and ``0``
810 (``module_search_paths_set``).
811
812 Part of the :ref:`Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200813
814 .. c:member:: int optimization_level
815
816 Compilation optimization level:
817
Victor Stinner4b9aad42020-11-02 16:49:54 +0100818 * ``0``: Peephole optimizer, set ``__debug__`` to ``True``.
819 * ``1``: Level 0, remove assertions, set ``__debug__`` to ``False``.
820 * ``2``: Level 1, strip docstrings.
821
822 Incremented by the :option:`-O` command line option. Set to the
823 :envvar:`PYTHONOPTIMIZE` environment variable value.
824
825 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200826
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200827 .. c:member:: PyWideStringList orig_argv
828
829 The list of the original command line arguments passed to the Python
Victor Stinner4b9aad42020-11-02 16:49:54 +0100830 executable: :data:`sys.orig_argv`.
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200831
832 If :c:member:`~PyConfig.orig_argv` list is empty and
833 :c:member:`~PyConfig.argv` is not a list only containing an empty
834 string, :c:func:`PyConfig_Read()` copies :c:member:`~PyConfig.argv` into
835 :c:member:`~PyConfig.orig_argv` before modifying
836 :c:member:`~PyConfig.argv` (if :c:member:`~PyConfig.parse_argv` is
837 non-zero).
838
839 See also the :c:member:`~PyConfig.argv` member and the
840 :c:func:`Py_GetArgcArgv` function.
841
Victor Stinner4b9aad42020-11-02 16:49:54 +0100842 Default: empty list.
843
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200844 .. versionadded:: 3.10
845
Victor Stinner331a6a52019-05-27 16:39:22 +0200846 .. c:member:: int parse_argv
847
Victor Stinner4b9aad42020-11-02 16:49:54 +0100848 Parse command line arguments?
849
Victor Stinner331a6a52019-05-27 16:39:22 +0200850 If non-zero, parse :c:member:`~PyConfig.argv` the same way the regular
Victor Stinner4b9aad42020-11-02 16:49:54 +0100851 Python parses :ref:`command line arguments <using-on-cmdline>`, and strip
852 Python arguments from :c:member:`~PyConfig.argv`.
853
854 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200855
856 .. c:member:: int parser_debug
857
Victor Stinner4b9aad42020-11-02 16:49:54 +0100858 Parser debug mode. If greater than 0, turn on parser debugging output (for expert only, depending
Victor Stinner331a6a52019-05-27 16:39:22 +0200859 on compilation options).
860
Victor Stinner4b9aad42020-11-02 16:49:54 +0100861 Incremented by the :option:`-d` command line option. Set to the
862 :envvar:`PYTHONDEBUG` environment variable value.
863
864 Default: ``0``.
865
Victor Stinner331a6a52019-05-27 16:39:22 +0200866 .. c:member:: int pathconfig_warnings
867
Victor Stinner4b9aad42020-11-02 16:49:54 +0100868 On Unix, if non-zero, calculating the :ref:`Path Configuration
869 <init-path-config>` can log warnings into ``stderr``. If equals to 0,
870 suppress these warnings.
871
872 It has no effect on Windows.
873
874 Default: ``1`` in Python mode, ``0`` in isolated mode.
875
876 Part of the :ref:`Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200877
878 .. c:member:: wchar_t* prefix
879
Victor Stinner4b9aad42020-11-02 16:49:54 +0100880 The site-specific directory prefix where the platform independent Python
881 files are installed: :data:`sys.prefix`.
882
883 Default: ``NULL``.
884
885 Part of the :ref:`Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200886
887 .. c:member:: wchar_t* program_name
888
Victor Stinner4b9aad42020-11-02 16:49:54 +0100889 Program name used to initialize :c:member:`~PyConfig.executable` and in
890 early error messages during Python initialization.
891
892 * If :func:`Py_SetProgramName` has been called, use its argument.
893 * On macOS, use :envvar:`PYTHONEXECUTABLE` environment variable if set.
894 * If the ``WITH_NEXT_FRAMEWORK`` macro is defined, use
895 :envvar:`__PYVENV_LAUNCHER__` environment variable if set.
896 * Use ``argv[0]`` of :c:member:`~PyConfig.argv` if available and
897 non-empty.
898 * Otherwise, use ``L"python"`` on Windows, or ``L"python3"`` on other
899 platforms.
900
901 Default: ``NULL``.
902
903 Part of the :ref:`Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200904
905 .. c:member:: wchar_t* pycache_prefix
906
Victor Stinner4b9aad42020-11-02 16:49:54 +0100907 Directory where cached ``.pyc`` files are written:
908 :data:`sys.pycache_prefix`.
909
910 Set by the :option:`-X pycache_prefix=PATH <-X>` command line option and
911 the :envvar:`PYTHONPYCACHEPREFIX` environment variable.
Victor Stinner88feaec2019-09-26 03:15:07 +0200912
Serhiy Storchakae835b312019-10-30 21:37:16 +0200913 If ``NULL``, :data:`sys.pycache_prefix` is set to ``None``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200914
Victor Stinner4b9aad42020-11-02 16:49:54 +0100915 Default: ``NULL``.
916
Victor Stinner331a6a52019-05-27 16:39:22 +0200917 .. c:member:: int quiet
918
Victor Stinner4b9aad42020-11-02 16:49:54 +0100919 Quiet mode. If greater than 0, don't display the copyright and version at
920 Python startup in interactive mode.
921
922 Incremented by the :option:`-q` command line option.
923
924 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200925
926 .. c:member:: wchar_t* run_command
927
Victor Stinner4b9aad42020-11-02 16:49:54 +0100928 Value of the :option:`-c` command line option.
929
930 Used by :c:func:`Py_RunMain`.
931
932 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200933
934 .. c:member:: wchar_t* run_filename
935
Victor Stinner4b9aad42020-11-02 16:49:54 +0100936 Filename passed on the command line: trailing command line argument
937 without :option:`-c` or :option:`-m`.
938
939 For example, it is set to ``script.py`` by the ``python3 script.py arg``
940 command.
941
942 Used by :c:func:`Py_RunMain`.
943
944 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200945
946 .. c:member:: wchar_t* run_module
947
Victor Stinner4b9aad42020-11-02 16:49:54 +0100948 Value of the :option:`-m` command line option.
949
950 Used by :c:func:`Py_RunMain`.
951
952 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200953
Victor Stinner331a6a52019-05-27 16:39:22 +0200954 .. c:member:: int show_ref_count
955
956 Show total reference count at exit?
957
Victor Stinner88feaec2019-09-26 03:15:07 +0200958 Set to 1 by :option:`-X showrefcount <-X>` command line option.
959
Victor Stinner331a6a52019-05-27 16:39:22 +0200960 Need a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
961
Victor Stinner4b9aad42020-11-02 16:49:54 +0100962 Default: ``0``.
963
Victor Stinner331a6a52019-05-27 16:39:22 +0200964 .. c:member:: int site_import
965
966 Import the :mod:`site` module at startup?
967
Victor Stinner4b9aad42020-11-02 16:49:54 +0100968 If equal to zero, disable the import of the module site and the
969 site-dependent manipulations of :data:`sys.path` that it entails.
970
971 Also disable these manipulations if the :mod:`site` module is explicitly
972 imported later (call :func:`site.main` if you want them to be triggered).
973
974 Set to ``0`` by the :option:`-S` command line option.
975
976 :data:`sys.flags.no_site` is set to the inverted value of
977 :c:member:`~PyConfig.site_import`.
978
979 Default: ``1``.
980
Victor Stinner331a6a52019-05-27 16:39:22 +0200981 .. c:member:: int skip_source_first_line
982
Victor Stinner4b9aad42020-11-02 16:49:54 +0100983 If non-zero, skip the first line of the :c:member:`PyConfig.run_filename`
984 source.
985
986 It allows the usage of non-Unix forms of ``#!cmd``. This is intended for
987 a DOS specific hack only.
988
989 Set to ``1`` by the :option:`-x` command line option.
990
991 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200992
993 .. c:member:: wchar_t* stdio_encoding
994 .. c:member:: wchar_t* stdio_errors
995
996 Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
Victor Stinner4b9aad42020-11-02 16:49:54 +0100997 :data:`sys.stderr` (but :data:`sys.stderr` always uses
998 ``"backslashreplace"`` error handler).
999
1000 If :c:func:`Py_SetStandardStreamEncoding` has been called, use its
1001 *error* and *errors* arguments if they are not ``NULL``.
1002
1003 Use the :envvar:`PYTHONIOENCODING` environment variable if it is
1004 non-empty.
1005
1006 Default encoding:
1007
1008 * ``"UTF-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero.
1009 * Otherwise, use the :term:`locale encoding`.
1010
1011 Default error handler:
1012
1013 * On Windows: use ``"surrogateescape"``.
1014 * ``"surrogateescape"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero,
1015 or if the LC_CTYPE locale is "C" or "POSIX".
1016 * ``"strict"`` otherwise.
Victor Stinner331a6a52019-05-27 16:39:22 +02001017
1018 .. c:member:: int tracemalloc
1019
Victor Stinner4b9aad42020-11-02 16:49:54 +01001020 Enable tracemalloc?
1021
Victor Stinner88feaec2019-09-26 03:15:07 +02001022 If non-zero, call :func:`tracemalloc.start` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +02001023
Victor Stinner4b9aad42020-11-02 16:49:54 +01001024 Set by :option:`-X tracemalloc=N <-X>` command line option and by the
1025 :envvar:`PYTHONTRACEMALLOC` environment variable.
1026
1027 Default: ``-1`` in Python mode, ``0`` in isolated mode.
1028
Victor Stinner331a6a52019-05-27 16:39:22 +02001029 .. c:member:: int use_environment
1030
Victor Stinner4b9aad42020-11-02 16:49:54 +01001031 Use :ref:`environment variables <using-on-envvars>`?
1032
1033 If equals to zero, ignore the :ref:`environment variables
1034 <using-on-envvars>`.
1035
1036 Default: ``1`` in Python config and ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +02001037
1038 .. c:member:: int user_site_directory
1039
Victor Stinner4b9aad42020-11-02 16:49:54 +01001040 If non-zero, add the user site directory to :data:`sys.path`.
1041
1042 Set to ``0`` by the :option:`-s` and :option:`-I` command line options.
1043
1044 Set to ``0`` by the :envvar:`PYTHONNOUSERSITE` environment variable.
1045
1046 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +02001047
1048 .. c:member:: int verbose
1049
Victor Stinner4b9aad42020-11-02 16:49:54 +01001050 Verbose mode. If greater than 0, print a message each time a module is
1051 imported, showing the place (filename or built-in module) from which
1052 it is loaded.
1053
1054 If greater or equal to 2, print a message for each file that is checked
1055 for when searching for a module. Also provides information on module
1056 cleanup at exit.
1057
1058 Incremented by the :option:`-v` command line option.
1059
1060 Set to the :envvar:`PYTHONVERBOSE` environment variable value.
1061
1062 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +02001063
1064 .. c:member:: PyWideStringList warnoptions
1065
Victor Stinner4b9aad42020-11-02 16:49:54 +01001066 Options of the :mod:`warnings` module to build warnings filters, lowest
1067 to highest priority: :data:`sys.warnoptions`.
Victor Stinnerfb4ae152019-09-30 01:40:17 +02001068
1069 The :mod:`warnings` module adds :data:`sys.warnoptions` in the reverse
1070 order: the last :c:member:`PyConfig.warnoptions` item becomes the first
1071 item of :data:`warnings.filters` which is checked first (highest
1072 priority).
Victor Stinner331a6a52019-05-27 16:39:22 +02001073
Victor Stinner4b9aad42020-11-02 16:49:54 +01001074 Default: empty list.
1075
Victor Stinner331a6a52019-05-27 16:39:22 +02001076 .. c:member:: int write_bytecode
1077
Victor Stinner4b9aad42020-11-02 16:49:54 +01001078 If equal to 0, Python won't try to write ``.pyc`` files on the import of
1079 source modules.
1080
1081 Set to ``0`` by the :option:`-B` command line option and the
1082 :envvar:`PYTHONDONTWRITEBYTECODE` environment variable.
Victor Stinner331a6a52019-05-27 16:39:22 +02001083
Victor Stinner88feaec2019-09-26 03:15:07 +02001084 :data:`sys.dont_write_bytecode` is initialized to the inverted value of
1085 :c:member:`~PyConfig.write_bytecode`.
1086
Victor Stinner4b9aad42020-11-02 16:49:54 +01001087 Default: ``1``.
1088
Victor Stinner331a6a52019-05-27 16:39:22 +02001089 .. c:member:: PyWideStringList xoptions
1090
Victor Stinner4b9aad42020-11-02 16:49:54 +01001091 Values of the :option:`-X` command line options: :data:`sys._xoptions`.
Victor Stinner331a6a52019-05-27 16:39:22 +02001092
Victor Stinner4b9aad42020-11-02 16:49:54 +01001093 Default: empty list.
Victor Stinner331a6a52019-05-27 16:39:22 +02001094
Victor Stinner4b9aad42020-11-02 16:49:54 +01001095If :c:member:`~PyConfig.parse_argv` is non-zero, :c:member:`~PyConfig.argv`
1096arguments are parsed the same way the regular Python parses :ref:`command line
1097arguments <using-on-cmdline>`, and Python arguments are stripped from
1098:c:member:`~PyConfig.argv`.
1099
1100The :c:member:`~PyConfig.xoptions` options are parsed to set other options: see
1101the :option:`-X` command line option.
Victor Stinner331a6a52019-05-27 16:39:22 +02001102
Victor Stinnerc6e5c112020-02-03 15:17:15 +01001103.. versionchanged:: 3.9
1104
1105 The ``show_alloc_count`` field has been removed.
1106
Victor Stinner331a6a52019-05-27 16:39:22 +02001107
1108Initialization with PyConfig
1109----------------------------
1110
1111Function to initialize Python:
1112
1113.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
1114
1115 Initialize Python from *config* configuration.
1116
1117The caller is responsible to handle exceptions (error or exit) using
1118:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
1119
Victor Stinner4b9aad42020-11-02 16:49:54 +01001120If :c:func:`PyImport_FrozenModules`, :c:func:`PyImport_AppendInittab` or
1121:c:func:`PyImport_ExtendInittab` are used, they must be set or called after
1122Python preinitialization and before the Python initialization.
Victor Stinner331a6a52019-05-27 16:39:22 +02001123
1124Example setting the program name::
1125
1126 void init_python(void)
1127 {
1128 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +02001129
Victor Stinner8462a492019-10-01 12:06:16 +02001130 PyConfig config;
1131 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001132
1133 /* Set the program name. Implicitly preinitialize Python. */
1134 status = PyConfig_SetString(&config, &config.program_name,
1135 L"/path/to/my_program");
1136 if (PyStatus_Exception(status)) {
1137 goto fail;
1138 }
1139
1140 status = Py_InitializeFromConfig(&config);
1141 if (PyStatus_Exception(status)) {
1142 goto fail;
1143 }
1144 PyConfig_Clear(&config);
1145 return;
1146
1147 fail:
1148 PyConfig_Clear(&config);
1149 Py_ExitStatusException(status);
1150 }
1151
1152More complete example modifying the default configuration, read the
1153configuration, and then override some parameters::
1154
1155 PyStatus init_python(const char *program_name)
1156 {
1157 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +02001158
Victor Stinner8462a492019-10-01 12:06:16 +02001159 PyConfig config;
1160 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001161
Gurupad Hegde6c7bb382019-12-28 17:16:02 -05001162 /* Set the program name before reading the configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001163 (decode byte string from the locale encoding).
1164
1165 Implicitly preinitialize Python. */
1166 status = PyConfig_SetBytesString(&config, &config.program_name,
Victor Stinner4b9aad42020-11-02 16:49:54 +01001167 program_name);
Victor Stinner331a6a52019-05-27 16:39:22 +02001168 if (PyStatus_Exception(status)) {
1169 goto done;
1170 }
1171
1172 /* Read all configuration at once */
1173 status = PyConfig_Read(&config);
1174 if (PyStatus_Exception(status)) {
1175 goto done;
1176 }
1177
1178 /* Append our custom search path to sys.path */
1179 status = PyWideStringList_Append(&config.module_search_paths,
Victor Stinner88feaec2019-09-26 03:15:07 +02001180 L"/path/to/more/modules");
Victor Stinner331a6a52019-05-27 16:39:22 +02001181 if (PyStatus_Exception(status)) {
1182 goto done;
1183 }
1184
1185 /* Override executable computed by PyConfig_Read() */
1186 status = PyConfig_SetString(&config, &config.executable,
1187 L"/path/to/my_executable");
1188 if (PyStatus_Exception(status)) {
1189 goto done;
1190 }
1191
1192 status = Py_InitializeFromConfig(&config);
1193
1194 done:
1195 PyConfig_Clear(&config);
1196 return status;
1197 }
1198
1199
1200.. _init-isolated-conf:
1201
1202Isolated Configuration
1203----------------------
1204
1205:c:func:`PyPreConfig_InitIsolatedConfig` and
1206:c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to
1207isolate Python from the system. For example, to embed Python into an
1208application.
1209
1210This configuration ignores global configuration variables, environments
Victor Stinner88feaec2019-09-26 03:15:07 +02001211variables, command line arguments (:c:member:`PyConfig.argv` is not parsed)
1212and user site directory. The C standard streams (ex: ``stdout``) and the
1213LC_CTYPE locale are left unchanged. Signal handlers are not installed.
Victor Stinner331a6a52019-05-27 16:39:22 +02001214
1215Configuration files are still used with this configuration. Set the
1216:ref:`Path Configuration <init-path-config>` ("output fields") to ignore these
1217configuration files and avoid the function computing the default path
1218configuration.
1219
1220
1221.. _init-python-config:
1222
1223Python Configuration
1224--------------------
1225
1226:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
1227functions create a configuration to build a customized Python which behaves as
1228the regular Python.
1229
1230Environments variables and command line arguments are used to configure
1231Python, whereas global configuration variables are ignored.
1232
Victor Stinner4b9aad42020-11-02 16:49:54 +01001233This function enables C locale coercion (:pep:`538`)
1234and :ref:`Python UTF-8 Mode <utf8-mode>`
Victor Stinner331a6a52019-05-27 16:39:22 +02001235(:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and
1236:envvar:`PYTHONCOERCECLOCALE` environment variables.
1237
1238Example of customized Python always running in isolated mode::
1239
1240 int main(int argc, char **argv)
1241 {
Victor Stinner331a6a52019-05-27 16:39:22 +02001242 PyStatus status;
Victor Stinner8462a492019-10-01 12:06:16 +02001243
Victor Stinner441b10c2019-09-28 04:28:35 +02001244 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001245 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001246 config.isolated = 1;
1247
1248 /* Decode command line arguments.
1249 Implicitly preinitialize Python (in isolated mode). */
1250 status = PyConfig_SetBytesArgv(&config, argc, argv);
1251 if (PyStatus_Exception(status)) {
1252 goto fail;
1253 }
1254
1255 status = Py_InitializeFromConfig(&config);
1256 if (PyStatus_Exception(status)) {
1257 goto fail;
1258 }
1259 PyConfig_Clear(&config);
1260
1261 return Py_RunMain();
1262
1263 fail:
1264 PyConfig_Clear(&config);
1265 if (PyStatus_IsExit(status)) {
1266 return status.exitcode;
1267 }
1268 /* Display the error message and exit the process with
1269 non-zero exit code */
1270 Py_ExitStatusException(status);
1271 }
1272
1273
1274.. _init-path-config:
1275
1276Path Configuration
1277------------------
1278
1279:c:type:`PyConfig` contains multiple fields for the path configuration:
1280
Victor Stinner8bf39b62019-09-26 02:22:35 +02001281* Path configuration inputs:
Victor Stinner331a6a52019-05-27 16:39:22 +02001282
1283 * :c:member:`PyConfig.home`
Sandro Mani8f023a22020-06-08 17:28:11 +02001284 * :c:member:`PyConfig.platlibdir`
Victor Stinner331a6a52019-05-27 16:39:22 +02001285 * :c:member:`PyConfig.pathconfig_warnings`
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001286 * :c:member:`PyConfig.program_name`
1287 * :c:member:`PyConfig.pythonpath_env`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001288 * current working directory: to get absolute paths
1289 * ``PATH`` environment variable to get the program full path
1290 (from :c:member:`PyConfig.program_name`)
1291 * ``__PYVENV_LAUNCHER__`` environment variable
1292 * (Windows only) Application paths in the registry under
1293 "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and
1294 HKEY_LOCAL_MACHINE (where X.Y is the Python version).
Victor Stinner331a6a52019-05-27 16:39:22 +02001295
1296* Path configuration output fields:
1297
Victor Stinner8bf39b62019-09-26 02:22:35 +02001298 * :c:member:`PyConfig.base_exec_prefix`
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001299 * :c:member:`PyConfig.base_executable`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001300 * :c:member:`PyConfig.base_prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +02001301 * :c:member:`PyConfig.exec_prefix`
1302 * :c:member:`PyConfig.executable`
Victor Stinner331a6a52019-05-27 16:39:22 +02001303 * :c:member:`PyConfig.module_search_paths_set`,
1304 :c:member:`PyConfig.module_search_paths`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001305 * :c:member:`PyConfig.prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +02001306
Victor Stinner8bf39b62019-09-26 02:22:35 +02001307If at least one "output field" is not set, Python calculates the path
Victor Stinner331a6a52019-05-27 16:39:22 +02001308configuration to fill unset fields. If
1309:c:member:`~PyConfig.module_search_paths_set` is equal to 0,
Min ho Kim39d87b52019-08-31 06:21:19 +10001310:c:member:`~PyConfig.module_search_paths` is overridden and
Victor Stinner331a6a52019-05-27 16:39:22 +02001311:c:member:`~PyConfig.module_search_paths_set` is set to 1.
1312
Victor Stinner8bf39b62019-09-26 02:22:35 +02001313It is possible to completely ignore the function calculating the default
Victor Stinner331a6a52019-05-27 16:39:22 +02001314path configuration by setting explicitly all path configuration output
1315fields listed above. A string is considered as set even if it is non-empty.
1316``module_search_paths`` is considered as set if
1317``module_search_paths_set`` is set to 1. In this case, path
1318configuration input fields are ignored as well.
1319
1320Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when
Victor Stinner8bf39b62019-09-26 02:22:35 +02001321calculating the path configuration (Unix only, Windows does not log any warning).
Victor Stinner331a6a52019-05-27 16:39:22 +02001322
1323If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix`
1324fields are not set, they inherit their value from :c:member:`~PyConfig.prefix`
1325and :c:member:`~PyConfig.exec_prefix` respectively.
1326
1327:c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`:
1328
1329* If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a
1330 ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to
1331 :data:`sys.path`.
1332* If :c:member:`~PyConfig.isolated` is zero:
1333
1334 * If :c:member:`~PyConfig.run_module` is set, prepend the current directory
1335 to :data:`sys.path`. Do nothing if the current directory cannot be read.
1336 * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the
1337 filename to :data:`sys.path`.
1338 * Otherwise, prepend an empty string to :data:`sys.path`.
1339
1340If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be
1341modified by the :mod:`site` module. If
1342:c:member:`~PyConfig.user_site_directory` is non-zero and the user's
1343site-package directory exists, the :mod:`site` module appends the user's
1344site-package directory to :data:`sys.path`.
1345
1346The following configuration files are used by the path configuration:
1347
1348* ``pyvenv.cfg``
1349* ``python._pth`` (Windows only)
1350* ``pybuilddir.txt`` (Unix only)
1351
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001352The ``__PYVENV_LAUNCHER__`` environment variable is used to set
1353:c:member:`PyConfig.base_executable`
1354
Victor Stinner331a6a52019-05-27 16:39:22 +02001355
1356Py_RunMain()
1357------------
1358
1359.. c:function:: int Py_RunMain(void)
1360
1361 Execute the command (:c:member:`PyConfig.run_command`), the script
1362 (:c:member:`PyConfig.run_filename`) or the module
1363 (:c:member:`PyConfig.run_module`) specified on the command line or in the
1364 configuration.
1365
1366 By default and when if :option:`-i` option is used, run the REPL.
1367
1368 Finally, finalizes Python and returns an exit status that can be passed to
1369 the ``exit()`` function.
1370
1371See :ref:`Python Configuration <init-python-config>` for an example of
1372customized Python always running in isolated mode using
1373:c:func:`Py_RunMain`.
1374
1375
Victor Stinnere81f6e62020-06-08 18:12:59 +02001376Py_GetArgcArgv()
1377----------------
1378
1379.. c:function:: void Py_GetArgcArgv(int *argc, wchar_t ***argv)
1380
1381 Get the original command line arguments, before Python modified them.
1382
Victor Stinnerdd8a93e2020-06-30 00:49:03 +02001383 See also :c:member:`PyConfig.orig_argv` member.
1384
Victor Stinnere81f6e62020-06-08 18:12:59 +02001385
Victor Stinner331a6a52019-05-27 16:39:22 +02001386Multi-Phase Initialization Private Provisional API
1387--------------------------------------------------
1388
1389This section is a private provisional API introducing multi-phase
1390initialization, the core feature of the :pep:`432`:
1391
1392* "Core" initialization phase, "bare minimum Python":
1393
1394 * Builtin types;
1395 * Builtin exceptions;
1396 * Builtin and frozen modules;
1397 * The :mod:`sys` module is only partially initialized
Victor Stinner88feaec2019-09-26 03:15:07 +02001398 (ex: :data:`sys.path` doesn't exist yet).
Victor Stinner331a6a52019-05-27 16:39:22 +02001399
1400* "Main" initialization phase, Python is fully initialized:
1401
1402 * Install and configure :mod:`importlib`;
1403 * Apply the :ref:`Path Configuration <init-path-config>`;
1404 * Install signal handlers;
1405 * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout`
1406 and :data:`sys.path`);
1407 * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`;
1408 * Import the :mod:`site` module;
1409 * etc.
1410
1411Private provisional API:
1412
1413* :c:member:`PyConfig._init_main`: if set to 0,
1414 :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase.
Victor Stinner252346a2020-05-01 11:33:44 +02001415* :c:member:`PyConfig._isolated_interpreter`: if non-zero,
1416 disallow threads, subprocesses and fork.
Victor Stinner331a6a52019-05-27 16:39:22 +02001417
1418.. c:function:: PyStatus _Py_InitializeMain(void)
1419
1420 Move to the "Main" initialization phase, finish the Python initialization.
1421
1422No module is imported during the "Core" phase and the ``importlib`` module is
1423not configured: the :ref:`Path Configuration <init-path-config>` is only
1424applied during the "Main" phase. It may allow to customize Python in Python to
1425override or tune the :ref:`Path Configuration <init-path-config>`, maybe
Victor Stinner88feaec2019-09-26 03:15:07 +02001426install a custom :data:`sys.meta_path` importer or an import hook, etc.
Victor Stinner331a6a52019-05-27 16:39:22 +02001427
Victor Stinner88feaec2019-09-26 03:15:07 +02001428It may become possible to calculatin the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001429<init-path-config>` in Python, after the Core phase and before the Main phase,
1430which is one of the :pep:`432` motivation.
1431
1432The "Core" phase is not properly defined: what should be and what should
1433not be available at this phase is not specified yet. The API is marked
1434as private and provisional: the API can be modified or even be removed
1435anytime until a proper public API is designed.
1436
1437Example running Python code between "Core" and "Main" initialization
1438phases::
1439
1440 void init_python(void)
1441 {
1442 PyStatus status;
Victor Stinner8462a492019-10-01 12:06:16 +02001443
Victor Stinner331a6a52019-05-27 16:39:22 +02001444 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001445 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001446 config._init_main = 0;
1447
1448 /* ... customize 'config' configuration ... */
1449
1450 status = Py_InitializeFromConfig(&config);
1451 PyConfig_Clear(&config);
1452 if (PyStatus_Exception(status)) {
1453 Py_ExitStatusException(status);
1454 }
1455
1456 /* Use sys.stderr because sys.stdout is only created
1457 by _Py_InitializeMain() */
1458 int res = PyRun_SimpleString(
1459 "import sys; "
1460 "print('Run Python code before _Py_InitializeMain', "
1461 "file=sys.stderr)");
1462 if (res < 0) {
1463 exit(1);
1464 }
1465
1466 /* ... put more configuration code here ... */
1467
1468 status = _Py_InitializeMain();
1469 if (PyStatus_Exception(status)) {
1470 Py_ExitStatusException(status);
1471 }
1472 }