blob: 9b0728d9621529acf8727c1b84d060cd8cfbe1f1 [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
192 Structure used to preinitialize Python:
193
194 * Set the Python memory allocator
195 * Configure the LC_CTYPE locale
196 * Set the UTF-8 mode
197
198 Function to initialize a preconfiguration:
199
Victor Stinner3c30a762019-10-01 10:56:37 +0200200 .. c:function:: void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200201
202 Initialize the preconfiguration with :ref:`Python Configuration
203 <init-python-config>`.
204
Victor Stinner3c30a762019-10-01 10:56:37 +0200205 .. c:function:: void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200206
207 Initialize the preconfiguration with :ref:`Isolated Configuration
208 <init-isolated-conf>`.
209
210 Structure fields:
211
212 .. c:member:: int allocator
213
214 Name of the memory allocator:
215
216 * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators
217 (use defaults)
218 * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators
219 * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with
220 debug hooks
221 * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()``
222 * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of
223 ``malloc()`` with debug hooks
224 * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory
225 allocator <pymalloc>`
226 * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc
227 memory allocator <pymalloc>` with debug hooks
228
229 ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG``
230 are not supported if Python is configured using ``--without-pymalloc``
231
232 See :ref:`Memory Management <memory>`.
233
234 .. c:member:: int configure_locale
235
236 Set the LC_CTYPE locale to the user preferred locale? If equals to 0, set
237 :c:member:`coerce_c_locale` and :c:member:`coerce_c_locale_warn` to 0.
238
239 .. c:member:: int coerce_c_locale
240
241 If equals to 2, coerce the C locale; if equals to 1, read the LC_CTYPE
242 locale to decide if it should be coerced.
243
244 .. c:member:: int coerce_c_locale_warn
Victor Stinner88feaec2019-09-26 03:15:07 +0200245
Victor Stinner331a6a52019-05-27 16:39:22 +0200246 If non-zero, emit a warning if the C locale is coerced.
247
248 .. c:member:: int dev_mode
249
250 See :c:member:`PyConfig.dev_mode`.
251
252 .. c:member:: int isolated
253
254 See :c:member:`PyConfig.isolated`.
255
256 .. c:member:: int legacy_windows_fs_encoding (Windows only)
257
258 If non-zero, disable UTF-8 Mode, set the Python filesystem encoding to
259 ``mbcs``, set the filesystem error handler to ``replace``.
260
261 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
262 Windows specific code.
263
264 .. c:member:: int parse_argv
265
266 If non-zero, :c:func:`Py_PreInitializeFromArgs` and
267 :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the
268 same way the regular Python parses command line arguments: see
269 :ref:`Command Line Arguments <using-on-cmdline>`.
270
271 .. c:member:: int use_environment
272
273 See :c:member:`PyConfig.use_environment`.
274
275 .. c:member:: int utf8_mode
276
277 If non-zero, enable the UTF-8 mode.
278
279Preinitialization with PyPreConfig
280----------------------------------
281
282Functions to preinitialize Python:
283
284.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
285
286 Preinitialize Python from *preconfig* preconfiguration.
287
288.. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv)
289
290 Preinitialize Python from *preconfig* preconfiguration and command line
291 arguments (bytes strings).
292
293.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)
294
295 Preinitialize Python from *preconfig* preconfiguration and command line
296 arguments (wide strings).
297
298The caller is responsible to handle exceptions (error or exit) using
299:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
300
301For :ref:`Python Configuration <init-python-config>`
302(:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with
303command line arguments, the command line arguments must also be passed to
304preinitialize Python, since they have an effect on the pre-configuration
Victor Stinner88feaec2019-09-26 03:15:07 +0200305like encodings. For example, the :option:`-X utf8 <-X>` command line option
Victor Stinner331a6a52019-05-27 16:39:22 +0200306enables the UTF-8 Mode.
307
308``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and
309before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator.
310It can be called before :c:func:`Py_PreInitialize` if
311:c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``.
312
313Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be
314used before Python preinitialization, whereas calling directly ``malloc()`` and
315``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called before
316the preinitialization.
317
318Example using the preinitialization to enable the UTF-8 Mode::
319
Victor Stinner441b10c2019-09-28 04:28:35 +0200320 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +0200321 PyPreConfig preconfig;
Victor Stinner3c30a762019-10-01 10:56:37 +0200322 PyPreConfig_InitPythonConfig(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200323
324 preconfig.utf8_mode = 1;
325
Victor Stinner441b10c2019-09-28 04:28:35 +0200326 status = Py_PreInitialize(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200327 if (PyStatus_Exception(status)) {
328 Py_ExitStatusException(status);
329 }
330
331 /* at this point, Python will speak UTF-8 */
332
333 Py_Initialize();
334 /* ... use Python API here ... */
335 Py_Finalize();
336
337
338PyConfig
339--------
340
341.. c:type:: PyConfig
342
343 Structure containing most parameters to configure Python.
344
345 Structure methods:
346
Victor Stinner8462a492019-10-01 12:06:16 +0200347 .. c:function:: void PyConfig_InitPythonConfig(PyConfig *config)
Victor Stinner331a6a52019-05-27 16:39:22 +0200348
349 Initialize configuration with :ref:`Python Configuration
350 <init-python-config>`.
351
Victor Stinner8462a492019-10-01 12:06:16 +0200352 .. c:function:: void PyConfig_InitIsolatedConfig(PyConfig *config)
Victor Stinner331a6a52019-05-27 16:39:22 +0200353
354 Initialize configuration with :ref:`Isolated Configuration
355 <init-isolated-conf>`.
356
357 .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str)
358
359 Copy the wide character string *str* into ``*config_str``.
360
361 Preinitialize Python if needed.
362
363 .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str)
364
365 Decode *str* using ``Py_DecodeLocale()`` and set the result into ``*config_str``.
366
367 Preinitialize Python if needed.
368
369 .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv)
370
371 Set command line arguments from wide character strings.
372
373 Preinitialize Python if needed.
374
375 .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv)
376
377 Set command line arguments: decode bytes using :c:func:`Py_DecodeLocale`.
378
379 Preinitialize Python if needed.
380
Victor Stinner36242fd2019-07-01 19:13:50 +0200381 .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items)
382
383 Set the list of wide strings *list* to *length* and *items*.
384
385 Preinitialize Python if needed.
386
Victor Stinner331a6a52019-05-27 16:39:22 +0200387 .. c:function:: PyStatus PyConfig_Read(PyConfig *config)
388
389 Read all Python configuration.
390
391 Fields which are already initialized are left unchanged.
392
393 Preinitialize Python if needed.
394
395 .. c:function:: void PyConfig_Clear(PyConfig *config)
396
397 Release configuration memory.
398
399 Most ``PyConfig`` methods preinitialize Python if needed. In that case, the
400 Python preinitialization configuration in based on the :c:type:`PyConfig`.
401 If configuration fields which are in common with :c:type:`PyPreConfig` are
402 tuned, they must be set before calling a :c:type:`PyConfig` method:
403
404 * :c:member:`~PyConfig.dev_mode`
405 * :c:member:`~PyConfig.isolated`
406 * :c:member:`~PyConfig.parse_argv`
407 * :c:member:`~PyConfig.use_environment`
408
409 Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv`
410 is used, this method must be called first, before other methods, since the
411 preinitialization configuration depends on command line arguments (if
412 :c:member:`parse_argv` is non-zero).
413
414 The caller of these methods is responsible to handle exceptions (error or
415 exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``.
416
417 Structure fields:
418
419 .. c:member:: PyWideStringList argv
420
421 Command line arguments, :data:`sys.argv`. See
422 :c:member:`~PyConfig.parse_argv` to parse :c:member:`~PyConfig.argv` the
423 same way the regular Python parses Python command line arguments. If
424 :c:member:`~PyConfig.argv` is empty, an empty string is added to ensure
425 that :data:`sys.argv` always exists and is never empty.
426
427 .. c:member:: wchar_t* base_exec_prefix
428
429 :data:`sys.base_exec_prefix`.
430
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200431 .. c:member:: wchar_t* base_executable
432
433 :data:`sys._base_executable`: ``__PYVENV_LAUNCHER__`` environment
434 variable value, or copy of :c:member:`PyConfig.executable`.
435
Victor Stinner331a6a52019-05-27 16:39:22 +0200436 .. c:member:: wchar_t* base_prefix
437
438 :data:`sys.base_prefix`.
439
Sandro Mani8f023a22020-06-08 17:28:11 +0200440 .. c:member:: wchar_t* platlibdir
441
442 :data:`sys.platlibdir`: platform library directory name, set at configure time
443 by ``--with-platlibdir``, overrideable by the ``PYTHONPLATLIBDIR``
444 environment variable.
445
Victor Stinner5edb8322020-06-08 20:04:47 +0200446 .. versionadded:: 3.9
Sandro Mani8f023a22020-06-08 17:28:11 +0200447
Victor Stinner331a6a52019-05-27 16:39:22 +0200448 .. c:member:: int buffered_stdio
449
450 If equals to 0, enable unbuffered mode, making the stdout and stderr
451 streams unbuffered.
452
453 stdin is always opened in buffered mode.
454
455 .. c:member:: int bytes_warning
456
457 If equals to 1, issue a warning when comparing :class:`bytes` or
458 :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with
459 :class:`int`. If equal or greater to 2, raise a :exc:`BytesWarning`
460 exception.
461
462 .. c:member:: wchar_t* check_hash_pycs_mode
463
464 Control the validation behavior of hash-based ``.pyc`` files (see
465 :pep:`552`): :option:`--check-hash-based-pycs` command line option value.
466
467 Valid values: ``always``, ``never`` and ``default``.
468
469 The default value is: ``default``.
470
471 .. c:member:: int configure_c_stdio
472
473 If non-zero, configure C standard streams (``stdio``, ``stdout``,
474 ``stdout``). For example, set their mode to ``O_BINARY`` on Windows.
475
476 .. c:member:: int dev_mode
477
Victor Stinnerb9783d22020-01-24 10:22:18 +0100478 If non-zero, enable the :ref:`Python Development Mode <devmode>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200479
480 .. c:member:: int dump_refs
481
482 If non-zero, dump all objects which are still alive at exit.
483
Hai Shia7847592020-02-17 17:18:19 +0800484 ``Py_TRACE_REFS`` macro must be defined in build.
Victor Stinner331a6a52019-05-27 16:39:22 +0200485
486 .. c:member:: wchar_t* exec_prefix
487
488 :data:`sys.exec_prefix`.
489
490 .. c:member:: wchar_t* executable
491
492 :data:`sys.executable`.
493
494 .. c:member:: int faulthandler
495
Victor Stinner88feaec2019-09-26 03:15:07 +0200496 If non-zero, call :func:`faulthandler.enable` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200497
498 .. c:member:: wchar_t* filesystem_encoding
499
500 Filesystem encoding, :func:`sys.getfilesystemencoding`.
501
502 .. c:member:: wchar_t* filesystem_errors
503
504 Filesystem encoding errors, :func:`sys.getfilesystemencodeerrors`.
505
506 .. c:member:: unsigned long hash_seed
507 .. c:member:: int use_hash_seed
508
509 Randomized hash function seed.
510
511 If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly
512 at Pythonstartup, and :c:member:`~PyConfig.hash_seed` is ignored.
513
514 .. c:member:: wchar_t* home
515
516 Python home directory.
517
Victor Stinner88feaec2019-09-26 03:15:07 +0200518 Initialized from :envvar:`PYTHONHOME` environment variable value by
519 default.
520
Victor Stinner331a6a52019-05-27 16:39:22 +0200521 .. c:member:: int import_time
522
523 If non-zero, profile import time.
524
525 .. c:member:: int inspect
526
527 Enter interactive mode after executing a script or a command.
528
529 .. c:member:: int install_signal_handlers
530
531 Install signal handlers?
532
533 .. c:member:: int interactive
534
535 Interactive mode.
536
537 .. c:member:: int isolated
538
539 If greater than 0, enable isolated mode:
540
541 * :data:`sys.path` contains neither the script's directory (computed from
542 ``argv[0]`` or the current directory) nor the user's site-packages
543 directory.
544 * Python REPL doesn't import :mod:`readline` nor enable default readline
545 configuration on interactive prompts.
546 * Set :c:member:`~PyConfig.use_environment` and
547 :c:member:`~PyConfig.user_site_directory` to 0.
548
549 .. c:member:: int legacy_windows_stdio
550
551 If non-zero, use :class:`io.FileIO` instead of
552 :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout`
553 and :data:`sys.stderr`.
554
555 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
556 Windows specific code.
557
558 .. c:member:: int malloc_stats
559
560 If non-zero, dump statistics on :ref:`Python pymalloc memory allocator
561 <pymalloc>` at exit.
562
563 The option is ignored if Python is built using ``--without-pymalloc``.
564
565 .. c:member:: wchar_t* pythonpath_env
566
567 Module search paths as a string separated by ``DELIM``
568 (:data:`os.path.pathsep`).
569
570 Initialized from :envvar:`PYTHONPATH` environment variable value by
571 default.
572
573 .. c:member:: PyWideStringList module_search_paths
574 .. c:member:: int module_search_paths_set
575
576 :data:`sys.path`. If :c:member:`~PyConfig.module_search_paths_set` is
577 equal to 0, the :c:member:`~PyConfig.module_search_paths` is overridden
Victor Stinner88feaec2019-09-26 03:15:07 +0200578 by the function calculating the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200579 <init-path-config>`.
580
581 .. c:member:: int optimization_level
582
583 Compilation optimization level:
584
585 * 0: Peephole optimizer (and ``__debug__`` is set to ``True``)
586 * 1: Remove assertions, set ``__debug__`` to ``False``
587 * 2: Strip docstrings
588
589 .. c:member:: int parse_argv
590
591 If non-zero, parse :c:member:`~PyConfig.argv` the same way the regular
592 Python command line arguments, and strip Python arguments from
593 :c:member:`~PyConfig.argv`: see :ref:`Command Line Arguments
594 <using-on-cmdline>`.
595
596 .. c:member:: int parser_debug
597
598 If non-zero, turn on parser debugging output (for expert only, depending
599 on compilation options).
600
601 .. c:member:: int pathconfig_warnings
602
Victor Stinner88feaec2019-09-26 03:15:07 +0200603 If equal to 0, suppress warnings when calculating the :ref:`Path
604 Configuration <init-path-config>` (Unix only, Windows does not log any
605 warning). Otherwise, warnings are written into ``stderr``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200606
607 .. c:member:: wchar_t* prefix
608
609 :data:`sys.prefix`.
610
611 .. c:member:: wchar_t* program_name
612
Victor Stinner88feaec2019-09-26 03:15:07 +0200613 Program name. Used to initialize :c:member:`~PyConfig.executable`, and in
614 early error messages.
Victor Stinner331a6a52019-05-27 16:39:22 +0200615
616 .. c:member:: wchar_t* pycache_prefix
617
Victor Stinner88feaec2019-09-26 03:15:07 +0200618 :data:`sys.pycache_prefix`: ``.pyc`` cache prefix.
619
Serhiy Storchakae835b312019-10-30 21:37:16 +0200620 If ``NULL``, :data:`sys.pycache_prefix` is set to ``None``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200621
622 .. c:member:: int quiet
623
624 Quiet mode. For example, don't display the copyright and version messages
Victor Stinner88feaec2019-09-26 03:15:07 +0200625 in interactive mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200626
627 .. c:member:: wchar_t* run_command
628
Victor Stinner88feaec2019-09-26 03:15:07 +0200629 ``python3 -c COMMAND`` argument. Used by :c:func:`Py_RunMain`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200630
631 .. c:member:: wchar_t* run_filename
632
Victor Stinner88feaec2019-09-26 03:15:07 +0200633 ``python3 FILENAME`` argument. Used by :c:func:`Py_RunMain`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200634
635 .. c:member:: wchar_t* run_module
636
Victor Stinner88feaec2019-09-26 03:15:07 +0200637 ``python3 -m MODULE`` argument. Used by :c:func:`Py_RunMain`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200638
Victor Stinner331a6a52019-05-27 16:39:22 +0200639 .. c:member:: int show_ref_count
640
641 Show total reference count at exit?
642
Victor Stinner88feaec2019-09-26 03:15:07 +0200643 Set to 1 by :option:`-X showrefcount <-X>` command line option.
644
Victor Stinner331a6a52019-05-27 16:39:22 +0200645 Need a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
646
647 .. c:member:: int site_import
648
649 Import the :mod:`site` module at startup?
650
651 .. c:member:: int skip_source_first_line
652
653 Skip the first line of the source?
654
655 .. c:member:: wchar_t* stdio_encoding
656 .. c:member:: wchar_t* stdio_errors
657
658 Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
659 :data:`sys.stderr`.
660
661 .. c:member:: int tracemalloc
662
Victor Stinner88feaec2019-09-26 03:15:07 +0200663 If non-zero, call :func:`tracemalloc.start` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200664
665 .. c:member:: int use_environment
666
667 If greater than 0, use :ref:`environment variables <using-on-envvars>`.
668
669 .. c:member:: int user_site_directory
670
671 If non-zero, add user site directory to :data:`sys.path`.
672
673 .. c:member:: int verbose
674
675 If non-zero, enable verbose mode.
676
677 .. c:member:: PyWideStringList warnoptions
678
Victor Stinnerfb4ae152019-09-30 01:40:17 +0200679 :data:`sys.warnoptions`: options of the :mod:`warnings` module to build
680 warnings filters: lowest to highest priority.
681
682 The :mod:`warnings` module adds :data:`sys.warnoptions` in the reverse
683 order: the last :c:member:`PyConfig.warnoptions` item becomes the first
684 item of :data:`warnings.filters` which is checked first (highest
685 priority).
Victor Stinner331a6a52019-05-27 16:39:22 +0200686
687 .. c:member:: int write_bytecode
688
689 If non-zero, write ``.pyc`` files.
690
Victor Stinner88feaec2019-09-26 03:15:07 +0200691 :data:`sys.dont_write_bytecode` is initialized to the inverted value of
692 :c:member:`~PyConfig.write_bytecode`.
693
Victor Stinner331a6a52019-05-27 16:39:22 +0200694 .. c:member:: PyWideStringList xoptions
695
696 :data:`sys._xoptions`.
697
698If ``parse_argv`` is non-zero, ``argv`` arguments are parsed the same
699way the regular Python parses command line arguments, and Python
700arguments are stripped from ``argv``: see :ref:`Command Line Arguments
701<using-on-cmdline>`.
702
703The ``xoptions`` options are parsed to set other options: see :option:`-X`
704option.
705
Victor Stinnerc6e5c112020-02-03 15:17:15 +0100706.. versionchanged:: 3.9
707
708 The ``show_alloc_count`` field has been removed.
709
Victor Stinner331a6a52019-05-27 16:39:22 +0200710
711Initialization with PyConfig
712----------------------------
713
714Function to initialize Python:
715
716.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
717
718 Initialize Python from *config* configuration.
719
720The caller is responsible to handle exceptions (error or exit) using
721:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
722
Victor Stinner88feaec2019-09-26 03:15:07 +0200723If ``PyImport_FrozenModules``, ``PyImport_AppendInittab()`` or
724``PyImport_ExtendInittab()`` are used, they must be set or called after Python
Victor Stinner331a6a52019-05-27 16:39:22 +0200725preinitialization and before the Python initialization.
726
727Example setting the program name::
728
729 void init_python(void)
730 {
731 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +0200732
Victor Stinner8462a492019-10-01 12:06:16 +0200733 PyConfig config;
734 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200735
736 /* Set the program name. Implicitly preinitialize Python. */
737 status = PyConfig_SetString(&config, &config.program_name,
738 L"/path/to/my_program");
739 if (PyStatus_Exception(status)) {
740 goto fail;
741 }
742
743 status = Py_InitializeFromConfig(&config);
744 if (PyStatus_Exception(status)) {
745 goto fail;
746 }
747 PyConfig_Clear(&config);
748 return;
749
750 fail:
751 PyConfig_Clear(&config);
752 Py_ExitStatusException(status);
753 }
754
755More complete example modifying the default configuration, read the
756configuration, and then override some parameters::
757
758 PyStatus init_python(const char *program_name)
759 {
760 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +0200761
Victor Stinner8462a492019-10-01 12:06:16 +0200762 PyConfig config;
763 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200764
Gurupad Hegde6c7bb382019-12-28 17:16:02 -0500765 /* Set the program name before reading the configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200766 (decode byte string from the locale encoding).
767
768 Implicitly preinitialize Python. */
769 status = PyConfig_SetBytesString(&config, &config.program_name,
770 program_name);
771 if (PyStatus_Exception(status)) {
772 goto done;
773 }
774
775 /* Read all configuration at once */
776 status = PyConfig_Read(&config);
777 if (PyStatus_Exception(status)) {
778 goto done;
779 }
780
781 /* Append our custom search path to sys.path */
782 status = PyWideStringList_Append(&config.module_search_paths,
Victor Stinner88feaec2019-09-26 03:15:07 +0200783 L"/path/to/more/modules");
Victor Stinner331a6a52019-05-27 16:39:22 +0200784 if (PyStatus_Exception(status)) {
785 goto done;
786 }
787
788 /* Override executable computed by PyConfig_Read() */
789 status = PyConfig_SetString(&config, &config.executable,
790 L"/path/to/my_executable");
791 if (PyStatus_Exception(status)) {
792 goto done;
793 }
794
795 status = Py_InitializeFromConfig(&config);
796
797 done:
798 PyConfig_Clear(&config);
799 return status;
800 }
801
802
803.. _init-isolated-conf:
804
805Isolated Configuration
806----------------------
807
808:c:func:`PyPreConfig_InitIsolatedConfig` and
809:c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to
810isolate Python from the system. For example, to embed Python into an
811application.
812
813This configuration ignores global configuration variables, environments
Victor Stinner88feaec2019-09-26 03:15:07 +0200814variables, command line arguments (:c:member:`PyConfig.argv` is not parsed)
815and user site directory. The C standard streams (ex: ``stdout``) and the
816LC_CTYPE locale are left unchanged. Signal handlers are not installed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200817
818Configuration files are still used with this configuration. Set the
819:ref:`Path Configuration <init-path-config>` ("output fields") to ignore these
820configuration files and avoid the function computing the default path
821configuration.
822
823
824.. _init-python-config:
825
826Python Configuration
827--------------------
828
829:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
830functions create a configuration to build a customized Python which behaves as
831the regular Python.
832
833Environments variables and command line arguments are used to configure
834Python, whereas global configuration variables are ignored.
835
836This function enables C locale coercion (:pep:`538`) and UTF-8 Mode
837(:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and
838:envvar:`PYTHONCOERCECLOCALE` environment variables.
839
840Example of customized Python always running in isolated mode::
841
842 int main(int argc, char **argv)
843 {
Victor Stinner331a6a52019-05-27 16:39:22 +0200844 PyStatus status;
Victor Stinner8462a492019-10-01 12:06:16 +0200845
Victor Stinner441b10c2019-09-28 04:28:35 +0200846 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +0200847 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +0200848 config.isolated = 1;
849
850 /* Decode command line arguments.
851 Implicitly preinitialize Python (in isolated mode). */
852 status = PyConfig_SetBytesArgv(&config, argc, argv);
853 if (PyStatus_Exception(status)) {
854 goto fail;
855 }
856
857 status = Py_InitializeFromConfig(&config);
858 if (PyStatus_Exception(status)) {
859 goto fail;
860 }
861 PyConfig_Clear(&config);
862
863 return Py_RunMain();
864
865 fail:
866 PyConfig_Clear(&config);
867 if (PyStatus_IsExit(status)) {
868 return status.exitcode;
869 }
870 /* Display the error message and exit the process with
871 non-zero exit code */
872 Py_ExitStatusException(status);
873 }
874
875
876.. _init-path-config:
877
878Path Configuration
879------------------
880
881:c:type:`PyConfig` contains multiple fields for the path configuration:
882
Victor Stinner8bf39b62019-09-26 02:22:35 +0200883* Path configuration inputs:
Victor Stinner331a6a52019-05-27 16:39:22 +0200884
885 * :c:member:`PyConfig.home`
Sandro Mani8f023a22020-06-08 17:28:11 +0200886 * :c:member:`PyConfig.platlibdir`
Victor Stinner331a6a52019-05-27 16:39:22 +0200887 * :c:member:`PyConfig.pathconfig_warnings`
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200888 * :c:member:`PyConfig.program_name`
889 * :c:member:`PyConfig.pythonpath_env`
Victor Stinner8bf39b62019-09-26 02:22:35 +0200890 * current working directory: to get absolute paths
891 * ``PATH`` environment variable to get the program full path
892 (from :c:member:`PyConfig.program_name`)
893 * ``__PYVENV_LAUNCHER__`` environment variable
894 * (Windows only) Application paths in the registry under
895 "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and
896 HKEY_LOCAL_MACHINE (where X.Y is the Python version).
Victor Stinner331a6a52019-05-27 16:39:22 +0200897
898* Path configuration output fields:
899
Victor Stinner8bf39b62019-09-26 02:22:35 +0200900 * :c:member:`PyConfig.base_exec_prefix`
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200901 * :c:member:`PyConfig.base_executable`
Victor Stinner8bf39b62019-09-26 02:22:35 +0200902 * :c:member:`PyConfig.base_prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +0200903 * :c:member:`PyConfig.exec_prefix`
904 * :c:member:`PyConfig.executable`
Victor Stinner331a6a52019-05-27 16:39:22 +0200905 * :c:member:`PyConfig.module_search_paths_set`,
906 :c:member:`PyConfig.module_search_paths`
Victor Stinner8bf39b62019-09-26 02:22:35 +0200907 * :c:member:`PyConfig.prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +0200908
Victor Stinner8bf39b62019-09-26 02:22:35 +0200909If at least one "output field" is not set, Python calculates the path
Victor Stinner331a6a52019-05-27 16:39:22 +0200910configuration to fill unset fields. If
911:c:member:`~PyConfig.module_search_paths_set` is equal to 0,
Min ho Kim39d87b52019-08-31 06:21:19 +1000912:c:member:`~PyConfig.module_search_paths` is overridden and
Victor Stinner331a6a52019-05-27 16:39:22 +0200913:c:member:`~PyConfig.module_search_paths_set` is set to 1.
914
Victor Stinner8bf39b62019-09-26 02:22:35 +0200915It is possible to completely ignore the function calculating the default
Victor Stinner331a6a52019-05-27 16:39:22 +0200916path configuration by setting explicitly all path configuration output
917fields listed above. A string is considered as set even if it is non-empty.
918``module_search_paths`` is considered as set if
919``module_search_paths_set`` is set to 1. In this case, path
920configuration input fields are ignored as well.
921
922Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when
Victor Stinner8bf39b62019-09-26 02:22:35 +0200923calculating the path configuration (Unix only, Windows does not log any warning).
Victor Stinner331a6a52019-05-27 16:39:22 +0200924
925If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix`
926fields are not set, they inherit their value from :c:member:`~PyConfig.prefix`
927and :c:member:`~PyConfig.exec_prefix` respectively.
928
929:c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`:
930
931* If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a
932 ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to
933 :data:`sys.path`.
934* If :c:member:`~PyConfig.isolated` is zero:
935
936 * If :c:member:`~PyConfig.run_module` is set, prepend the current directory
937 to :data:`sys.path`. Do nothing if the current directory cannot be read.
938 * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the
939 filename to :data:`sys.path`.
940 * Otherwise, prepend an empty string to :data:`sys.path`.
941
942If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be
943modified by the :mod:`site` module. If
944:c:member:`~PyConfig.user_site_directory` is non-zero and the user's
945site-package directory exists, the :mod:`site` module appends the user's
946site-package directory to :data:`sys.path`.
947
948The following configuration files are used by the path configuration:
949
950* ``pyvenv.cfg``
951* ``python._pth`` (Windows only)
952* ``pybuilddir.txt`` (Unix only)
953
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200954The ``__PYVENV_LAUNCHER__`` environment variable is used to set
955:c:member:`PyConfig.base_executable`
956
Victor Stinner331a6a52019-05-27 16:39:22 +0200957
958Py_RunMain()
959------------
960
961.. c:function:: int Py_RunMain(void)
962
963 Execute the command (:c:member:`PyConfig.run_command`), the script
964 (:c:member:`PyConfig.run_filename`) or the module
965 (:c:member:`PyConfig.run_module`) specified on the command line or in the
966 configuration.
967
968 By default and when if :option:`-i` option is used, run the REPL.
969
970 Finally, finalizes Python and returns an exit status that can be passed to
971 the ``exit()`` function.
972
973See :ref:`Python Configuration <init-python-config>` for an example of
974customized Python always running in isolated mode using
975:c:func:`Py_RunMain`.
976
977
Victor Stinnere81f6e62020-06-08 18:12:59 +0200978Py_GetArgcArgv()
979----------------
980
981.. c:function:: void Py_GetArgcArgv(int *argc, wchar_t ***argv)
982
983 Get the original command line arguments, before Python modified them.
984
985
Victor Stinner331a6a52019-05-27 16:39:22 +0200986Multi-Phase Initialization Private Provisional API
987--------------------------------------------------
988
989This section is a private provisional API introducing multi-phase
990initialization, the core feature of the :pep:`432`:
991
992* "Core" initialization phase, "bare minimum Python":
993
994 * Builtin types;
995 * Builtin exceptions;
996 * Builtin and frozen modules;
997 * The :mod:`sys` module is only partially initialized
Victor Stinner88feaec2019-09-26 03:15:07 +0200998 (ex: :data:`sys.path` doesn't exist yet).
Victor Stinner331a6a52019-05-27 16:39:22 +0200999
1000* "Main" initialization phase, Python is fully initialized:
1001
1002 * Install and configure :mod:`importlib`;
1003 * Apply the :ref:`Path Configuration <init-path-config>`;
1004 * Install signal handlers;
1005 * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout`
1006 and :data:`sys.path`);
1007 * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`;
1008 * Import the :mod:`site` module;
1009 * etc.
1010
1011Private provisional API:
1012
1013* :c:member:`PyConfig._init_main`: if set to 0,
1014 :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase.
Victor Stinner252346a2020-05-01 11:33:44 +02001015* :c:member:`PyConfig._isolated_interpreter`: if non-zero,
1016 disallow threads, subprocesses and fork.
Victor Stinner331a6a52019-05-27 16:39:22 +02001017
1018.. c:function:: PyStatus _Py_InitializeMain(void)
1019
1020 Move to the "Main" initialization phase, finish the Python initialization.
1021
1022No module is imported during the "Core" phase and the ``importlib`` module is
1023not configured: the :ref:`Path Configuration <init-path-config>` is only
1024applied during the "Main" phase. It may allow to customize Python in Python to
1025override or tune the :ref:`Path Configuration <init-path-config>`, maybe
Victor Stinner88feaec2019-09-26 03:15:07 +02001026install a custom :data:`sys.meta_path` importer or an import hook, etc.
Victor Stinner331a6a52019-05-27 16:39:22 +02001027
Victor Stinner88feaec2019-09-26 03:15:07 +02001028It may become possible to calculatin the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001029<init-path-config>` in Python, after the Core phase and before the Main phase,
1030which is one of the :pep:`432` motivation.
1031
1032The "Core" phase is not properly defined: what should be and what should
1033not be available at this phase is not specified yet. The API is marked
1034as private and provisional: the API can be modified or even be removed
1035anytime until a proper public API is designed.
1036
1037Example running Python code between "Core" and "Main" initialization
1038phases::
1039
1040 void init_python(void)
1041 {
1042 PyStatus status;
Victor Stinner8462a492019-10-01 12:06:16 +02001043
Victor Stinner331a6a52019-05-27 16:39:22 +02001044 PyConfig config;
Victor Stinner8462a492019-10-01 12:06:16 +02001045 PyConfig_InitPythonConfig(&config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001046 config._init_main = 0;
1047
1048 /* ... customize 'config' configuration ... */
1049
1050 status = Py_InitializeFromConfig(&config);
1051 PyConfig_Clear(&config);
1052 if (PyStatus_Exception(status)) {
1053 Py_ExitStatusException(status);
1054 }
1055
1056 /* Use sys.stderr because sys.stdout is only created
1057 by _Py_InitializeMain() */
1058 int res = PyRun_SimpleString(
1059 "import sys; "
1060 "print('Run Python code before _Py_InitializeMain', "
1061 "file=sys.stderr)");
1062 if (res < 0) {
1063 exit(1);
1064 }
1065
1066 /* ... put more configuration code here ... */
1067
1068 status = _Py_InitializeMain();
1069 if (PyStatus_Exception(status)) {
1070 Py_ExitStatusException(status);
1071 }
1072 }