blob: 0c3c725c841ac12c70b757008b4f9752983bcf8d [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`
46
47The preconfiguration (``PyPreConfig`` type) is stored in
48``_PyRuntime.preconfig`` and the configuration (``PyConfig`` type) is stored in
49``PyInterpreterState.config``.
50
Victor Stinner1beb7c32019-08-23 17:59:12 +010051See also :ref:`Initialization, Finalization, and Threads <initialization>`.
52
Victor Stinner331a6a52019-05-27 16:39:22 +020053.. seealso::
54 :pep:`587` "Python Initialization Configuration".
55
56
57PyWideStringList
58----------------
59
60.. c:type:: PyWideStringList
61
62 List of ``wchar_t*`` strings.
63
64 If *length* is non-zero, *items* must be non-NULL and all strings must be
65 non-NULL.
66
67 Methods:
68
69 .. c:function:: PyStatus PyWideStringList_Append(PyWideStringList *list, const wchar_t *item)
70
71 Append *item* to *list*.
72
73 Python must be preinitialized to call this function.
74
75 .. c:function:: PyStatus PyWideStringList_Insert(PyWideStringList *list, Py_ssize_t index, const wchar_t *item)
76
Victor Stinner3842f292019-08-23 16:57:54 +010077 Insert *item* into *list* at *index*.
78
79 If *index* is greater than or equal to *list* length, append *item* to
80 *list*.
81
82 *index* must be greater than or equal to 0.
Victor Stinner331a6a52019-05-27 16:39:22 +020083
84 Python must be preinitialized to call this function.
85
86 Structure fields:
87
88 .. c:member:: Py_ssize_t length
89
90 List length.
91
92 .. c:member:: wchar_t** items
93
94 List items.
95
96PyStatus
97--------
98
99.. c:type:: PyStatus
100
101 Structure to store an initialization function status: success, error
102 or exit.
103
104 For an error, it can store the C function name which created the error.
105
106 Structure fields:
107
108 .. c:member:: int exitcode
109
110 Exit code. Argument passed to ``exit()``.
111
112 .. c:member:: const char *err_msg
113
114 Error message.
115
116 .. c:member:: const char *func
117
118 Name of the function which created an error, can be ``NULL``.
119
120 Functions to create a status:
121
122 .. c:function:: PyStatus PyStatus_Ok(void)
123
124 Success.
125
126 .. c:function:: PyStatus PyStatus_Error(const char *err_msg)
127
128 Initialization error with a message.
129
130 .. c:function:: PyStatus PyStatus_NoMemory(void)
131
132 Memory allocation failure (out of memory).
133
134 .. c:function:: PyStatus PyStatus_Exit(int exitcode)
135
136 Exit Python with the specified exit code.
137
138 Functions to handle a status:
139
140 .. c:function:: int PyStatus_Exception(PyStatus status)
141
142 Is the status an error or an exit? If true, the exception must be
143 handled; by calling :c:func:`Py_ExitStatusException` for example.
144
145 .. c:function:: int PyStatus_IsError(PyStatus status)
146
147 Is the result an error?
148
149 .. c:function:: int PyStatus_IsExit(PyStatus status)
150
151 Is the result an exit?
152
153 .. c:function:: void Py_ExitStatusException(PyStatus status)
154
155 Call ``exit(exitcode)`` if *status* is an exit. Print the error
156 message and exit with a non-zero exit code if *status* is an error. Must
157 only be called if ``PyStatus_Exception(status)`` is non-zero.
158
159.. note::
160 Internally, Python uses macros which set ``PyStatus.func``,
161 whereas functions to create a status set ``func`` to ``NULL``.
162
163Example::
164
165 PyStatus alloc(void **ptr, size_t size)
166 {
167 *ptr = PyMem_RawMalloc(size);
168 if (*ptr == NULL) {
169 return PyStatus_NoMemory();
170 }
171 return PyStatus_Ok();
172 }
173
174 int main(int argc, char **argv)
175 {
176 void *ptr;
177 PyStatus status = alloc(&ptr, 16);
178 if (PyStatus_Exception(status)) {
179 Py_ExitStatusException(status);
180 }
181 PyMem_Free(ptr);
182 return 0;
183 }
184
185
186PyPreConfig
187-----------
188
189.. c:type:: PyPreConfig
190
191 Structure used to preinitialize Python:
192
193 * Set the Python memory allocator
194 * Configure the LC_CTYPE locale
195 * Set the UTF-8 mode
196
197 Function to initialize a preconfiguration:
198
199 .. c:function:: void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
200
201 Initialize the preconfiguration with :ref:`Python Configuration
202 <init-python-config>`.
203
204 .. c:function:: void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
205
206 Initialize the preconfiguration with :ref:`Isolated Configuration
207 <init-isolated-conf>`.
208
209 Structure fields:
210
211 .. c:member:: int allocator
212
213 Name of the memory allocator:
214
215 * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators
216 (use defaults)
217 * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators
218 * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with
219 debug hooks
220 * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()``
221 * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of
222 ``malloc()`` with debug hooks
223 * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory
224 allocator <pymalloc>`
225 * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc
226 memory allocator <pymalloc>` with debug hooks
227
228 ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG``
229 are not supported if Python is configured using ``--without-pymalloc``
230
231 See :ref:`Memory Management <memory>`.
232
233 .. c:member:: int configure_locale
234
235 Set the LC_CTYPE locale to the user preferred locale? If equals to 0, set
236 :c:member:`coerce_c_locale` and :c:member:`coerce_c_locale_warn` to 0.
237
238 .. c:member:: int coerce_c_locale
239
240 If equals to 2, coerce the C locale; if equals to 1, read the LC_CTYPE
241 locale to decide if it should be coerced.
242
243 .. c:member:: int coerce_c_locale_warn
Victor Stinner88feaec2019-09-26 03:15:07 +0200244
Victor Stinner331a6a52019-05-27 16:39:22 +0200245 If non-zero, emit a warning if the C locale is coerced.
246
247 .. c:member:: int dev_mode
248
249 See :c:member:`PyConfig.dev_mode`.
250
251 .. c:member:: int isolated
252
253 See :c:member:`PyConfig.isolated`.
254
255 .. c:member:: int legacy_windows_fs_encoding (Windows only)
256
257 If non-zero, disable UTF-8 Mode, set the Python filesystem encoding to
258 ``mbcs``, set the filesystem error handler to ``replace``.
259
260 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
261 Windows specific code.
262
263 .. c:member:: int parse_argv
264
265 If non-zero, :c:func:`Py_PreInitializeFromArgs` and
266 :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the
267 same way the regular Python parses command line arguments: see
268 :ref:`Command Line Arguments <using-on-cmdline>`.
269
270 .. c:member:: int use_environment
271
272 See :c:member:`PyConfig.use_environment`.
273
274 .. c:member:: int utf8_mode
275
276 If non-zero, enable the UTF-8 mode.
277
278Preinitialization with PyPreConfig
279----------------------------------
280
281Functions to preinitialize Python:
282
283.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
284
285 Preinitialize Python from *preconfig* preconfiguration.
286
287.. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv)
288
289 Preinitialize Python from *preconfig* preconfiguration and command line
290 arguments (bytes strings).
291
292.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)
293
294 Preinitialize Python from *preconfig* preconfiguration and command line
295 arguments (wide strings).
296
297The caller is responsible to handle exceptions (error or exit) using
298:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
299
300For :ref:`Python Configuration <init-python-config>`
301(:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with
302command line arguments, the command line arguments must also be passed to
303preinitialize Python, since they have an effect on the pre-configuration
Victor Stinner88feaec2019-09-26 03:15:07 +0200304like encodings. For example, the :option:`-X utf8 <-X>` command line option
Victor Stinner331a6a52019-05-27 16:39:22 +0200305enables the UTF-8 Mode.
306
307``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and
308before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator.
309It can be called before :c:func:`Py_PreInitialize` if
310:c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``.
311
312Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be
313used before Python preinitialization, whereas calling directly ``malloc()`` and
314``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called before
315the preinitialization.
316
317Example using the preinitialization to enable the UTF-8 Mode::
318
319 PyPreConfig preconfig;
320 PyPreConfig_InitPythonConfig(&preconfig);
321
322 preconfig.utf8_mode = 1;
323
324 PyStatus status = Py_PreInitialize(&preconfig);
325 if (PyStatus_Exception(status)) {
326 Py_ExitStatusException(status);
327 }
328
329 /* at this point, Python will speak UTF-8 */
330
331 Py_Initialize();
332 /* ... use Python API here ... */
333 Py_Finalize();
334
335
336PyConfig
337--------
338
339.. c:type:: PyConfig
340
341 Structure containing most parameters to configure Python.
342
343 Structure methods:
344
345 .. c:function:: PyStatus PyConfig_InitPythonConfig(PyConfig *config)
346
347 Initialize configuration with :ref:`Python Configuration
348 <init-python-config>`.
349
350 .. c:function:: PyStatus PyConfig_InitIsolatedConfig(PyConfig *config)
351
352 Initialize configuration with :ref:`Isolated Configuration
353 <init-isolated-conf>`.
354
355 .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str)
356
357 Copy the wide character string *str* into ``*config_str``.
358
359 Preinitialize Python if needed.
360
361 .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str)
362
363 Decode *str* using ``Py_DecodeLocale()`` and set the result into ``*config_str``.
364
365 Preinitialize Python if needed.
366
367 .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv)
368
369 Set command line arguments from wide character strings.
370
371 Preinitialize Python if needed.
372
373 .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv)
374
375 Set command line arguments: decode bytes using :c:func:`Py_DecodeLocale`.
376
377 Preinitialize Python if needed.
378
Victor Stinner36242fd2019-07-01 19:13:50 +0200379 .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items)
380
381 Set the list of wide strings *list* to *length* and *items*.
382
383 Preinitialize Python if needed.
384
Victor Stinner331a6a52019-05-27 16:39:22 +0200385 .. c:function:: PyStatus PyConfig_Read(PyConfig *config)
386
387 Read all Python configuration.
388
389 Fields which are already initialized are left unchanged.
390
391 Preinitialize Python if needed.
392
393 .. c:function:: void PyConfig_Clear(PyConfig *config)
394
395 Release configuration memory.
396
397 Most ``PyConfig`` methods preinitialize Python if needed. In that case, the
398 Python preinitialization configuration in based on the :c:type:`PyConfig`.
399 If configuration fields which are in common with :c:type:`PyPreConfig` are
400 tuned, they must be set before calling a :c:type:`PyConfig` method:
401
402 * :c:member:`~PyConfig.dev_mode`
403 * :c:member:`~PyConfig.isolated`
404 * :c:member:`~PyConfig.parse_argv`
405 * :c:member:`~PyConfig.use_environment`
406
407 Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv`
408 is used, this method must be called first, before other methods, since the
409 preinitialization configuration depends on command line arguments (if
410 :c:member:`parse_argv` is non-zero).
411
412 The caller of these methods is responsible to handle exceptions (error or
413 exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``.
414
415 Structure fields:
416
417 .. c:member:: PyWideStringList argv
418
419 Command line arguments, :data:`sys.argv`. See
420 :c:member:`~PyConfig.parse_argv` to parse :c:member:`~PyConfig.argv` the
421 same way the regular Python parses Python command line arguments. If
422 :c:member:`~PyConfig.argv` is empty, an empty string is added to ensure
423 that :data:`sys.argv` always exists and is never empty.
424
425 .. c:member:: wchar_t* base_exec_prefix
426
427 :data:`sys.base_exec_prefix`.
428
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200429 .. c:member:: wchar_t* base_executable
430
431 :data:`sys._base_executable`: ``__PYVENV_LAUNCHER__`` environment
432 variable value, or copy of :c:member:`PyConfig.executable`.
433
Victor Stinner331a6a52019-05-27 16:39:22 +0200434 .. c:member:: wchar_t* base_prefix
435
436 :data:`sys.base_prefix`.
437
438 .. c:member:: int buffered_stdio
439
440 If equals to 0, enable unbuffered mode, making the stdout and stderr
441 streams unbuffered.
442
443 stdin is always opened in buffered mode.
444
445 .. c:member:: int bytes_warning
446
447 If equals to 1, issue a warning when comparing :class:`bytes` or
448 :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with
449 :class:`int`. If equal or greater to 2, raise a :exc:`BytesWarning`
450 exception.
451
452 .. c:member:: wchar_t* check_hash_pycs_mode
453
454 Control the validation behavior of hash-based ``.pyc`` files (see
455 :pep:`552`): :option:`--check-hash-based-pycs` command line option value.
456
457 Valid values: ``always``, ``never`` and ``default``.
458
459 The default value is: ``default``.
460
461 .. c:member:: int configure_c_stdio
462
463 If non-zero, configure C standard streams (``stdio``, ``stdout``,
464 ``stdout``). For example, set their mode to ``O_BINARY`` on Windows.
465
466 .. c:member:: int dev_mode
467
Victor Stinner88feaec2019-09-26 03:15:07 +0200468 Development mode: see :option:`-X dev <-X>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200469
470 .. c:member:: int dump_refs
471
472 If non-zero, dump all objects which are still alive at exit.
473
474 Require a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
475
476 .. c:member:: wchar_t* exec_prefix
477
478 :data:`sys.exec_prefix`.
479
480 .. c:member:: wchar_t* executable
481
482 :data:`sys.executable`.
483
484 .. c:member:: int faulthandler
485
Victor Stinner88feaec2019-09-26 03:15:07 +0200486 If non-zero, call :func:`faulthandler.enable` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200487
488 .. c:member:: wchar_t* filesystem_encoding
489
490 Filesystem encoding, :func:`sys.getfilesystemencoding`.
491
492 .. c:member:: wchar_t* filesystem_errors
493
494 Filesystem encoding errors, :func:`sys.getfilesystemencodeerrors`.
495
496 .. c:member:: unsigned long hash_seed
497 .. c:member:: int use_hash_seed
498
499 Randomized hash function seed.
500
501 If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly
502 at Pythonstartup, and :c:member:`~PyConfig.hash_seed` is ignored.
503
504 .. c:member:: wchar_t* home
505
506 Python home directory.
507
Victor Stinner88feaec2019-09-26 03:15:07 +0200508 Initialized from :envvar:`PYTHONHOME` environment variable value by
509 default.
510
Victor Stinner331a6a52019-05-27 16:39:22 +0200511 .. c:member:: int import_time
512
513 If non-zero, profile import time.
514
515 .. c:member:: int inspect
516
517 Enter interactive mode after executing a script or a command.
518
519 .. c:member:: int install_signal_handlers
520
521 Install signal handlers?
522
523 .. c:member:: int interactive
524
525 Interactive mode.
526
527 .. c:member:: int isolated
528
529 If greater than 0, enable isolated mode:
530
531 * :data:`sys.path` contains neither the script's directory (computed from
532 ``argv[0]`` or the current directory) nor the user's site-packages
533 directory.
534 * Python REPL doesn't import :mod:`readline` nor enable default readline
535 configuration on interactive prompts.
536 * Set :c:member:`~PyConfig.use_environment` and
537 :c:member:`~PyConfig.user_site_directory` to 0.
538
539 .. c:member:: int legacy_windows_stdio
540
541 If non-zero, use :class:`io.FileIO` instead of
542 :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout`
543 and :data:`sys.stderr`.
544
545 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
546 Windows specific code.
547
548 .. c:member:: int malloc_stats
549
550 If non-zero, dump statistics on :ref:`Python pymalloc memory allocator
551 <pymalloc>` at exit.
552
553 The option is ignored if Python is built using ``--without-pymalloc``.
554
555 .. c:member:: wchar_t* pythonpath_env
556
557 Module search paths as a string separated by ``DELIM``
558 (:data:`os.path.pathsep`).
559
560 Initialized from :envvar:`PYTHONPATH` environment variable value by
561 default.
562
563 .. c:member:: PyWideStringList module_search_paths
564 .. c:member:: int module_search_paths_set
565
566 :data:`sys.path`. If :c:member:`~PyConfig.module_search_paths_set` is
567 equal to 0, the :c:member:`~PyConfig.module_search_paths` is overridden
Victor Stinner88feaec2019-09-26 03:15:07 +0200568 by the function calculating the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200569 <init-path-config>`.
570
571 .. c:member:: int optimization_level
572
573 Compilation optimization level:
574
575 * 0: Peephole optimizer (and ``__debug__`` is set to ``True``)
576 * 1: Remove assertions, set ``__debug__`` to ``False``
577 * 2: Strip docstrings
578
579 .. c:member:: int parse_argv
580
581 If non-zero, parse :c:member:`~PyConfig.argv` the same way the regular
582 Python command line arguments, and strip Python arguments from
583 :c:member:`~PyConfig.argv`: see :ref:`Command Line Arguments
584 <using-on-cmdline>`.
585
586 .. c:member:: int parser_debug
587
588 If non-zero, turn on parser debugging output (for expert only, depending
589 on compilation options).
590
591 .. c:member:: int pathconfig_warnings
592
Victor Stinner88feaec2019-09-26 03:15:07 +0200593 If equal to 0, suppress warnings when calculating the :ref:`Path
594 Configuration <init-path-config>` (Unix only, Windows does not log any
595 warning). Otherwise, warnings are written into ``stderr``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200596
597 .. c:member:: wchar_t* prefix
598
599 :data:`sys.prefix`.
600
601 .. c:member:: wchar_t* program_name
602
Victor Stinner88feaec2019-09-26 03:15:07 +0200603 Program name. Used to initialize :c:member:`~PyConfig.executable`, and in
604 early error messages.
Victor Stinner331a6a52019-05-27 16:39:22 +0200605
606 .. c:member:: wchar_t* pycache_prefix
607
Victor Stinner88feaec2019-09-26 03:15:07 +0200608 :data:`sys.pycache_prefix`: ``.pyc`` cache prefix.
609
610 If NULL, :data:`sys.pycache_prefix` is set to ``None``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200611
612 .. c:member:: int quiet
613
614 Quiet mode. For example, don't display the copyright and version messages
Victor Stinner88feaec2019-09-26 03:15:07 +0200615 in interactive mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200616
617 .. c:member:: wchar_t* run_command
618
Victor Stinner88feaec2019-09-26 03:15:07 +0200619 ``python3 -c COMMAND`` argument. Used by :c:func:`Py_RunMain`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200620
621 .. c:member:: wchar_t* run_filename
622
Victor Stinner88feaec2019-09-26 03:15:07 +0200623 ``python3 FILENAME`` argument. Used by :c:func:`Py_RunMain`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200624
625 .. c:member:: wchar_t* run_module
626
Victor Stinner88feaec2019-09-26 03:15:07 +0200627 ``python3 -m MODULE`` argument. Used by :c:func:`Py_RunMain`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200628
629 .. c:member:: int show_alloc_count
630
631 Show allocation counts at exit?
632
Victor Stinner88feaec2019-09-26 03:15:07 +0200633 Set to 1 by :option:`-X showalloccount <-X>` command line option.
634
Victor Stinner331a6a52019-05-27 16:39:22 +0200635 Need a special Python build with ``COUNT_ALLOCS`` macro defined.
636
637 .. c:member:: int show_ref_count
638
639 Show total reference count at exit?
640
Victor Stinner88feaec2019-09-26 03:15:07 +0200641 Set to 1 by :option:`-X showrefcount <-X>` command line option.
642
Victor Stinner331a6a52019-05-27 16:39:22 +0200643 Need a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
644
645 .. c:member:: int site_import
646
647 Import the :mod:`site` module at startup?
648
649 .. c:member:: int skip_source_first_line
650
651 Skip the first line of the source?
652
653 .. c:member:: wchar_t* stdio_encoding
654 .. c:member:: wchar_t* stdio_errors
655
656 Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
657 :data:`sys.stderr`.
658
659 .. c:member:: int tracemalloc
660
Victor Stinner88feaec2019-09-26 03:15:07 +0200661 If non-zero, call :func:`tracemalloc.start` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200662
663 .. c:member:: int use_environment
664
665 If greater than 0, use :ref:`environment variables <using-on-envvars>`.
666
667 .. c:member:: int user_site_directory
668
669 If non-zero, add user site directory to :data:`sys.path`.
670
671 .. c:member:: int verbose
672
673 If non-zero, enable verbose mode.
674
675 .. c:member:: PyWideStringList warnoptions
676
677 Options of the :mod:`warnings` module to build warnings filters.
678
679 .. c:member:: int write_bytecode
680
681 If non-zero, write ``.pyc`` files.
682
Victor Stinner88feaec2019-09-26 03:15:07 +0200683 :data:`sys.dont_write_bytecode` is initialized to the inverted value of
684 :c:member:`~PyConfig.write_bytecode`.
685
Victor Stinner331a6a52019-05-27 16:39:22 +0200686 .. c:member:: PyWideStringList xoptions
687
688 :data:`sys._xoptions`.
689
690If ``parse_argv`` is non-zero, ``argv`` arguments are parsed the same
691way the regular Python parses command line arguments, and Python
692arguments are stripped from ``argv``: see :ref:`Command Line Arguments
693<using-on-cmdline>`.
694
695The ``xoptions`` options are parsed to set other options: see :option:`-X`
696option.
697
698
699Initialization with PyConfig
700----------------------------
701
702Function to initialize Python:
703
704.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
705
706 Initialize Python from *config* configuration.
707
708The caller is responsible to handle exceptions (error or exit) using
709:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
710
Victor Stinner88feaec2019-09-26 03:15:07 +0200711If ``PyImport_FrozenModules``, ``PyImport_AppendInittab()`` or
712``PyImport_ExtendInittab()`` are used, they must be set or called after Python
Victor Stinner331a6a52019-05-27 16:39:22 +0200713preinitialization and before the Python initialization.
714
715Example setting the program name::
716
717 void init_python(void)
718 {
719 PyStatus status;
720 PyConfig config;
721
722 status = PyConfig_InitPythonConfig(&config);
723 if (PyStatus_Exception(status)) {
724 goto fail;
725 }
726
727 /* Set the program name. Implicitly preinitialize Python. */
728 status = PyConfig_SetString(&config, &config.program_name,
729 L"/path/to/my_program");
730 if (PyStatus_Exception(status)) {
731 goto fail;
732 }
733
734 status = Py_InitializeFromConfig(&config);
735 if (PyStatus_Exception(status)) {
736 goto fail;
737 }
738 PyConfig_Clear(&config);
739 return;
740
741 fail:
742 PyConfig_Clear(&config);
743 Py_ExitStatusException(status);
744 }
745
746More complete example modifying the default configuration, read the
747configuration, and then override some parameters::
748
749 PyStatus init_python(const char *program_name)
750 {
751 PyStatus status;
752 PyConfig config;
753
754 status = PyConfig_InitPythonConfig(&config);
755 if (PyStatus_Exception(status)) {
756 goto done;
757 }
758
759 /* Set the program name before reading the configuraton
760 (decode byte string from the locale encoding).
761
762 Implicitly preinitialize Python. */
763 status = PyConfig_SetBytesString(&config, &config.program_name,
764 program_name);
765 if (PyStatus_Exception(status)) {
766 goto done;
767 }
768
769 /* Read all configuration at once */
770 status = PyConfig_Read(&config);
771 if (PyStatus_Exception(status)) {
772 goto done;
773 }
774
775 /* Append our custom search path to sys.path */
776 status = PyWideStringList_Append(&config.module_search_paths,
Victor Stinner88feaec2019-09-26 03:15:07 +0200777 L"/path/to/more/modules");
Victor Stinner331a6a52019-05-27 16:39:22 +0200778 if (PyStatus_Exception(status)) {
779 goto done;
780 }
781
782 /* Override executable computed by PyConfig_Read() */
783 status = PyConfig_SetString(&config, &config.executable,
784 L"/path/to/my_executable");
785 if (PyStatus_Exception(status)) {
786 goto done;
787 }
788
789 status = Py_InitializeFromConfig(&config);
790
791 done:
792 PyConfig_Clear(&config);
793 return status;
794 }
795
796
797.. _init-isolated-conf:
798
799Isolated Configuration
800----------------------
801
802:c:func:`PyPreConfig_InitIsolatedConfig` and
803:c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to
804isolate Python from the system. For example, to embed Python into an
805application.
806
807This configuration ignores global configuration variables, environments
Victor Stinner88feaec2019-09-26 03:15:07 +0200808variables, command line arguments (:c:member:`PyConfig.argv` is not parsed)
809and user site directory. The C standard streams (ex: ``stdout``) and the
810LC_CTYPE locale are left unchanged. Signal handlers are not installed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200811
812Configuration files are still used with this configuration. Set the
813:ref:`Path Configuration <init-path-config>` ("output fields") to ignore these
814configuration files and avoid the function computing the default path
815configuration.
816
817
818.. _init-python-config:
819
820Python Configuration
821--------------------
822
823:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
824functions create a configuration to build a customized Python which behaves as
825the regular Python.
826
827Environments variables and command line arguments are used to configure
828Python, whereas global configuration variables are ignored.
829
830This function enables C locale coercion (:pep:`538`) and UTF-8 Mode
831(:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and
832:envvar:`PYTHONCOERCECLOCALE` environment variables.
833
834Example of customized Python always running in isolated mode::
835
836 int main(int argc, char **argv)
837 {
838 PyConfig config;
839 PyStatus status;
840
841 status = PyConfig_InitPythonConfig(&config);
842 if (PyStatus_Exception(status)) {
843 goto fail;
844 }
845
846 config.isolated = 1;
847
848 /* Decode command line arguments.
849 Implicitly preinitialize Python (in isolated mode). */
850 status = PyConfig_SetBytesArgv(&config, argc, argv);
851 if (PyStatus_Exception(status)) {
852 goto fail;
853 }
854
855 status = Py_InitializeFromConfig(&config);
856 if (PyStatus_Exception(status)) {
857 goto fail;
858 }
859 PyConfig_Clear(&config);
860
861 return Py_RunMain();
862
863 fail:
864 PyConfig_Clear(&config);
865 if (PyStatus_IsExit(status)) {
866 return status.exitcode;
867 }
868 /* Display the error message and exit the process with
869 non-zero exit code */
870 Py_ExitStatusException(status);
871 }
872
873
874.. _init-path-config:
875
876Path Configuration
877------------------
878
879:c:type:`PyConfig` contains multiple fields for the path configuration:
880
Victor Stinner8bf39b62019-09-26 02:22:35 +0200881* Path configuration inputs:
Victor Stinner331a6a52019-05-27 16:39:22 +0200882
883 * :c:member:`PyConfig.home`
Victor Stinner331a6a52019-05-27 16:39:22 +0200884 * :c:member:`PyConfig.pathconfig_warnings`
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200885 * :c:member:`PyConfig.program_name`
886 * :c:member:`PyConfig.pythonpath_env`
Victor Stinner8bf39b62019-09-26 02:22:35 +0200887 * current working directory: to get absolute paths
888 * ``PATH`` environment variable to get the program full path
889 (from :c:member:`PyConfig.program_name`)
890 * ``__PYVENV_LAUNCHER__`` environment variable
891 * (Windows only) Application paths in the registry under
892 "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and
893 HKEY_LOCAL_MACHINE (where X.Y is the Python version).
Victor Stinner331a6a52019-05-27 16:39:22 +0200894
895* Path configuration output fields:
896
Victor Stinner8bf39b62019-09-26 02:22:35 +0200897 * :c:member:`PyConfig.base_exec_prefix`
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200898 * :c:member:`PyConfig.base_executable`
Victor Stinner8bf39b62019-09-26 02:22:35 +0200899 * :c:member:`PyConfig.base_prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +0200900 * :c:member:`PyConfig.exec_prefix`
901 * :c:member:`PyConfig.executable`
Victor Stinner331a6a52019-05-27 16:39:22 +0200902 * :c:member:`PyConfig.module_search_paths_set`,
903 :c:member:`PyConfig.module_search_paths`
Victor Stinner8bf39b62019-09-26 02:22:35 +0200904 * :c:member:`PyConfig.prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +0200905
Victor Stinner8bf39b62019-09-26 02:22:35 +0200906If at least one "output field" is not set, Python calculates the path
Victor Stinner331a6a52019-05-27 16:39:22 +0200907configuration to fill unset fields. If
908:c:member:`~PyConfig.module_search_paths_set` is equal to 0,
Min ho Kim39d87b52019-08-31 06:21:19 +1000909:c:member:`~PyConfig.module_search_paths` is overridden and
Victor Stinner331a6a52019-05-27 16:39:22 +0200910:c:member:`~PyConfig.module_search_paths_set` is set to 1.
911
Victor Stinner8bf39b62019-09-26 02:22:35 +0200912It is possible to completely ignore the function calculating the default
Victor Stinner331a6a52019-05-27 16:39:22 +0200913path configuration by setting explicitly all path configuration output
914fields listed above. A string is considered as set even if it is non-empty.
915``module_search_paths`` is considered as set if
916``module_search_paths_set`` is set to 1. In this case, path
917configuration input fields are ignored as well.
918
919Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when
Victor Stinner8bf39b62019-09-26 02:22:35 +0200920calculating the path configuration (Unix only, Windows does not log any warning).
Victor Stinner331a6a52019-05-27 16:39:22 +0200921
922If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix`
923fields are not set, they inherit their value from :c:member:`~PyConfig.prefix`
924and :c:member:`~PyConfig.exec_prefix` respectively.
925
926:c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`:
927
928* If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a
929 ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to
930 :data:`sys.path`.
931* If :c:member:`~PyConfig.isolated` is zero:
932
933 * If :c:member:`~PyConfig.run_module` is set, prepend the current directory
934 to :data:`sys.path`. Do nothing if the current directory cannot be read.
935 * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the
936 filename to :data:`sys.path`.
937 * Otherwise, prepend an empty string to :data:`sys.path`.
938
939If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be
940modified by the :mod:`site` module. If
941:c:member:`~PyConfig.user_site_directory` is non-zero and the user's
942site-package directory exists, the :mod:`site` module appends the user's
943site-package directory to :data:`sys.path`.
944
945The following configuration files are used by the path configuration:
946
947* ``pyvenv.cfg``
948* ``python._pth`` (Windows only)
949* ``pybuilddir.txt`` (Unix only)
950
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200951The ``__PYVENV_LAUNCHER__`` environment variable is used to set
952:c:member:`PyConfig.base_executable`
953
Victor Stinner331a6a52019-05-27 16:39:22 +0200954
955Py_RunMain()
956------------
957
958.. c:function:: int Py_RunMain(void)
959
960 Execute the command (:c:member:`PyConfig.run_command`), the script
961 (:c:member:`PyConfig.run_filename`) or the module
962 (:c:member:`PyConfig.run_module`) specified on the command line or in the
963 configuration.
964
965 By default and when if :option:`-i` option is used, run the REPL.
966
967 Finally, finalizes Python and returns an exit status that can be passed to
968 the ``exit()`` function.
969
970See :ref:`Python Configuration <init-python-config>` for an example of
971customized Python always running in isolated mode using
972:c:func:`Py_RunMain`.
973
974
975Multi-Phase Initialization Private Provisional API
976--------------------------------------------------
977
978This section is a private provisional API introducing multi-phase
979initialization, the core feature of the :pep:`432`:
980
981* "Core" initialization phase, "bare minimum Python":
982
983 * Builtin types;
984 * Builtin exceptions;
985 * Builtin and frozen modules;
986 * The :mod:`sys` module is only partially initialized
Victor Stinner88feaec2019-09-26 03:15:07 +0200987 (ex: :data:`sys.path` doesn't exist yet).
Victor Stinner331a6a52019-05-27 16:39:22 +0200988
989* "Main" initialization phase, Python is fully initialized:
990
991 * Install and configure :mod:`importlib`;
992 * Apply the :ref:`Path Configuration <init-path-config>`;
993 * Install signal handlers;
994 * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout`
995 and :data:`sys.path`);
996 * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`;
997 * Import the :mod:`site` module;
998 * etc.
999
1000Private provisional API:
1001
1002* :c:member:`PyConfig._init_main`: if set to 0,
1003 :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase.
1004
1005.. c:function:: PyStatus _Py_InitializeMain(void)
1006
1007 Move to the "Main" initialization phase, finish the Python initialization.
1008
1009No module is imported during the "Core" phase and the ``importlib`` module is
1010not configured: the :ref:`Path Configuration <init-path-config>` is only
1011applied during the "Main" phase. It may allow to customize Python in Python to
1012override or tune the :ref:`Path Configuration <init-path-config>`, maybe
Victor Stinner88feaec2019-09-26 03:15:07 +02001013install a custom :data:`sys.meta_path` importer or an import hook, etc.
Victor Stinner331a6a52019-05-27 16:39:22 +02001014
Victor Stinner88feaec2019-09-26 03:15:07 +02001015It may become possible to calculatin the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001016<init-path-config>` in Python, after the Core phase and before the Main phase,
1017which is one of the :pep:`432` motivation.
1018
1019The "Core" phase is not properly defined: what should be and what should
1020not be available at this phase is not specified yet. The API is marked
1021as private and provisional: the API can be modified or even be removed
1022anytime until a proper public API is designed.
1023
1024Example running Python code between "Core" and "Main" initialization
1025phases::
1026
1027 void init_python(void)
1028 {
1029 PyStatus status;
1030 PyConfig config;
1031
1032 status = PyConfig_InitPythonConfig(&config);
1033 if (PyStatus_Exception(status)) {
1034 PyConfig_Clear(&config);
1035 Py_ExitStatusException(status);
1036 }
1037
1038 config._init_main = 0;
1039
1040 /* ... customize 'config' configuration ... */
1041
1042 status = Py_InitializeFromConfig(&config);
1043 PyConfig_Clear(&config);
1044 if (PyStatus_Exception(status)) {
1045 Py_ExitStatusException(status);
1046 }
1047
1048 /* Use sys.stderr because sys.stdout is only created
1049 by _Py_InitializeMain() */
1050 int res = PyRun_SimpleString(
1051 "import sys; "
1052 "print('Run Python code before _Py_InitializeMain', "
1053 "file=sys.stderr)");
1054 if (res < 0) {
1055 exit(1);
1056 }
1057
1058 /* ... put more configuration code here ... */
1059
1060 status = _Py_InitializeMain();
1061 if (PyStatus_Exception(status)) {
1062 Py_ExitStatusException(status);
1063 }
1064 }