blob: 29fbb68195b347cf3b14bc35594bce1753eede66 [file] [log] [blame]
Victor Stinner331a6a52019-05-27 16:39:22 +02001.. highlight:: c
2
3.. _init-config:
4
5***********************************
6Python Initialization Configuration
7***********************************
8
9.. versionadded:: 3.8
10
Victor Stinnerdc42af82020-11-05 18:58:07 +010011Python can be initialized with :c:func:`Py_InitializeFromConfig` and the
12:c:type:`PyConfig` structure. It can be preinitialized with
13:c:func:`Py_PreInitialize` and the :c:type:`PyPreConfig` structure.
Victor Stinner331a6a52019-05-27 16:39:22 +020014
Victor Stinnerdc42af82020-11-05 18:58:07 +010015There are two kinds of configuration:
Victor Stinner331a6a52019-05-27 16:39:22 +020016
Victor Stinnerdc42af82020-11-05 18:58:07 +010017* The :ref:`Python Configuration <init-python-config>` can be used to build a
18 customized Python which behaves as the regular Python. For example,
19 environments variables and command line arguments are used to configure
20 Python.
Victor Stinner331a6a52019-05-27 16:39:22 +020021
Victor Stinnerdc42af82020-11-05 18:58:07 +010022* The :ref:`Isolated Configuration <init-isolated-conf>` can be used to embed
23 Python into an application. It isolates Python from the system. For example,
24 environments variables are ignored, the LC_CTYPE locale is left unchanged and
25 no signal handler is registred.
Victor Stinner331a6a52019-05-27 16:39:22 +020026
Victor Stinnerace3f9a2020-11-10 21:10:22 +010027The :c:func:`Py_RunMain` function can be used to write a customized Python
28program.
29
Victor Stinner1beb7c32019-08-23 17:59:12 +010030See also :ref:`Initialization, Finalization, and Threads <initialization>`.
31
Victor Stinner331a6a52019-05-27 16:39:22 +020032.. seealso::
33 :pep:`587` "Python Initialization Configuration".
34
Victor Stinnerace3f9a2020-11-10 21:10:22 +010035
Victor Stinnerdc42af82020-11-05 18:58:07 +010036Example
37=======
38
39Example of customized Python always running in isolated mode::
40
41 int main(int argc, char **argv)
42 {
43 PyStatus status;
44
45 PyConfig config;
46 PyConfig_InitPythonConfig(&config);
47 config.isolated = 1;
48
49 /* Decode command line arguments.
50 Implicitly preinitialize Python (in isolated mode). */
51 status = PyConfig_SetBytesArgv(&config, argc, argv);
52 if (PyStatus_Exception(status)) {
53 goto exception;
54 }
55
56 status = Py_InitializeFromConfig(&config);
57 if (PyStatus_Exception(status)) {
58 goto exception;
59 }
60 PyConfig_Clear(&config);
61
62 return Py_RunMain();
63
64 exception:
65 PyConfig_Clear(&config);
66 if (PyStatus_IsExit(status)) {
67 return status.exitcode;
68 }
69 /* Display the error message and exit the process with
70 non-zero exit code */
71 Py_ExitStatusException(status);
72 }
73
Victor Stinner331a6a52019-05-27 16:39:22 +020074
75PyWideStringList
Victor Stinnerdc42af82020-11-05 18:58:07 +010076================
Victor Stinner331a6a52019-05-27 16:39:22 +020077
78.. c:type:: PyWideStringList
79
80 List of ``wchar_t*`` strings.
81
Serhiy Storchakae835b312019-10-30 21:37:16 +020082 If *length* is non-zero, *items* must be non-``NULL`` and all strings must be
83 non-``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +020084
85 Methods:
86
87 .. c:function:: PyStatus PyWideStringList_Append(PyWideStringList *list, const wchar_t *item)
88
89 Append *item* to *list*.
90
91 Python must be preinitialized to call this function.
92
93 .. c:function:: PyStatus PyWideStringList_Insert(PyWideStringList *list, Py_ssize_t index, const wchar_t *item)
94
Victor Stinner3842f292019-08-23 16:57:54 +010095 Insert *item* into *list* at *index*.
96
97 If *index* is greater than or equal to *list* length, append *item* to
98 *list*.
99
100 *index* must be greater than or equal to 0.
Victor Stinner331a6a52019-05-27 16:39:22 +0200101
102 Python must be preinitialized to call this function.
103
104 Structure fields:
105
106 .. c:member:: Py_ssize_t length
107
108 List length.
109
110 .. c:member:: wchar_t** items
111
112 List items.
113
114PyStatus
Victor Stinnerdc42af82020-11-05 18:58:07 +0100115========
Victor Stinner331a6a52019-05-27 16:39:22 +0200116
117.. c:type:: PyStatus
118
119 Structure to store an initialization function status: success, error
120 or exit.
121
122 For an error, it can store the C function name which created the error.
123
124 Structure fields:
125
126 .. c:member:: int exitcode
127
128 Exit code. Argument passed to ``exit()``.
129
130 .. c:member:: const char *err_msg
131
132 Error message.
133
134 .. c:member:: const char *func
135
136 Name of the function which created an error, can be ``NULL``.
137
138 Functions to create a status:
139
140 .. c:function:: PyStatus PyStatus_Ok(void)
141
142 Success.
143
144 .. c:function:: PyStatus PyStatus_Error(const char *err_msg)
145
146 Initialization error with a message.
147
Victor Stinner048a3562020-11-05 00:45:56 +0100148 *err_msg* must not be ``NULL``.
149
Victor Stinner331a6a52019-05-27 16:39:22 +0200150 .. c:function:: PyStatus PyStatus_NoMemory(void)
151
152 Memory allocation failure (out of memory).
153
154 .. c:function:: PyStatus PyStatus_Exit(int exitcode)
155
156 Exit Python with the specified exit code.
157
158 Functions to handle a status:
159
160 .. c:function:: int PyStatus_Exception(PyStatus status)
161
162 Is the status an error or an exit? If true, the exception must be
163 handled; by calling :c:func:`Py_ExitStatusException` for example.
164
165 .. c:function:: int PyStatus_IsError(PyStatus status)
166
167 Is the result an error?
168
169 .. c:function:: int PyStatus_IsExit(PyStatus status)
170
171 Is the result an exit?
172
173 .. c:function:: void Py_ExitStatusException(PyStatus status)
174
175 Call ``exit(exitcode)`` if *status* is an exit. Print the error
176 message and exit with a non-zero exit code if *status* is an error. Must
177 only be called if ``PyStatus_Exception(status)`` is non-zero.
178
179.. note::
180 Internally, Python uses macros which set ``PyStatus.func``,
181 whereas functions to create a status set ``func`` to ``NULL``.
182
183Example::
184
185 PyStatus alloc(void **ptr, size_t size)
186 {
187 *ptr = PyMem_RawMalloc(size);
188 if (*ptr == NULL) {
189 return PyStatus_NoMemory();
190 }
191 return PyStatus_Ok();
192 }
193
194 int main(int argc, char **argv)
195 {
196 void *ptr;
197 PyStatus status = alloc(&ptr, 16);
198 if (PyStatus_Exception(status)) {
199 Py_ExitStatusException(status);
200 }
201 PyMem_Free(ptr);
202 return 0;
203 }
204
205
206PyPreConfig
Victor Stinnerdc42af82020-11-05 18:58:07 +0100207===========
Victor Stinner331a6a52019-05-27 16:39:22 +0200208
209.. c:type:: PyPreConfig
210
Victor Stinner4b9aad42020-11-02 16:49:54 +0100211 Structure used to preinitialize Python.
Victor Stinner331a6a52019-05-27 16:39:22 +0200212
213 Function to initialize a preconfiguration:
214
tomerv741008a2020-07-01 12:32:54 +0300215 .. c:function:: void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200216
217 Initialize the preconfiguration with :ref:`Python Configuration
218 <init-python-config>`.
219
tomerv741008a2020-07-01 12:32:54 +0300220 .. c:function:: void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200221
222 Initialize the preconfiguration with :ref:`Isolated Configuration
223 <init-isolated-conf>`.
224
225 Structure fields:
226
227 .. c:member:: int allocator
228
Victor Stinner4b9aad42020-11-02 16:49:54 +0100229 Name of the Python memory allocators:
Victor Stinner331a6a52019-05-27 16:39:22 +0200230
231 * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators
232 (use defaults)
233 * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators
234 * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with
235 debug hooks
236 * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()``
237 * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of
238 ``malloc()`` with debug hooks
239 * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory
240 allocator <pymalloc>`
241 * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc
242 memory allocator <pymalloc>` with debug hooks
243
244 ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG``
245 are not supported if Python is configured using ``--without-pymalloc``
246
247 See :ref:`Memory Management <memory>`.
248
Victor Stinner4b9aad42020-11-02 16:49:54 +0100249 Default: ``PYMEM_ALLOCATOR_NOT_SET``.
250
Victor Stinner331a6a52019-05-27 16:39:22 +0200251 .. c:member:: int configure_locale
252
Victor Stinner4b9aad42020-11-02 16:49:54 +0100253 Set the LC_CTYPE locale to the user preferred locale?
254
255 If equals to 0, set :c:member:`~PyPreConfig.coerce_c_locale` and
256 :c:member:`~PyPreConfig.coerce_c_locale_warn` members to 0.
257
258 See the :term:`locale encoding`.
259
260 Default: ``1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200261
262 .. c:member:: int coerce_c_locale
263
Victor Stinner4b9aad42020-11-02 16:49:54 +0100264 If equals to 2, coerce the C locale.
265
266 If equals to 1, read the LC_CTYPE locale to decide if it should be
267 coerced.
268
269 See the :term:`locale encoding`.
270
271 Default: ``-1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200272
273 .. c:member:: int coerce_c_locale_warn
Victor Stinner88feaec2019-09-26 03:15:07 +0200274
Victor Stinner331a6a52019-05-27 16:39:22 +0200275 If non-zero, emit a warning if the C locale is coerced.
276
Victor Stinner4b9aad42020-11-02 16:49:54 +0100277 Default: ``-1`` in Python config, ``0`` in isolated config.
278
Victor Stinner331a6a52019-05-27 16:39:22 +0200279 .. c:member:: int dev_mode
280
Victor Stinner4b9aad42020-11-02 16:49:54 +0100281 If non-zero, enables the :ref:`Python Development Mode <devmode>`:
282 see :c:member:`PyConfig.dev_mode`.
283
284 Default: ``-1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200285
286 .. c:member:: int isolated
287
Victor Stinner4b9aad42020-11-02 16:49:54 +0100288 Isolated mode: see :c:member:`PyConfig.isolated`.
289
290 Default: ``0`` in Python mode, ``1`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200291
Victor Stinnere662c392020-11-01 23:07:23 +0100292 .. c:member:: int legacy_windows_fs_encoding
Victor Stinner331a6a52019-05-27 16:39:22 +0200293
Victor Stinnere662c392020-11-01 23:07:23 +0100294 If non-zero:
295
296 * Set :c:member:`PyPreConfig.utf8_mode` to ``0``,
297 * Set :c:member:`PyConfig.filesystem_encoding` to ``"mbcs"``,
298 * Set :c:member:`PyConfig.filesystem_errors` to ``"replace"``.
299
300 Initialized the from :envvar:`PYTHONLEGACYWINDOWSFSENCODING` environment
301 variable value.
Victor Stinner331a6a52019-05-27 16:39:22 +0200302
303 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
304 Windows specific code.
305
Victor Stinner4b9aad42020-11-02 16:49:54 +0100306 Default: ``0``.
307
Victor Stinner331a6a52019-05-27 16:39:22 +0200308 .. c:member:: int parse_argv
309
310 If non-zero, :c:func:`Py_PreInitializeFromArgs` and
311 :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the
312 same way the regular Python parses command line arguments: see
313 :ref:`Command Line Arguments <using-on-cmdline>`.
314
Victor Stinner4b9aad42020-11-02 16:49:54 +0100315 Default: ``1`` in Python config, ``0`` in isolated config.
316
Victor Stinner331a6a52019-05-27 16:39:22 +0200317 .. c:member:: int use_environment
318
Victor Stinner4b9aad42020-11-02 16:49:54 +0100319 Use :ref:`environment variables <using-on-envvars>`? See
320 :c:member:`PyConfig.use_environment`.
321
322 Default: ``1`` in Python config and ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200323
324 .. c:member:: int utf8_mode
325
Victor Stinner4b9aad42020-11-02 16:49:54 +0100326 If non-zero, enable the :ref:`Python UTF-8 Mode <utf8-mode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200327
Victor Stinner4b9aad42020-11-02 16:49:54 +0100328 Set by the :option:`-X utf8 <-X>` command line option and the
329 :envvar:`PYTHONUTF8` environment variable.
330
331 Default: ``-1`` in Python config and ``0`` in isolated config.
332
333
334.. _c-preinit:
335
336Preinitialize Python with PyPreConfig
Victor Stinnerdc42af82020-11-05 18:58:07 +0100337=====================================
Victor Stinner4b9aad42020-11-02 16:49:54 +0100338
339The preinitialization of Python:
340
341* Set the Python memory allocators (:c:member:`PyPreConfig.allocator`)
342* Configure the LC_CTYPE locale (:term:`locale encoding`)
343* Set the :ref:`Python UTF-8 Mode <utf8-mode>`
344 (:c:member:`PyPreConfig.utf8_mode`)
Victor Stinner331a6a52019-05-27 16:39:22 +0200345
Victor Stinnerdc42af82020-11-05 18:58:07 +0100346The current preconfiguration (``PyPreConfig`` type) is stored in
347``_PyRuntime.preconfig``.
348
Victor Stinner331a6a52019-05-27 16:39:22 +0200349Functions to preinitialize Python:
350
351.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
352
353 Preinitialize Python from *preconfig* preconfiguration.
354
Victor Stinnerdc42af82020-11-05 18:58:07 +0100355 *preconfig* must not be ``NULL``.
356
Victor Stinner331a6a52019-05-27 16:39:22 +0200357.. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv)
358
Victor Stinner4b9aad42020-11-02 16:49:54 +0100359 Preinitialize Python from *preconfig* preconfiguration.
360
361 Parse *argv* command line arguments (bytes strings) if
362 :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero.
Victor Stinner331a6a52019-05-27 16:39:22 +0200363
Victor Stinnerdc42af82020-11-05 18:58:07 +0100364 *preconfig* must not be ``NULL``.
365
Victor Stinner331a6a52019-05-27 16:39:22 +0200366.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)
367
Victor Stinner4b9aad42020-11-02 16:49:54 +0100368 Preinitialize Python from *preconfig* preconfiguration.
369
370 Parse *argv* command line arguments (wide strings) if
371 :c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero.
Victor Stinner331a6a52019-05-27 16:39:22 +0200372
Victor Stinnerdc42af82020-11-05 18:58:07 +0100373 *preconfig* must not be ``NULL``.
374
Victor Stinner331a6a52019-05-27 16:39:22 +0200375The caller is responsible to handle exceptions (error or exit) using
376:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
377
378For :ref:`Python Configuration <init-python-config>`
379(:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with
380command line arguments, the command line arguments must also be passed to
381preinitialize Python, since they have an effect on the pre-configuration
Victor Stinner88feaec2019-09-26 03:15:07 +0200382like encodings. For example, the :option:`-X utf8 <-X>` command line option
Victor Stinner4b9aad42020-11-02 16:49:54 +0100383enables the :ref:`Python UTF-8 Mode <utf8-mode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200384
385``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and
386before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator.
387It can be called before :c:func:`Py_PreInitialize` if
388:c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``.
389
390Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be
Victor Stinner4b9aad42020-11-02 16:49:54 +0100391used before the Python preinitialization, whereas calling directly ``malloc()``
392and ``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called
393before the Python preinitialization.
Victor Stinner331a6a52019-05-27 16:39:22 +0200394
Victor Stinner4b9aad42020-11-02 16:49:54 +0100395Example using the preinitialization to enable
396the :ref:`Python UTF-8 Mode <utf8-mode>`::
Victor Stinner331a6a52019-05-27 16:39:22 +0200397
Victor Stinner441b10c2019-09-28 04:28:35 +0200398 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +0200399 PyPreConfig preconfig;
Victor Stinner3c30a762019-10-01 10:56:37 +0200400 PyPreConfig_InitPythonConfig(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200401
402 preconfig.utf8_mode = 1;
403
Victor Stinner441b10c2019-09-28 04:28:35 +0200404 status = Py_PreInitialize(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200405 if (PyStatus_Exception(status)) {
406 Py_ExitStatusException(status);
407 }
408
Victor Stinner4b9aad42020-11-02 16:49:54 +0100409 /* at this point, Python speaks UTF-8 */
Victor Stinner331a6a52019-05-27 16:39:22 +0200410
411 Py_Initialize();
412 /* ... use Python API here ... */
413 Py_Finalize();
414
415
416PyConfig
Victor Stinnerdc42af82020-11-05 18:58:07 +0100417========
Victor Stinner331a6a52019-05-27 16:39:22 +0200418
419.. c:type:: PyConfig
420
421 Structure containing most parameters to configure Python.
422
Victor Stinner4b9aad42020-11-02 16:49:54 +0100423 When done, the :c:func:`PyConfig_Clear` function must be used to release the
424 configuration memory.
425
Victor Stinner331a6a52019-05-27 16:39:22 +0200426 Structure methods:
427
Victor Stinner8462a492019-10-01 12:06:16 +0200428 .. c:function:: void PyConfig_InitPythonConfig(PyConfig *config)
Victor Stinner331a6a52019-05-27 16:39:22 +0200429
Victor Stinner4b9aad42020-11-02 16:49:54 +0100430 Initialize configuration with the :ref:`Python Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200431 <init-python-config>`.
432
Victor Stinner8462a492019-10-01 12:06:16 +0200433 .. c:function:: void PyConfig_InitIsolatedConfig(PyConfig *config)
Victor Stinner331a6a52019-05-27 16:39:22 +0200434
Victor Stinner4b9aad42020-11-02 16:49:54 +0100435 Initialize configuration with the :ref:`Isolated Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200436 <init-isolated-conf>`.
437
438 .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str)
439
440 Copy the wide character string *str* into ``*config_str``.
441
Victor Stinner4b9aad42020-11-02 16:49:54 +0100442 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200443
444 .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str)
445
Victor Stinner4b9aad42020-11-02 16:49:54 +0100446 Decode *str* using :c:func:`Py_DecodeLocale` and set the result into
447 ``*config_str``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200448
Victor Stinner4b9aad42020-11-02 16:49:54 +0100449 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200450
451 .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv)
452
Victor Stinner4b9aad42020-11-02 16:49:54 +0100453 Set command line arguments (:c:member:`~PyConfig.argv` member of
454 *config*) from the *argv* list of wide character strings.
Victor Stinner331a6a52019-05-27 16:39:22 +0200455
Victor Stinner4b9aad42020-11-02 16:49:54 +0100456 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200457
458 .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv)
459
Victor Stinner4b9aad42020-11-02 16:49:54 +0100460 Set command line arguments (:c:member:`~PyConfig.argv` member of
461 *config*) from the *argv* list of bytes strings. Decode bytes using
462 :c:func:`Py_DecodeLocale`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200463
Victor Stinner4b9aad42020-11-02 16:49:54 +0100464 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200465
Victor Stinner36242fd2019-07-01 19:13:50 +0200466 .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items)
467
468 Set the list of wide strings *list* to *length* and *items*.
469
Victor Stinner4b9aad42020-11-02 16:49:54 +0100470 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner36242fd2019-07-01 19:13:50 +0200471
Victor Stinner331a6a52019-05-27 16:39:22 +0200472 .. c:function:: PyStatus PyConfig_Read(PyConfig *config)
473
474 Read all Python configuration.
475
476 Fields which are already initialized are left unchanged.
477
Victor Stinnerdc42af82020-11-05 18:58:07 +0100478 The :c:func:`PyConfig_Read` function only parses
479 :c:member:`PyConfig.argv` arguments once: :c:member:`PyConfig.parse_argv`
480 is set to ``2`` after arguments are parsed. Since Python arguments are
481 strippped from :c:member:`PyConfig.argv`, parsing arguments twice would
482 parse the application options as Python options.
483
Victor Stinner4b9aad42020-11-02 16:49:54 +0100484 :ref:`Preinitialize Python <c-preinit>` if needed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200485
Victor Stinnerdc42af82020-11-05 18:58:07 +0100486 .. versionchanged:: 3.10
487 The :c:member:`PyConfig.argv` arguments are now only parsed once,
488 :c:member:`PyConfig.parse_argv` is set to ``2`` after arguments are
489 parsed, and arguments are only parsed if
490 :c:member:`PyConfig.parse_argv` equals ``1``.
491
Victor Stinner331a6a52019-05-27 16:39:22 +0200492 .. c:function:: void PyConfig_Clear(PyConfig *config)
493
494 Release configuration memory.
495
Victor Stinner4b9aad42020-11-02 16:49:54 +0100496 Most ``PyConfig`` methods :ref:`preinitialize Python <c-preinit>` if needed.
497 In that case, the Python preinitialization configuration
498 (:c:type:`PyPreConfig`) in based on the :c:type:`PyConfig`. If configuration
499 fields which are in common with :c:type:`PyPreConfig` are tuned, they must
500 be set before calling a :c:type:`PyConfig` method:
Victor Stinner331a6a52019-05-27 16:39:22 +0200501
Victor Stinner4b9aad42020-11-02 16:49:54 +0100502 * :c:member:`PyConfig.dev_mode`
503 * :c:member:`PyConfig.isolated`
504 * :c:member:`PyConfig.parse_argv`
505 * :c:member:`PyConfig.use_environment`
Victor Stinner331a6a52019-05-27 16:39:22 +0200506
507 Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv`
Victor Stinner4b9aad42020-11-02 16:49:54 +0100508 is used, this method must be called before other methods, since the
Victor Stinner331a6a52019-05-27 16:39:22 +0200509 preinitialization configuration depends on command line arguments (if
510 :c:member:`parse_argv` is non-zero).
511
512 The caller of these methods is responsible to handle exceptions (error or
513 exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``.
514
515 Structure fields:
516
517 .. c:member:: PyWideStringList argv
518
Victor Stinner4b9aad42020-11-02 16:49:54 +0100519 Command line arguments: :data:`sys.argv`.
520
521 Set :c:member:`~PyConfig.parse_argv` to ``1`` to parse
522 :c:member:`~PyConfig.argv` the same way the regular Python parses Python
523 command line arguments and then to strip Python arguments from
524 :c:member:`~PyConfig.argv`.
525
526 If :c:member:`~PyConfig.argv` is empty, an empty string is added to
527 ensure that :data:`sys.argv` always exists and is never empty.
528
529 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200530
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200531 See also the :c:member:`~PyConfig.orig_argv` member.
532
Victor Stinner331a6a52019-05-27 16:39:22 +0200533 .. c:member:: wchar_t* base_exec_prefix
534
535 :data:`sys.base_exec_prefix`.
536
Victor Stinner4b9aad42020-11-02 16:49:54 +0100537 Default: ``NULL``.
538
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100539 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Victor Stinner4b9aad42020-11-02 16:49:54 +0100540
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200541 .. c:member:: wchar_t* base_executable
542
Victor Stinner4b9aad42020-11-02 16:49:54 +0100543 Python base executable: :data:`sys._base_executable`.
544
545 Set by the :envvar:`__PYVENV_LAUNCHER__` environment variable.
546
547 Set from :c:member:`PyConfig.executable` if ``NULL``.
548
549 Default: ``NULL``.
550
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100551 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200552
Victor Stinner331a6a52019-05-27 16:39:22 +0200553 .. c:member:: wchar_t* base_prefix
554
555 :data:`sys.base_prefix`.
556
Victor Stinner4b9aad42020-11-02 16:49:54 +0100557 Default: ``NULL``.
Sandro Mani8f023a22020-06-08 17:28:11 +0200558
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100559 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Sandro Mani8f023a22020-06-08 17:28:11 +0200560
Victor Stinner331a6a52019-05-27 16:39:22 +0200561 .. c:member:: int buffered_stdio
562
Victor Stinner4b9aad42020-11-02 16:49:54 +0100563 If equals to 0 and :c:member:`~PyConfig.configure_c_stdio` is non-zero,
564 disable buffering on the C streams stdout and stderr.
565
566 Set to 0 by the :option:`-u` command line option and the
567 :envvar:`PYTHONUNBUFFERED` environment variable.
Victor Stinner331a6a52019-05-27 16:39:22 +0200568
569 stdin is always opened in buffered mode.
570
Victor Stinner4b9aad42020-11-02 16:49:54 +0100571 Default: ``1``.
572
Victor Stinner331a6a52019-05-27 16:39:22 +0200573 .. c:member:: int bytes_warning
574
575 If equals to 1, issue a warning when comparing :class:`bytes` or
576 :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with
Victor Stinner4b9aad42020-11-02 16:49:54 +0100577 :class:`int`.
578
579 If equal or greater to 2, raise a :exc:`BytesWarning` exception in these
580 cases.
581
582 Incremented by the :option:`-b` command line option.
583
584 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200585
Inada Naoki48274832021-03-29 12:28:14 +0900586 .. c:member:: int warn_default_encoding
587
588 If non-zero, emit a :exc:`EncodingWarning` warning when :class:`io.TextIOWrapper`
589 uses its default encoding. See :ref:`io-encoding-warning` for details.
590
591 Default: ``0``.
592
593 .. versionadded:: 3.10
594
Victor Stinner331a6a52019-05-27 16:39:22 +0200595 .. c:member:: wchar_t* check_hash_pycs_mode
596
Victor Stinner4b9aad42020-11-02 16:49:54 +0100597 Control the validation behavior of hash-based ``.pyc`` files:
598 value of the :option:`--check-hash-based-pycs` command line option.
Victor Stinner331a6a52019-05-27 16:39:22 +0200599
Victor Stinner4b9aad42020-11-02 16:49:54 +0100600 Valid values:
Victor Stinner331a6a52019-05-27 16:39:22 +0200601
Victor Stinner4b9aad42020-11-02 16:49:54 +0100602 - ``L"always"``: Hash the source file for invalidation regardless of
603 value of the 'check_source' flag.
604 - ``L"never"``: Assume that hash-based pycs always are valid.
605 - ``L"default"``: The 'check_source' flag in hash-based pycs
606 determines invalidation.
607
608 Default: ``L"default"``.
609
610 See also :pep:`552` "Deterministic pycs".
Victor Stinner331a6a52019-05-27 16:39:22 +0200611
612 .. c:member:: int configure_c_stdio
613
Victor Stinner4b9aad42020-11-02 16:49:54 +0100614 If non-zero, configure C standard streams:
615
616 * On Windows, set the binary mode (``O_BINARY``) on stdin, stdout and
617 stderr.
618 * If :c:member:`~PyConfig.buffered_stdio` equals zero, disable buffering
619 of stdin, stdout and stderr streams.
620 * If :c:member:`~PyConfig.interactive` is non-zero, enable stream
621 buffering on stdin and stdout (only stdout on Windows).
622
623 Default: ``1`` in Python config, ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +0200624
625 .. c:member:: int dev_mode
626
Victor Stinnerb9783d22020-01-24 10:22:18 +0100627 If non-zero, enable the :ref:`Python Development Mode <devmode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200628
Victor Stinner4b9aad42020-11-02 16:49:54 +0100629 Default: ``-1`` in Python mode, ``0`` in isolated mode.
630
Victor Stinner331a6a52019-05-27 16:39:22 +0200631 .. c:member:: int dump_refs
632
Victor Stinner4b9aad42020-11-02 16:49:54 +0100633 Dump Python refererences?
634
Victor Stinner331a6a52019-05-27 16:39:22 +0200635 If non-zero, dump all objects which are still alive at exit.
636
Victor Stinner4b9aad42020-11-02 16:49:54 +0100637 Set to ``1`` by the :envvar:`PYTHONDUMPREFS` environment variable.
638
639 Need a special build of Python with the ``Py_TRACE_REFS`` macro defined.
640
641 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200642
643 .. c:member:: wchar_t* exec_prefix
644
Victor Stinner4b9aad42020-11-02 16:49:54 +0100645 The site-specific directory prefix where the platform-dependent Python
646 files are installed: :data:`sys.exec_prefix`.
647
648 Default: ``NULL``.
649
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100650 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200651
652 .. c:member:: wchar_t* executable
653
Victor Stinner4b9aad42020-11-02 16:49:54 +0100654 The absolute path of the executable binary for the Python interpreter:
Victor Stinner331a6a52019-05-27 16:39:22 +0200655 :data:`sys.executable`.
656
Victor Stinner4b9aad42020-11-02 16:49:54 +0100657 Default: ``NULL``.
658
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100659 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Victor Stinner4b9aad42020-11-02 16:49:54 +0100660
Victor Stinner331a6a52019-05-27 16:39:22 +0200661 .. c:member:: int faulthandler
662
Victor Stinner4b9aad42020-11-02 16:49:54 +0100663 Enable faulthandler?
664
Victor Stinner88feaec2019-09-26 03:15:07 +0200665 If non-zero, call :func:`faulthandler.enable` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200666
Victor Stinner4b9aad42020-11-02 16:49:54 +0100667 Set to ``1`` by :option:`-X faulthandler <-X>` and the
668 :envvar:`PYTHONFAULTHANDLER` environment variable.
669
670 Default: ``-1`` in Python mode, ``0`` in isolated mode.
671
Victor Stinner331a6a52019-05-27 16:39:22 +0200672 .. c:member:: wchar_t* filesystem_encoding
673
Victor Stinner4b9aad42020-11-02 16:49:54 +0100674 :term:`Filesystem encoding <filesystem encoding and error handler>`:
675 :func:`sys.getfilesystemencoding`.
Victor Stinnere662c392020-11-01 23:07:23 +0100676
677 On macOS, Android and VxWorks: use ``"utf-8"`` by default.
678
679 On Windows: use ``"utf-8"`` by default, or ``"mbcs"`` if
680 :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of
681 :c:type:`PyPreConfig` is non-zero.
682
683 Default encoding on other platforms:
684
685 * ``"utf-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero.
686 * ``"ascii"`` if Python detects that ``nl_langinfo(CODESET)`` announces
687 the ASCII encoding (or Roman8 encoding on HP-UX), whereas the
688 ``mbstowcs()`` function decodes from a different encoding (usually
689 Latin1).
690 * ``"utf-8"`` if ``nl_langinfo(CODESET)`` returns an empty string.
Victor Stinner4b9aad42020-11-02 16:49:54 +0100691 * Otherwise, use the :term:`locale encoding`:
Victor Stinnere662c392020-11-01 23:07:23 +0100692 ``nl_langinfo(CODESET)`` result.
693
694 At Python statup, the encoding name is normalized to the Python codec
695 name. For example, ``"ANSI_X3.4-1968"`` is replaced with ``"ascii"``.
696
697 See also the :c:member:`~PyConfig.filesystem_errors` member.
Victor Stinner331a6a52019-05-27 16:39:22 +0200698
699 .. c:member:: wchar_t* filesystem_errors
700
Victor Stinner4b9aad42020-11-02 16:49:54 +0100701 :term:`Filesystem error handler <filesystem encoding and error handler>`:
702 :func:`sys.getfilesystemencodeerrors`.
Victor Stinnere662c392020-11-01 23:07:23 +0100703
704 On Windows: use ``"surrogatepass"`` by default, or ``"replace"`` if
705 :c:member:`~PyPreConfig.legacy_windows_fs_encoding` of
706 :c:type:`PyPreConfig` is non-zero.
707
708 On other platforms: use ``"surrogateescape"`` by default.
709
710 Supported error handlers:
711
712 * ``"strict"``
713 * ``"surrogateescape"``
714 * ``"surrogatepass"`` (only supported with the UTF-8 encoding)
715
716 See also the :c:member:`~PyConfig.filesystem_encoding` member.
Victor Stinner331a6a52019-05-27 16:39:22 +0200717
718 .. c:member:: unsigned long hash_seed
719 .. c:member:: int use_hash_seed
720
721 Randomized hash function seed.
722
723 If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly
Victor Stinner4b9aad42020-11-02 16:49:54 +0100724 at Python startup, and :c:member:`~PyConfig.hash_seed` is ignored.
725
726 Set by the :envvar:`PYTHONHASHSEED` environment variable.
727
728 Default *use_hash_seed* value: ``-1`` in Python mode, ``0`` in isolated
729 mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200730
731 .. c:member:: wchar_t* home
732
733 Python home directory.
734
Victor Stinner4b9aad42020-11-02 16:49:54 +0100735 If :c:func:`Py_SetPythonHome` has been called, use its argument if it is
736 not ``NULL``.
737
738 Set by the :envvar:`PYTHONHOME` environment variable.
739
740 Default: ``NULL``.
741
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100742 Part of the :ref:`Python Path Configuration <init-path-config>` input.
Victor Stinner88feaec2019-09-26 03:15:07 +0200743
Victor Stinner331a6a52019-05-27 16:39:22 +0200744 .. c:member:: int import_time
745
746 If non-zero, profile import time.
747
Victor Stinner4b9aad42020-11-02 16:49:54 +0100748 Set the ``1`` by the :option:`-X importtime <-X>` option and the
749 :envvar:`PYTHONPROFILEIMPORTTIME` environment variable.
750
751 Default: ``0``.
752
Victor Stinner331a6a52019-05-27 16:39:22 +0200753 .. c:member:: int inspect
754
755 Enter interactive mode after executing a script or a command.
756
Victor Stinner4b9aad42020-11-02 16:49:54 +0100757 If greater than 0, enable inspect: when a script is passed as first
758 argument or the -c option is used, enter interactive mode after executing
759 the script or the command, even when :data:`sys.stdin` does not appear to
760 be a terminal.
761
762 Incremented by the :option:`-i` command line option. Set to ``1`` if the
763 :envvar:`PYTHONINSPECT` environment variable is non-empty.
764
765 Default: ``0``.
766
Victor Stinner331a6a52019-05-27 16:39:22 +0200767 .. c:member:: int install_signal_handlers
768
Victor Stinner4b9aad42020-11-02 16:49:54 +0100769 Install Python signal handlers?
770
771 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200772
773 .. c:member:: int interactive
774
Victor Stinner4b9aad42020-11-02 16:49:54 +0100775 If greater than 0, enable the interactive mode (REPL).
776
777 Incremented by the :option:`-i` command line option.
778
779 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200780
781 .. c:member:: int isolated
782
783 If greater than 0, enable isolated mode:
784
785 * :data:`sys.path` contains neither the script's directory (computed from
786 ``argv[0]`` or the current directory) nor the user's site-packages
787 directory.
788 * Python REPL doesn't import :mod:`readline` nor enable default readline
789 configuration on interactive prompts.
790 * Set :c:member:`~PyConfig.use_environment` and
791 :c:member:`~PyConfig.user_site_directory` to 0.
792
Victor Stinner4b9aad42020-11-02 16:49:54 +0100793 Default: ``0`` in Python mode, ``1`` in isolated mode.
794
795 See also :c:member:`PyPreConfig.isolated`.
796
Victor Stinner331a6a52019-05-27 16:39:22 +0200797 .. c:member:: int legacy_windows_stdio
798
799 If non-zero, use :class:`io.FileIO` instead of
800 :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout`
801 and :data:`sys.stderr`.
802
Victor Stinner4b9aad42020-11-02 16:49:54 +0100803 Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSSTDIO` environment
804 variable is set to a non-empty string.
805
Victor Stinner331a6a52019-05-27 16:39:22 +0200806 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
807 Windows specific code.
808
Victor Stinner4b9aad42020-11-02 16:49:54 +0100809 Default: ``0``.
810
811 See also the :pep:`528` (Change Windows console encoding to UTF-8).
812
Victor Stinner331a6a52019-05-27 16:39:22 +0200813 .. c:member:: int malloc_stats
814
815 If non-zero, dump statistics on :ref:`Python pymalloc memory allocator
816 <pymalloc>` at exit.
817
Victor Stinner4b9aad42020-11-02 16:49:54 +0100818 Set to ``1`` by the :envvar:`PYTHONMALLOCSTATS` environment variable.
819
Victor Stinner331a6a52019-05-27 16:39:22 +0200820 The option is ignored if Python is built using ``--without-pymalloc``.
821
Victor Stinner4b9aad42020-11-02 16:49:54 +0100822 Default: ``0``.
823
824 .. c:member:: wchar_t* platlibdir
825
826 Platform library directory name: :data:`sys.platlibdir`.
827
828 Set by the :envvar:`PYTHONPLATLIBDIR` environment variable.
829
830 Default: value of the ``PLATLIBDIR`` macro which is set at configure time
831 by ``--with-platlibdir`` (default: ``"lib"``).
832
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100833 Part of the :ref:`Python Path Configuration <init-path-config>` input.
Victor Stinner4b9aad42020-11-02 16:49:54 +0100834
835 .. versionadded:: 3.9
836
Victor Stinner331a6a52019-05-27 16:39:22 +0200837 .. c:member:: wchar_t* pythonpath_env
838
Victor Stinner4b9aad42020-11-02 16:49:54 +0100839 Module search paths (:data:`sys.path`) as a string separated by ``DELIM``
Victor Stinner331a6a52019-05-27 16:39:22 +0200840 (:data:`os.path.pathsep`).
841
Victor Stinner4b9aad42020-11-02 16:49:54 +0100842 Set by the :envvar:`PYTHONPATH` environment variable.
843
844 Default: ``NULL``.
845
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100846 Part of the :ref:`Python Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200847
848 .. c:member:: PyWideStringList module_search_paths
849 .. c:member:: int module_search_paths_set
850
Victor Stinner4b9aad42020-11-02 16:49:54 +0100851 Module search paths: :data:`sys.path`.
852
853 If :c:member:`~PyConfig.module_search_paths_set` is equal to 0, the
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100854 function calculating the :ref:`Python Path Configuration <init-path-config>`
Victor Stinner4b9aad42020-11-02 16:49:54 +0100855 overrides the :c:member:`~PyConfig.module_search_paths` and sets
856 :c:member:`~PyConfig.module_search_paths_set` to ``1``.
857
858 Default: empty list (``module_search_paths``) and ``0``
859 (``module_search_paths_set``).
860
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100861 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200862
863 .. c:member:: int optimization_level
864
865 Compilation optimization level:
866
Victor Stinner4b9aad42020-11-02 16:49:54 +0100867 * ``0``: Peephole optimizer, set ``__debug__`` to ``True``.
868 * ``1``: Level 0, remove assertions, set ``__debug__`` to ``False``.
869 * ``2``: Level 1, strip docstrings.
870
871 Incremented by the :option:`-O` command line option. Set to the
872 :envvar:`PYTHONOPTIMIZE` environment variable value.
873
874 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200875
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200876 .. c:member:: PyWideStringList orig_argv
877
878 The list of the original command line arguments passed to the Python
Victor Stinner4b9aad42020-11-02 16:49:54 +0100879 executable: :data:`sys.orig_argv`.
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200880
881 If :c:member:`~PyConfig.orig_argv` list is empty and
882 :c:member:`~PyConfig.argv` is not a list only containing an empty
Victor Stinnerdc42af82020-11-05 18:58:07 +0100883 string, :c:func:`PyConfig_Read` copies :c:member:`~PyConfig.argv` into
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200884 :c:member:`~PyConfig.orig_argv` before modifying
885 :c:member:`~PyConfig.argv` (if :c:member:`~PyConfig.parse_argv` is
886 non-zero).
887
888 See also the :c:member:`~PyConfig.argv` member and the
889 :c:func:`Py_GetArgcArgv` function.
890
Victor Stinner4b9aad42020-11-02 16:49:54 +0100891 Default: empty list.
892
Victor Stinnerdd8a93e2020-06-30 00:49:03 +0200893 .. versionadded:: 3.10
894
Victor Stinner331a6a52019-05-27 16:39:22 +0200895 .. c:member:: int parse_argv
896
Victor Stinner4b9aad42020-11-02 16:49:54 +0100897 Parse command line arguments?
898
Victor Stinnerdc42af82020-11-05 18:58:07 +0100899 If equals to ``1``, parse :c:member:`~PyConfig.argv` the same way the regular
Victor Stinner4b9aad42020-11-02 16:49:54 +0100900 Python parses :ref:`command line arguments <using-on-cmdline>`, and strip
901 Python arguments from :c:member:`~PyConfig.argv`.
902
Victor Stinnerdc42af82020-11-05 18:58:07 +0100903 The :c:func:`PyConfig_Read` function only parses
904 :c:member:`PyConfig.argv` arguments once: :c:member:`PyConfig.parse_argv`
905 is set to ``2`` after arguments are parsed. Since Python arguments are
906 strippped from :c:member:`PyConfig.argv`, parsing arguments twice would
907 parse the application options as Python options.
908
Victor Stinner4b9aad42020-11-02 16:49:54 +0100909 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200910
Victor Stinnerdc42af82020-11-05 18:58:07 +0100911 .. versionchanged:: 3.10
912 The :c:member:`PyConfig.argv` arguments are now only parsed if
913 :c:member:`PyConfig.parse_argv` equals to ``1``.
914
Victor Stinner331a6a52019-05-27 16:39:22 +0200915 .. c:member:: int parser_debug
916
Victor Stinner4b9aad42020-11-02 16:49:54 +0100917 Parser debug mode. If greater than 0, turn on parser debugging output (for expert only, depending
Victor Stinner331a6a52019-05-27 16:39:22 +0200918 on compilation options).
919
Victor Stinner4b9aad42020-11-02 16:49:54 +0100920 Incremented by the :option:`-d` command line option. Set to the
921 :envvar:`PYTHONDEBUG` environment variable value.
922
923 Default: ``0``.
924
Victor Stinner331a6a52019-05-27 16:39:22 +0200925 .. c:member:: int pathconfig_warnings
926
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100927 On Unix, if non-zero, calculating the :ref:`Python Path Configuration
Victor Stinner4b9aad42020-11-02 16:49:54 +0100928 <init-path-config>` can log warnings into ``stderr``. If equals to 0,
929 suppress these warnings.
930
931 It has no effect on Windows.
932
933 Default: ``1`` in Python mode, ``0`` in isolated mode.
934
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100935 Part of the :ref:`Python Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200936
937 .. c:member:: wchar_t* prefix
938
Victor Stinner4b9aad42020-11-02 16:49:54 +0100939 The site-specific directory prefix where the platform independent Python
940 files are installed: :data:`sys.prefix`.
941
942 Default: ``NULL``.
943
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100944 Part of the :ref:`Python Path Configuration <init-path-config>` output.
Victor Stinner331a6a52019-05-27 16:39:22 +0200945
946 .. c:member:: wchar_t* program_name
947
Victor Stinner4b9aad42020-11-02 16:49:54 +0100948 Program name used to initialize :c:member:`~PyConfig.executable` and in
949 early error messages during Python initialization.
950
951 * If :func:`Py_SetProgramName` has been called, use its argument.
952 * On macOS, use :envvar:`PYTHONEXECUTABLE` environment variable if set.
953 * If the ``WITH_NEXT_FRAMEWORK`` macro is defined, use
954 :envvar:`__PYVENV_LAUNCHER__` environment variable if set.
955 * Use ``argv[0]`` of :c:member:`~PyConfig.argv` if available and
956 non-empty.
957 * Otherwise, use ``L"python"`` on Windows, or ``L"python3"`` on other
958 platforms.
959
960 Default: ``NULL``.
961
Victor Stinnerace3f9a2020-11-10 21:10:22 +0100962 Part of the :ref:`Python Path Configuration <init-path-config>` input.
Victor Stinner331a6a52019-05-27 16:39:22 +0200963
964 .. c:member:: wchar_t* pycache_prefix
965
Victor Stinner4b9aad42020-11-02 16:49:54 +0100966 Directory where cached ``.pyc`` files are written:
967 :data:`sys.pycache_prefix`.
968
969 Set by the :option:`-X pycache_prefix=PATH <-X>` command line option and
970 the :envvar:`PYTHONPYCACHEPREFIX` environment variable.
Victor Stinner88feaec2019-09-26 03:15:07 +0200971
Serhiy Storchakae835b312019-10-30 21:37:16 +0200972 If ``NULL``, :data:`sys.pycache_prefix` is set to ``None``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200973
Victor Stinner4b9aad42020-11-02 16:49:54 +0100974 Default: ``NULL``.
975
Victor Stinner331a6a52019-05-27 16:39:22 +0200976 .. c:member:: int quiet
977
Victor Stinner4b9aad42020-11-02 16:49:54 +0100978 Quiet mode. If greater than 0, don't display the copyright and version at
979 Python startup in interactive mode.
980
981 Incremented by the :option:`-q` command line option.
982
983 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200984
985 .. c:member:: wchar_t* run_command
986
Victor Stinner4b9aad42020-11-02 16:49:54 +0100987 Value of the :option:`-c` command line option.
988
989 Used by :c:func:`Py_RunMain`.
990
991 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200992
993 .. c:member:: wchar_t* run_filename
994
Victor Stinner4b9aad42020-11-02 16:49:54 +0100995 Filename passed on the command line: trailing command line argument
996 without :option:`-c` or :option:`-m`.
997
998 For example, it is set to ``script.py`` by the ``python3 script.py arg``
999 command.
1000
1001 Used by :c:func:`Py_RunMain`.
1002
1003 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +02001004
1005 .. c:member:: wchar_t* run_module
1006
Victor Stinner4b9aad42020-11-02 16:49:54 +01001007 Value of the :option:`-m` command line option.
1008
1009 Used by :c:func:`Py_RunMain`.
1010
1011 Default: ``NULL``.
Victor Stinner331a6a52019-05-27 16:39:22 +02001012
Victor Stinner331a6a52019-05-27 16:39:22 +02001013 .. c:member:: int show_ref_count
1014
1015 Show total reference count at exit?
1016
Victor Stinner88feaec2019-09-26 03:15:07 +02001017 Set to 1 by :option:`-X showrefcount <-X>` command line option.
1018
Victor Stinner331a6a52019-05-27 16:39:22 +02001019 Need a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
1020
Victor Stinner4b9aad42020-11-02 16:49:54 +01001021 Default: ``0``.
1022
Victor Stinner331a6a52019-05-27 16:39:22 +02001023 .. c:member:: int site_import
1024
1025 Import the :mod:`site` module at startup?
1026
Victor Stinner4b9aad42020-11-02 16:49:54 +01001027 If equal to zero, disable the import of the module site and the
1028 site-dependent manipulations of :data:`sys.path` that it entails.
1029
1030 Also disable these manipulations if the :mod:`site` module is explicitly
1031 imported later (call :func:`site.main` if you want them to be triggered).
1032
1033 Set to ``0`` by the :option:`-S` command line option.
1034
1035 :data:`sys.flags.no_site` is set to the inverted value of
1036 :c:member:`~PyConfig.site_import`.
1037
1038 Default: ``1``.
1039
Victor Stinner331a6a52019-05-27 16:39:22 +02001040 .. c:member:: int skip_source_first_line
1041
Victor Stinner4b9aad42020-11-02 16:49:54 +01001042 If non-zero, skip the first line of the :c:member:`PyConfig.run_filename`
1043 source.
1044
1045 It allows the usage of non-Unix forms of ``#!cmd``. This is intended for
1046 a DOS specific hack only.
1047
1048 Set to ``1`` by the :option:`-x` command line option.
1049
1050 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +02001051
1052 .. c:member:: wchar_t* stdio_encoding
1053 .. c:member:: wchar_t* stdio_errors
1054
1055 Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
Victor Stinner4b9aad42020-11-02 16:49:54 +01001056 :data:`sys.stderr` (but :data:`sys.stderr` always uses
1057 ``"backslashreplace"`` error handler).
1058
1059 If :c:func:`Py_SetStandardStreamEncoding` has been called, use its
1060 *error* and *errors* arguments if they are not ``NULL``.
1061
1062 Use the :envvar:`PYTHONIOENCODING` environment variable if it is
1063 non-empty.
1064
1065 Default encoding:
1066
1067 * ``"UTF-8"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero.
1068 * Otherwise, use the :term:`locale encoding`.
1069
1070 Default error handler:
1071
1072 * On Windows: use ``"surrogateescape"``.
1073 * ``"surrogateescape"`` if :c:member:`PyPreConfig.utf8_mode` is non-zero,
1074 or if the LC_CTYPE locale is "C" or "POSIX".
1075 * ``"strict"`` otherwise.
Victor Stinner331a6a52019-05-27 16:39:22 +02001076
1077 .. c:member:: int tracemalloc
1078
Victor Stinner4b9aad42020-11-02 16:49:54 +01001079 Enable tracemalloc?
1080
Victor Stinner88feaec2019-09-26 03:15:07 +02001081 If non-zero, call :func:`tracemalloc.start` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +02001082
Victor Stinner4b9aad42020-11-02 16:49:54 +01001083 Set by :option:`-X tracemalloc=N <-X>` command line option and by the
1084 :envvar:`PYTHONTRACEMALLOC` environment variable.
1085
1086 Default: ``-1`` in Python mode, ``0`` in isolated mode.
1087
Victor Stinner331a6a52019-05-27 16:39:22 +02001088 .. c:member:: int use_environment
1089
Victor Stinner4b9aad42020-11-02 16:49:54 +01001090 Use :ref:`environment variables <using-on-envvars>`?
1091
1092 If equals to zero, ignore the :ref:`environment variables
1093 <using-on-envvars>`.
1094
1095 Default: ``1`` in Python config and ``0`` in isolated config.
Victor Stinner331a6a52019-05-27 16:39:22 +02001096
1097 .. c:member:: int user_site_directory
1098
Victor Stinner4b9aad42020-11-02 16:49:54 +01001099 If non-zero, add the user site directory to :data:`sys.path`.
1100
1101 Set to ``0`` by the :option:`-s` and :option:`-I` command line options.
1102
1103 Set to ``0`` by the :envvar:`PYTHONNOUSERSITE` environment variable.
1104
1105 Default: ``1`` in Python mode, ``0`` in isolated mode.
Victor Stinner331a6a52019-05-27 16:39:22 +02001106
1107 .. c:member:: int verbose
1108
Victor Stinner4b9aad42020-11-02 16:49:54 +01001109 Verbose mode. If greater than 0, print a message each time a module is
1110 imported, showing the place (filename or built-in module) from which
1111 it is loaded.
1112
1113 If greater or equal to 2, print a message for each file that is checked
1114 for when searching for a module. Also provides information on module
1115 cleanup at exit.
1116
1117 Incremented by the :option:`-v` command line option.
1118
1119 Set to the :envvar:`PYTHONVERBOSE` environment variable value.
1120
1121 Default: ``0``.
Victor Stinner331a6a52019-05-27 16:39:22 +02001122
1123 .. c:member:: PyWideStringList warnoptions
1124
Victor Stinner4b9aad42020-11-02 16:49:54 +01001125 Options of the :mod:`warnings` module to build warnings filters, lowest
1126 to highest priority: :data:`sys.warnoptions`.
Victor Stinnerfb4ae152019-09-30 01:40:17 +02001127
1128 The :mod:`warnings` module adds :data:`sys.warnoptions` in the reverse
1129 order: the last :c:member:`PyConfig.warnoptions` item becomes the first
1130 item of :data:`warnings.filters` which is checked first (highest
1131 priority).
Victor Stinner331a6a52019-05-27 16:39:22 +02001132
Victor Stinner4b9aad42020-11-02 16:49:54 +01001133 Default: empty list.
1134
Victor Stinner331a6a52019-05-27 16:39:22 +02001135 .. c:member:: int write_bytecode
1136
Victor Stinner4b9aad42020-11-02 16:49:54 +01001137 If equal to 0, Python won't try to write ``.pyc`` files on the import of
1138 source modules.
1139
1140 Set to ``0`` by the :option:`-B` command line option and the
1141 :envvar:`PYTHONDONTWRITEBYTECODE` environment variable.
Victor Stinner331a6a52019-05-27 16:39:22 +02001142
Victor Stinner88feaec2019-09-26 03:15:07 +02001143 :data:`sys.dont_write_bytecode` is initialized to the inverted value of
1144 :c:member:`~PyConfig.write_bytecode`.
1145
Victor Stinner4b9aad42020-11-02 16:49:54 +01001146 Default: ``1``.
1147
Victor Stinner331a6a52019-05-27 16:39:22 +02001148 .. c:member:: PyWideStringList xoptions
1149
Victor Stinner4b9aad42020-11-02 16:49:54 +01001150 Values of the :option:`-X` command line options: :data:`sys._xoptions`.
Victor Stinner331a6a52019-05-27 16:39:22 +02001151
Victor Stinner4b9aad42020-11-02 16:49:54 +01001152 Default: empty list.
Victor Stinner331a6a52019-05-27 16:39:22 +02001153
Victor Stinner4b9aad42020-11-02 16:49:54 +01001154If :c:member:`~PyConfig.parse_argv` is non-zero, :c:member:`~PyConfig.argv`
1155arguments are parsed the same way the regular Python parses :ref:`command line
1156arguments <using-on-cmdline>`, and Python arguments are stripped from
1157:c:member:`~PyConfig.argv`.
1158
1159The :c:member:`~PyConfig.xoptions` options are parsed to set other options: see
1160the :option:`-X` command line option.
Victor Stinner331a6a52019-05-27 16:39:22 +02001161
Victor Stinnerc6e5c112020-02-03 15:17:15 +01001162.. versionchanged:: 3.9
1163
1164 The ``show_alloc_count`` field has been removed.
1165
Victor Stinner331a6a52019-05-27 16:39:22 +02001166
1167Initialization with PyConfig
Victor Stinnerdc42af82020-11-05 18:58:07 +01001168============================
Victor Stinner331a6a52019-05-27 16:39:22 +02001169
1170Function to initialize Python:
1171
1172.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
1173
1174 Initialize Python from *config* configuration.
1175
1176The caller is responsible to handle exceptions (error or exit) using
1177:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
1178
Victor Stinner4b9aad42020-11-02 16:49:54 +01001179If :c:func:`PyImport_FrozenModules`, :c:func:`PyImport_AppendInittab` or
1180:c:func:`PyImport_ExtendInittab` are used, they must be set or called after
1181Python preinitialization and before the Python initialization.
Victor Stinner331a6a52019-05-27 16:39:22 +02001182
Victor Stinnerdc42af82020-11-05 18:58:07 +01001183The current configuration (``PyConfig`` type) is stored in
1184``PyInterpreterState.config``.
1185
Victor Stinner331a6a52019-05-27 16:39:22 +02001186Example setting the program name::
1187
1188 void init_python(void)
1189 {
1190 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +02001191
Victor Stinner8462a492019-10-01 12:06:16 +02001192 PyConfig config;
1193 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001194
1195 /* Set the program name. Implicitly preinitialize Python. */
1196 status = PyConfig_SetString(&config, &config.program_name,
1197 L"/path/to/my_program");
1198 if (PyStatus_Exception(status)) {
Victor Stinnerdc42af82020-11-05 18:58:07 +01001199 goto exception;
Victor Stinner331a6a52019-05-27 16:39:22 +02001200 }
1201
1202 status = Py_InitializeFromConfig(&config);
1203 if (PyStatus_Exception(status)) {
Victor Stinnerdc42af82020-11-05 18:58:07 +01001204 goto exception;
Victor Stinner331a6a52019-05-27 16:39:22 +02001205 }
1206 PyConfig_Clear(&config);
1207 return;
1208
Victor Stinnerdc42af82020-11-05 18:58:07 +01001209 exception:
Victor Stinner331a6a52019-05-27 16:39:22 +02001210 PyConfig_Clear(&config);
1211 Py_ExitStatusException(status);
1212 }
1213
1214More complete example modifying the default configuration, read the
1215configuration, and then override some parameters::
1216
1217 PyStatus init_python(const char *program_name)
1218 {
1219 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +02001220
Victor Stinner8462a492019-10-01 12:06:16 +02001221 PyConfig config;
1222 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001223
Gurupad Hegde6c7bb382019-12-28 17:16:02 -05001224 /* Set the program name before reading the configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001225 (decode byte string from the locale encoding).
1226
1227 Implicitly preinitialize Python. */
1228 status = PyConfig_SetBytesString(&config, &config.program_name,
Victor Stinner4b9aad42020-11-02 16:49:54 +01001229 program_name);
Victor Stinner331a6a52019-05-27 16:39:22 +02001230 if (PyStatus_Exception(status)) {
1231 goto done;
1232 }
1233
1234 /* Read all configuration at once */
1235 status = PyConfig_Read(&config);
1236 if (PyStatus_Exception(status)) {
1237 goto done;
1238 }
1239
1240 /* Append our custom search path to sys.path */
1241 status = PyWideStringList_Append(&config.module_search_paths,
Victor Stinner88feaec2019-09-26 03:15:07 +02001242 L"/path/to/more/modules");
Victor Stinner331a6a52019-05-27 16:39:22 +02001243 if (PyStatus_Exception(status)) {
1244 goto done;
1245 }
1246
1247 /* Override executable computed by PyConfig_Read() */
1248 status = PyConfig_SetString(&config, &config.executable,
1249 L"/path/to/my_executable");
1250 if (PyStatus_Exception(status)) {
1251 goto done;
1252 }
1253
1254 status = Py_InitializeFromConfig(&config);
1255
1256 done:
1257 PyConfig_Clear(&config);
1258 return status;
1259 }
1260
1261
1262.. _init-isolated-conf:
1263
1264Isolated Configuration
Victor Stinnerdc42af82020-11-05 18:58:07 +01001265======================
Victor Stinner331a6a52019-05-27 16:39:22 +02001266
1267:c:func:`PyPreConfig_InitIsolatedConfig` and
1268:c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to
1269isolate Python from the system. For example, to embed Python into an
1270application.
1271
1272This configuration ignores global configuration variables, environments
Victor Stinner88feaec2019-09-26 03:15:07 +02001273variables, command line arguments (:c:member:`PyConfig.argv` is not parsed)
1274and user site directory. The C standard streams (ex: ``stdout``) and the
1275LC_CTYPE locale are left unchanged. Signal handlers are not installed.
Victor Stinner331a6a52019-05-27 16:39:22 +02001276
1277Configuration files are still used with this configuration. Set the
Victor Stinnerace3f9a2020-11-10 21:10:22 +01001278:ref:`Python Path Configuration <init-path-config>` ("output fields") to ignore these
Victor Stinner331a6a52019-05-27 16:39:22 +02001279configuration files and avoid the function computing the default path
1280configuration.
1281
1282
1283.. _init-python-config:
1284
1285Python Configuration
Victor Stinnerdc42af82020-11-05 18:58:07 +01001286====================
Victor Stinner331a6a52019-05-27 16:39:22 +02001287
1288:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
1289functions create a configuration to build a customized Python which behaves as
1290the regular Python.
1291
1292Environments variables and command line arguments are used to configure
1293Python, whereas global configuration variables are ignored.
1294
Victor Stinner4b9aad42020-11-02 16:49:54 +01001295This function enables C locale coercion (:pep:`538`)
1296and :ref:`Python UTF-8 Mode <utf8-mode>`
Victor Stinner331a6a52019-05-27 16:39:22 +02001297(:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and
1298:envvar:`PYTHONCOERCECLOCALE` environment variables.
1299
Victor Stinner331a6a52019-05-27 16:39:22 +02001300
1301.. _init-path-config:
1302
Victor Stinnerace3f9a2020-11-10 21:10:22 +01001303Python Path Configuration
1304=========================
Victor Stinner331a6a52019-05-27 16:39:22 +02001305
1306:c:type:`PyConfig` contains multiple fields for the path configuration:
1307
Victor Stinner8bf39b62019-09-26 02:22:35 +02001308* Path configuration inputs:
Victor Stinner331a6a52019-05-27 16:39:22 +02001309
1310 * :c:member:`PyConfig.home`
Sandro Mani8f023a22020-06-08 17:28:11 +02001311 * :c:member:`PyConfig.platlibdir`
Victor Stinner331a6a52019-05-27 16:39:22 +02001312 * :c:member:`PyConfig.pathconfig_warnings`
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001313 * :c:member:`PyConfig.program_name`
1314 * :c:member:`PyConfig.pythonpath_env`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001315 * current working directory: to get absolute paths
1316 * ``PATH`` environment variable to get the program full path
1317 (from :c:member:`PyConfig.program_name`)
1318 * ``__PYVENV_LAUNCHER__`` environment variable
1319 * (Windows only) Application paths in the registry under
1320 "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and
1321 HKEY_LOCAL_MACHINE (where X.Y is the Python version).
Victor Stinner331a6a52019-05-27 16:39:22 +02001322
1323* Path configuration output fields:
1324
Victor Stinner8bf39b62019-09-26 02:22:35 +02001325 * :c:member:`PyConfig.base_exec_prefix`
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001326 * :c:member:`PyConfig.base_executable`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001327 * :c:member:`PyConfig.base_prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +02001328 * :c:member:`PyConfig.exec_prefix`
1329 * :c:member:`PyConfig.executable`
Victor Stinner331a6a52019-05-27 16:39:22 +02001330 * :c:member:`PyConfig.module_search_paths_set`,
1331 :c:member:`PyConfig.module_search_paths`
Victor Stinner8bf39b62019-09-26 02:22:35 +02001332 * :c:member:`PyConfig.prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +02001333
Victor Stinner8bf39b62019-09-26 02:22:35 +02001334If at least one "output field" is not set, Python calculates the path
Victor Stinner331a6a52019-05-27 16:39:22 +02001335configuration to fill unset fields. If
1336:c:member:`~PyConfig.module_search_paths_set` is equal to 0,
Min ho Kim39d87b52019-08-31 06:21:19 +10001337:c:member:`~PyConfig.module_search_paths` is overridden and
Victor Stinner331a6a52019-05-27 16:39:22 +02001338:c:member:`~PyConfig.module_search_paths_set` is set to 1.
1339
Victor Stinner8bf39b62019-09-26 02:22:35 +02001340It is possible to completely ignore the function calculating the default
Victor Stinner331a6a52019-05-27 16:39:22 +02001341path configuration by setting explicitly all path configuration output
1342fields listed above. A string is considered as set even if it is non-empty.
1343``module_search_paths`` is considered as set if
1344``module_search_paths_set`` is set to 1. In this case, path
1345configuration input fields are ignored as well.
1346
1347Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when
Victor Stinner8bf39b62019-09-26 02:22:35 +02001348calculating the path configuration (Unix only, Windows does not log any warning).
Victor Stinner331a6a52019-05-27 16:39:22 +02001349
1350If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix`
1351fields are not set, they inherit their value from :c:member:`~PyConfig.prefix`
1352and :c:member:`~PyConfig.exec_prefix` respectively.
1353
1354:c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`:
1355
1356* If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a
1357 ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to
1358 :data:`sys.path`.
1359* If :c:member:`~PyConfig.isolated` is zero:
1360
1361 * If :c:member:`~PyConfig.run_module` is set, prepend the current directory
1362 to :data:`sys.path`. Do nothing if the current directory cannot be read.
1363 * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the
1364 filename to :data:`sys.path`.
1365 * Otherwise, prepend an empty string to :data:`sys.path`.
1366
1367If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be
1368modified by the :mod:`site` module. If
1369:c:member:`~PyConfig.user_site_directory` is non-zero and the user's
1370site-package directory exists, the :mod:`site` module appends the user's
1371site-package directory to :data:`sys.path`.
1372
1373The following configuration files are used by the path configuration:
1374
1375* ``pyvenv.cfg``
1376* ``python._pth`` (Windows only)
1377* ``pybuilddir.txt`` (Unix only)
1378
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001379The ``__PYVENV_LAUNCHER__`` environment variable is used to set
1380:c:member:`PyConfig.base_executable`
1381
Victor Stinner331a6a52019-05-27 16:39:22 +02001382
1383Py_RunMain()
Victor Stinnerdc42af82020-11-05 18:58:07 +01001384============
Victor Stinner331a6a52019-05-27 16:39:22 +02001385
1386.. c:function:: int Py_RunMain(void)
1387
1388 Execute the command (:c:member:`PyConfig.run_command`), the script
1389 (:c:member:`PyConfig.run_filename`) or the module
1390 (:c:member:`PyConfig.run_module`) specified on the command line or in the
1391 configuration.
1392
1393 By default and when if :option:`-i` option is used, run the REPL.
1394
1395 Finally, finalizes Python and returns an exit status that can be passed to
1396 the ``exit()`` function.
1397
1398See :ref:`Python Configuration <init-python-config>` for an example of
1399customized Python always running in isolated mode using
1400:c:func:`Py_RunMain`.
1401
1402
Victor Stinnere81f6e62020-06-08 18:12:59 +02001403Py_GetArgcArgv()
Victor Stinnerdc42af82020-11-05 18:58:07 +01001404================
Victor Stinnere81f6e62020-06-08 18:12:59 +02001405
1406.. c:function:: void Py_GetArgcArgv(int *argc, wchar_t ***argv)
1407
1408 Get the original command line arguments, before Python modified them.
1409
Victor Stinnerdd8a93e2020-06-30 00:49:03 +02001410 See also :c:member:`PyConfig.orig_argv` member.
1411
Victor Stinnere81f6e62020-06-08 18:12:59 +02001412
Victor Stinner331a6a52019-05-27 16:39:22 +02001413Multi-Phase Initialization Private Provisional API
Victor Stinnerdc42af82020-11-05 18:58:07 +01001414==================================================
Victor Stinner331a6a52019-05-27 16:39:22 +02001415
1416This section is a private provisional API introducing multi-phase
1417initialization, the core feature of the :pep:`432`:
1418
1419* "Core" initialization phase, "bare minimum Python":
1420
1421 * Builtin types;
1422 * Builtin exceptions;
1423 * Builtin and frozen modules;
1424 * The :mod:`sys` module is only partially initialized
Victor Stinner88feaec2019-09-26 03:15:07 +02001425 (ex: :data:`sys.path` doesn't exist yet).
Victor Stinner331a6a52019-05-27 16:39:22 +02001426
1427* "Main" initialization phase, Python is fully initialized:
1428
1429 * Install and configure :mod:`importlib`;
1430 * Apply the :ref:`Path Configuration <init-path-config>`;
1431 * Install signal handlers;
1432 * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout`
1433 and :data:`sys.path`);
1434 * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`;
1435 * Import the :mod:`site` module;
1436 * etc.
1437
1438Private provisional API:
1439
1440* :c:member:`PyConfig._init_main`: if set to 0,
1441 :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase.
Victor Stinner252346a2020-05-01 11:33:44 +02001442* :c:member:`PyConfig._isolated_interpreter`: if non-zero,
1443 disallow threads, subprocesses and fork.
Victor Stinner331a6a52019-05-27 16:39:22 +02001444
1445.. c:function:: PyStatus _Py_InitializeMain(void)
1446
1447 Move to the "Main" initialization phase, finish the Python initialization.
1448
1449No module is imported during the "Core" phase and the ``importlib`` module is
1450not configured: the :ref:`Path Configuration <init-path-config>` is only
1451applied during the "Main" phase. It may allow to customize Python in Python to
1452override or tune the :ref:`Path Configuration <init-path-config>`, maybe
Victor Stinner88feaec2019-09-26 03:15:07 +02001453install a custom :data:`sys.meta_path` importer or an import hook, etc.
Victor Stinner331a6a52019-05-27 16:39:22 +02001454
Victor Stinner88feaec2019-09-26 03:15:07 +02001455It may become possible to calculatin the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001456<init-path-config>` in Python, after the Core phase and before the Main phase,
1457which is one of the :pep:`432` motivation.
1458
1459The "Core" phase is not properly defined: what should be and what should
1460not be available at this phase is not specified yet. The API is marked
1461as private and provisional: the API can be modified or even be removed
1462anytime until a proper public API is designed.
1463
1464Example running Python code between "Core" and "Main" initialization
1465phases::
1466
1467 void init_python(void)
1468 {
1469 PyStatus status;
Victor Stinner8462a492019-10-01 12:06:16 +02001470
Victor Stinner331a6a52019-05-27 16:39:22 +02001471 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001472 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001473 config._init_main = 0;
1474
1475 /* ... customize 'config' configuration ... */
1476
1477 status = Py_InitializeFromConfig(&config);
1478 PyConfig_Clear(&config);
1479 if (PyStatus_Exception(status)) {
1480 Py_ExitStatusException(status);
1481 }
1482
1483 /* Use sys.stderr because sys.stdout is only created
1484 by _Py_InitializeMain() */
1485 int res = PyRun_SimpleString(
1486 "import sys; "
1487 "print('Run Python code before _Py_InitializeMain', "
1488 "file=sys.stderr)");
1489 if (res < 0) {
1490 exit(1);
1491 }
1492
1493 /* ... put more configuration code here ... */
1494
1495 status = _Py_InitializeMain();
1496 if (PyStatus_Exception(status)) {
1497 Py_ExitStatusException(status);
1498 }
1499 }