blob: 4c77d5d1e0e82733b6bdea15e6073947b11ea8cb [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
Victor Stinner441b10c2019-09-28 04:28:35 +0200197 The :c:member:`struct_size` field must be explicitly initialized to
198 ``sizeof(PyPreConfig)``.
199
Victor Stinner331a6a52019-05-27 16:39:22 +0200200 Function to initialize a preconfiguration:
201
Victor Stinner441b10c2019-09-28 04:28:35 +0200202 .. c:function:: PyStatus PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200203
204 Initialize the preconfiguration with :ref:`Python Configuration
205 <init-python-config>`.
206
Victor Stinner441b10c2019-09-28 04:28:35 +0200207 .. c:function:: PyStatus PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)
Victor Stinner331a6a52019-05-27 16:39:22 +0200208
209 Initialize the preconfiguration with :ref:`Isolated Configuration
210 <init-isolated-conf>`.
211
Victor Stinner441b10c2019-09-28 04:28:35 +0200212 The caller of these functions is responsible to handle exceptions (error or
213 exit) using :c:func:`PyStatus_Exception` and
214 :c:func:`Py_ExitStatusException`.
215
Victor Stinner331a6a52019-05-27 16:39:22 +0200216 Structure fields:
217
218 .. c:member:: int allocator
219
220 Name of the memory allocator:
221
222 * ``PYMEM_ALLOCATOR_NOT_SET`` (``0``): don't change memory allocators
223 (use defaults)
224 * ``PYMEM_ALLOCATOR_DEFAULT`` (``1``): default memory allocators
225 * ``PYMEM_ALLOCATOR_DEBUG`` (``2``): default memory allocators with
226 debug hooks
227 * ``PYMEM_ALLOCATOR_MALLOC`` (``3``): force usage of ``malloc()``
228 * ``PYMEM_ALLOCATOR_MALLOC_DEBUG`` (``4``): force usage of
229 ``malloc()`` with debug hooks
230 * ``PYMEM_ALLOCATOR_PYMALLOC`` (``5``): :ref:`Python pymalloc memory
231 allocator <pymalloc>`
232 * ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG`` (``6``): :ref:`Python pymalloc
233 memory allocator <pymalloc>` with debug hooks
234
235 ``PYMEM_ALLOCATOR_PYMALLOC`` and ``PYMEM_ALLOCATOR_PYMALLOC_DEBUG``
236 are not supported if Python is configured using ``--without-pymalloc``
237
238 See :ref:`Memory Management <memory>`.
239
240 .. c:member:: int configure_locale
241
242 Set the LC_CTYPE locale to the user preferred locale? If equals to 0, set
243 :c:member:`coerce_c_locale` and :c:member:`coerce_c_locale_warn` to 0.
244
245 .. c:member:: int coerce_c_locale
246
247 If equals to 2, coerce the C locale; if equals to 1, read the LC_CTYPE
248 locale to decide if it should be coerced.
249
250 .. c:member:: int coerce_c_locale_warn
Victor Stinner88feaec2019-09-26 03:15:07 +0200251
Victor Stinner331a6a52019-05-27 16:39:22 +0200252 If non-zero, emit a warning if the C locale is coerced.
253
254 .. c:member:: int dev_mode
255
256 See :c:member:`PyConfig.dev_mode`.
257
258 .. c:member:: int isolated
259
260 See :c:member:`PyConfig.isolated`.
261
262 .. c:member:: int legacy_windows_fs_encoding (Windows only)
263
264 If non-zero, disable UTF-8 Mode, set the Python filesystem encoding to
265 ``mbcs``, set the filesystem error handler to ``replace``.
266
267 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
268 Windows specific code.
269
270 .. c:member:: int parse_argv
271
272 If non-zero, :c:func:`Py_PreInitializeFromArgs` and
273 :c:func:`Py_PreInitializeFromBytesArgs` parse their ``argv`` argument the
274 same way the regular Python parses command line arguments: see
275 :ref:`Command Line Arguments <using-on-cmdline>`.
276
Victor Stinner441b10c2019-09-28 04:28:35 +0200277 .. c:member:: size_t struct_size
278
279 Size of the structure in bytes: must be initialized to
280 ``sizeof(PyPreConfig)``.
281
282 Field used for API and ABI compatibility.
283
Victor Stinner331a6a52019-05-27 16:39:22 +0200284 .. c:member:: int use_environment
285
286 See :c:member:`PyConfig.use_environment`.
287
288 .. c:member:: int utf8_mode
289
290 If non-zero, enable the UTF-8 mode.
291
292Preinitialization with PyPreConfig
293----------------------------------
294
295Functions to preinitialize Python:
296
297.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
298
299 Preinitialize Python from *preconfig* preconfiguration.
300
301.. c:function:: PyStatus Py_PreInitializeFromBytesArgs(const PyPreConfig *preconfig, int argc, char * const *argv)
302
303 Preinitialize Python from *preconfig* preconfiguration and command line
304 arguments (bytes strings).
305
306.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)
307
308 Preinitialize Python from *preconfig* preconfiguration and command line
309 arguments (wide strings).
310
311The caller is responsible to handle exceptions (error or exit) using
312:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
313
314For :ref:`Python Configuration <init-python-config>`
315(:c:func:`PyPreConfig_InitPythonConfig`), if Python is initialized with
316command line arguments, the command line arguments must also be passed to
317preinitialize Python, since they have an effect on the pre-configuration
Victor Stinner88feaec2019-09-26 03:15:07 +0200318like encodings. For example, the :option:`-X utf8 <-X>` command line option
Victor Stinner331a6a52019-05-27 16:39:22 +0200319enables the UTF-8 Mode.
320
321``PyMem_SetAllocator()`` can be called after :c:func:`Py_PreInitialize` and
322before :c:func:`Py_InitializeFromConfig` to install a custom memory allocator.
323It can be called before :c:func:`Py_PreInitialize` if
324:c:member:`PyPreConfig.allocator` is set to ``PYMEM_ALLOCATOR_NOT_SET``.
325
326Python memory allocation functions like :c:func:`PyMem_RawMalloc` must not be
327used before Python preinitialization, whereas calling directly ``malloc()`` and
328``free()`` is always safe. :c:func:`Py_DecodeLocale` must not be called before
329the preinitialization.
330
331Example using the preinitialization to enable the UTF-8 Mode::
332
Victor Stinner441b10c2019-09-28 04:28:35 +0200333 PyStatus status;
Victor Stinner331a6a52019-05-27 16:39:22 +0200334 PyPreConfig preconfig;
Victor Stinner441b10c2019-09-28 04:28:35 +0200335 preconfig.struct_size = sizeof(PyPreConfig);
336
337 status = PyPreConfig_InitPythonConfig(&preconfig);
338 if (PyStatus_Exception(status)) {
339 Py_ExitStatusException(status);
340 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200341
342 preconfig.utf8_mode = 1;
343
Victor Stinner441b10c2019-09-28 04:28:35 +0200344 status = Py_PreInitialize(&preconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200345 if (PyStatus_Exception(status)) {
346 Py_ExitStatusException(status);
347 }
348
349 /* at this point, Python will speak UTF-8 */
350
351 Py_Initialize();
352 /* ... use Python API here ... */
353 Py_Finalize();
354
355
356PyConfig
357--------
358
359.. c:type:: PyConfig
360
361 Structure containing most parameters to configure Python.
362
Victor Stinner441b10c2019-09-28 04:28:35 +0200363 The :c:member:`struct_size` field must be explicitly initialized to
364 ``sizeof(PyConfig)``.
365
Victor Stinner331a6a52019-05-27 16:39:22 +0200366 Structure methods:
367
368 .. c:function:: PyStatus PyConfig_InitPythonConfig(PyConfig *config)
369
370 Initialize configuration with :ref:`Python Configuration
371 <init-python-config>`.
372
373 .. c:function:: PyStatus PyConfig_InitIsolatedConfig(PyConfig *config)
374
375 Initialize configuration with :ref:`Isolated Configuration
376 <init-isolated-conf>`.
377
378 .. c:function:: PyStatus PyConfig_SetString(PyConfig *config, wchar_t * const *config_str, const wchar_t *str)
379
380 Copy the wide character string *str* into ``*config_str``.
381
382 Preinitialize Python if needed.
383
384 .. c:function:: PyStatus PyConfig_SetBytesString(PyConfig *config, wchar_t * const *config_str, const char *str)
385
386 Decode *str* using ``Py_DecodeLocale()`` and set the result into ``*config_str``.
387
388 Preinitialize Python if needed.
389
390 .. c:function:: PyStatus PyConfig_SetArgv(PyConfig *config, int argc, wchar_t * const *argv)
391
392 Set command line arguments from wide character strings.
393
394 Preinitialize Python if needed.
395
396 .. c:function:: PyStatus PyConfig_SetBytesArgv(PyConfig *config, int argc, char * const *argv)
397
398 Set command line arguments: decode bytes using :c:func:`Py_DecodeLocale`.
399
400 Preinitialize Python if needed.
401
Victor Stinner36242fd2019-07-01 19:13:50 +0200402 .. c:function:: PyStatus PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list, Py_ssize_t length, wchar_t **items)
403
404 Set the list of wide strings *list* to *length* and *items*.
405
406 Preinitialize Python if needed.
407
Victor Stinner331a6a52019-05-27 16:39:22 +0200408 .. c:function:: PyStatus PyConfig_Read(PyConfig *config)
409
410 Read all Python configuration.
411
412 Fields which are already initialized are left unchanged.
413
414 Preinitialize Python if needed.
415
416 .. c:function:: void PyConfig_Clear(PyConfig *config)
417
418 Release configuration memory.
419
420 Most ``PyConfig`` methods preinitialize Python if needed. In that case, the
421 Python preinitialization configuration in based on the :c:type:`PyConfig`.
422 If configuration fields which are in common with :c:type:`PyPreConfig` are
423 tuned, they must be set before calling a :c:type:`PyConfig` method:
424
425 * :c:member:`~PyConfig.dev_mode`
426 * :c:member:`~PyConfig.isolated`
427 * :c:member:`~PyConfig.parse_argv`
428 * :c:member:`~PyConfig.use_environment`
429
430 Moreover, if :c:func:`PyConfig_SetArgv` or :c:func:`PyConfig_SetBytesArgv`
431 is used, this method must be called first, before other methods, since the
432 preinitialization configuration depends on command line arguments (if
433 :c:member:`parse_argv` is non-zero).
434
435 The caller of these methods is responsible to handle exceptions (error or
436 exit) using ``PyStatus_Exception()`` and ``Py_ExitStatusException()``.
437
438 Structure fields:
439
440 .. c:member:: PyWideStringList argv
441
442 Command line arguments, :data:`sys.argv`. See
443 :c:member:`~PyConfig.parse_argv` to parse :c:member:`~PyConfig.argv` the
444 same way the regular Python parses Python command line arguments. If
445 :c:member:`~PyConfig.argv` is empty, an empty string is added to ensure
446 that :data:`sys.argv` always exists and is never empty.
447
448 .. c:member:: wchar_t* base_exec_prefix
449
450 :data:`sys.base_exec_prefix`.
451
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200452 .. c:member:: wchar_t* base_executable
453
454 :data:`sys._base_executable`: ``__PYVENV_LAUNCHER__`` environment
455 variable value, or copy of :c:member:`PyConfig.executable`.
456
Victor Stinner331a6a52019-05-27 16:39:22 +0200457 .. c:member:: wchar_t* base_prefix
458
459 :data:`sys.base_prefix`.
460
461 .. c:member:: int buffered_stdio
462
463 If equals to 0, enable unbuffered mode, making the stdout and stderr
464 streams unbuffered.
465
466 stdin is always opened in buffered mode.
467
468 .. c:member:: int bytes_warning
469
470 If equals to 1, issue a warning when comparing :class:`bytes` or
471 :class:`bytearray` with :class:`str`, or comparing :class:`bytes` with
472 :class:`int`. If equal or greater to 2, raise a :exc:`BytesWarning`
473 exception.
474
475 .. c:member:: wchar_t* check_hash_pycs_mode
476
477 Control the validation behavior of hash-based ``.pyc`` files (see
478 :pep:`552`): :option:`--check-hash-based-pycs` command line option value.
479
480 Valid values: ``always``, ``never`` and ``default``.
481
482 The default value is: ``default``.
483
484 .. c:member:: int configure_c_stdio
485
486 If non-zero, configure C standard streams (``stdio``, ``stdout``,
487 ``stdout``). For example, set their mode to ``O_BINARY`` on Windows.
488
489 .. c:member:: int dev_mode
490
Victor Stinner88feaec2019-09-26 03:15:07 +0200491 Development mode: see :option:`-X dev <-X>`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200492
493 .. c:member:: int dump_refs
494
495 If non-zero, dump all objects which are still alive at exit.
496
497 Require a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
498
499 .. c:member:: wchar_t* exec_prefix
500
501 :data:`sys.exec_prefix`.
502
503 .. c:member:: wchar_t* executable
504
505 :data:`sys.executable`.
506
507 .. c:member:: int faulthandler
508
Victor Stinner88feaec2019-09-26 03:15:07 +0200509 If non-zero, call :func:`faulthandler.enable` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200510
511 .. c:member:: wchar_t* filesystem_encoding
512
513 Filesystem encoding, :func:`sys.getfilesystemencoding`.
514
515 .. c:member:: wchar_t* filesystem_errors
516
517 Filesystem encoding errors, :func:`sys.getfilesystemencodeerrors`.
518
519 .. c:member:: unsigned long hash_seed
520 .. c:member:: int use_hash_seed
521
522 Randomized hash function seed.
523
524 If :c:member:`~PyConfig.use_hash_seed` is zero, a seed is chosen randomly
525 at Pythonstartup, and :c:member:`~PyConfig.hash_seed` is ignored.
526
527 .. c:member:: wchar_t* home
528
529 Python home directory.
530
Victor Stinner88feaec2019-09-26 03:15:07 +0200531 Initialized from :envvar:`PYTHONHOME` environment variable value by
532 default.
533
Victor Stinner331a6a52019-05-27 16:39:22 +0200534 .. c:member:: int import_time
535
536 If non-zero, profile import time.
537
538 .. c:member:: int inspect
539
540 Enter interactive mode after executing a script or a command.
541
542 .. c:member:: int install_signal_handlers
543
544 Install signal handlers?
545
546 .. c:member:: int interactive
547
548 Interactive mode.
549
550 .. c:member:: int isolated
551
552 If greater than 0, enable isolated mode:
553
554 * :data:`sys.path` contains neither the script's directory (computed from
555 ``argv[0]`` or the current directory) nor the user's site-packages
556 directory.
557 * Python REPL doesn't import :mod:`readline` nor enable default readline
558 configuration on interactive prompts.
559 * Set :c:member:`~PyConfig.use_environment` and
560 :c:member:`~PyConfig.user_site_directory` to 0.
561
562 .. c:member:: int legacy_windows_stdio
563
564 If non-zero, use :class:`io.FileIO` instead of
565 :class:`io.WindowsConsoleIO` for :data:`sys.stdin`, :data:`sys.stdout`
566 and :data:`sys.stderr`.
567
568 Only available on Windows. ``#ifdef MS_WINDOWS`` macro can be used for
569 Windows specific code.
570
571 .. c:member:: int malloc_stats
572
573 If non-zero, dump statistics on :ref:`Python pymalloc memory allocator
574 <pymalloc>` at exit.
575
576 The option is ignored if Python is built using ``--without-pymalloc``.
577
578 .. c:member:: wchar_t* pythonpath_env
579
580 Module search paths as a string separated by ``DELIM``
581 (:data:`os.path.pathsep`).
582
583 Initialized from :envvar:`PYTHONPATH` environment variable value by
584 default.
585
586 .. c:member:: PyWideStringList module_search_paths
587 .. c:member:: int module_search_paths_set
588
589 :data:`sys.path`. If :c:member:`~PyConfig.module_search_paths_set` is
590 equal to 0, the :c:member:`~PyConfig.module_search_paths` is overridden
Victor Stinner88feaec2019-09-26 03:15:07 +0200591 by the function calculating the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +0200592 <init-path-config>`.
593
594 .. c:member:: int optimization_level
595
596 Compilation optimization level:
597
598 * 0: Peephole optimizer (and ``__debug__`` is set to ``True``)
599 * 1: Remove assertions, set ``__debug__`` to ``False``
600 * 2: Strip docstrings
601
602 .. c:member:: int parse_argv
603
604 If non-zero, parse :c:member:`~PyConfig.argv` the same way the regular
605 Python command line arguments, and strip Python arguments from
606 :c:member:`~PyConfig.argv`: see :ref:`Command Line Arguments
607 <using-on-cmdline>`.
608
609 .. c:member:: int parser_debug
610
611 If non-zero, turn on parser debugging output (for expert only, depending
612 on compilation options).
613
614 .. c:member:: int pathconfig_warnings
615
Victor Stinner88feaec2019-09-26 03:15:07 +0200616 If equal to 0, suppress warnings when calculating the :ref:`Path
617 Configuration <init-path-config>` (Unix only, Windows does not log any
618 warning). Otherwise, warnings are written into ``stderr``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200619
620 .. c:member:: wchar_t* prefix
621
622 :data:`sys.prefix`.
623
624 .. c:member:: wchar_t* program_name
625
Victor Stinner88feaec2019-09-26 03:15:07 +0200626 Program name. Used to initialize :c:member:`~PyConfig.executable`, and in
627 early error messages.
Victor Stinner331a6a52019-05-27 16:39:22 +0200628
629 .. c:member:: wchar_t* pycache_prefix
630
Victor Stinner88feaec2019-09-26 03:15:07 +0200631 :data:`sys.pycache_prefix`: ``.pyc`` cache prefix.
632
633 If NULL, :data:`sys.pycache_prefix` is set to ``None``.
Victor Stinner331a6a52019-05-27 16:39:22 +0200634
635 .. c:member:: int quiet
636
637 Quiet mode. For example, don't display the copyright and version messages
Victor Stinner88feaec2019-09-26 03:15:07 +0200638 in interactive mode.
Victor Stinner331a6a52019-05-27 16:39:22 +0200639
640 .. c:member:: wchar_t* run_command
641
Victor Stinner88feaec2019-09-26 03:15:07 +0200642 ``python3 -c COMMAND`` argument. Used by :c:func:`Py_RunMain`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200643
644 .. c:member:: wchar_t* run_filename
645
Victor Stinner88feaec2019-09-26 03:15:07 +0200646 ``python3 FILENAME`` argument. Used by :c:func:`Py_RunMain`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200647
648 .. c:member:: wchar_t* run_module
649
Victor Stinner88feaec2019-09-26 03:15:07 +0200650 ``python3 -m MODULE`` argument. Used by :c:func:`Py_RunMain`.
Victor Stinner331a6a52019-05-27 16:39:22 +0200651
652 .. c:member:: int show_alloc_count
653
654 Show allocation counts at exit?
655
Victor Stinner88feaec2019-09-26 03:15:07 +0200656 Set to 1 by :option:`-X showalloccount <-X>` command line option.
657
Victor Stinner331a6a52019-05-27 16:39:22 +0200658 Need a special Python build with ``COUNT_ALLOCS`` macro defined.
659
660 .. c:member:: int show_ref_count
661
662 Show total reference count at exit?
663
Victor Stinner88feaec2019-09-26 03:15:07 +0200664 Set to 1 by :option:`-X showrefcount <-X>` command line option.
665
Victor Stinner331a6a52019-05-27 16:39:22 +0200666 Need a debug build of Python (``Py_REF_DEBUG`` macro must be defined).
667
668 .. c:member:: int site_import
669
670 Import the :mod:`site` module at startup?
671
672 .. c:member:: int skip_source_first_line
673
674 Skip the first line of the source?
675
676 .. c:member:: wchar_t* stdio_encoding
677 .. c:member:: wchar_t* stdio_errors
678
679 Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
680 :data:`sys.stderr`.
681
Victor Stinner441b10c2019-09-28 04:28:35 +0200682 .. c:member:: size_t struct_size
683
684 Size of the structure in bytes: must be initialized to
685 ``sizeof(PyConfig)``.
686
687 Field used for API and ABI compatibility.
688
Victor Stinner331a6a52019-05-27 16:39:22 +0200689 .. c:member:: int tracemalloc
690
Victor Stinner88feaec2019-09-26 03:15:07 +0200691 If non-zero, call :func:`tracemalloc.start` at startup.
Victor Stinner331a6a52019-05-27 16:39:22 +0200692
693 .. c:member:: int use_environment
694
695 If greater than 0, use :ref:`environment variables <using-on-envvars>`.
696
697 .. c:member:: int user_site_directory
698
699 If non-zero, add user site directory to :data:`sys.path`.
700
701 .. c:member:: int verbose
702
703 If non-zero, enable verbose mode.
704
705 .. c:member:: PyWideStringList warnoptions
706
Victor Stinnerfb4ae152019-09-30 01:40:17 +0200707 :data:`sys.warnoptions`: options of the :mod:`warnings` module to build
708 warnings filters: lowest to highest priority.
709
710 The :mod:`warnings` module adds :data:`sys.warnoptions` in the reverse
711 order: the last :c:member:`PyConfig.warnoptions` item becomes the first
712 item of :data:`warnings.filters` which is checked first (highest
713 priority).
Victor Stinner331a6a52019-05-27 16:39:22 +0200714
715 .. c:member:: int write_bytecode
716
717 If non-zero, write ``.pyc`` files.
718
Victor Stinner88feaec2019-09-26 03:15:07 +0200719 :data:`sys.dont_write_bytecode` is initialized to the inverted value of
720 :c:member:`~PyConfig.write_bytecode`.
721
Victor Stinner331a6a52019-05-27 16:39:22 +0200722 .. c:member:: PyWideStringList xoptions
723
724 :data:`sys._xoptions`.
725
726If ``parse_argv`` is non-zero, ``argv`` arguments are parsed the same
727way the regular Python parses command line arguments, and Python
728arguments are stripped from ``argv``: see :ref:`Command Line Arguments
729<using-on-cmdline>`.
730
731The ``xoptions`` options are parsed to set other options: see :option:`-X`
732option.
733
734
735Initialization with PyConfig
736----------------------------
737
738Function to initialize Python:
739
740.. c:function:: PyStatus Py_InitializeFromConfig(const PyConfig *config)
741
742 Initialize Python from *config* configuration.
743
744The caller is responsible to handle exceptions (error or exit) using
745:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
746
Victor Stinner88feaec2019-09-26 03:15:07 +0200747If ``PyImport_FrozenModules``, ``PyImport_AppendInittab()`` or
748``PyImport_ExtendInittab()`` are used, they must be set or called after Python
Victor Stinner331a6a52019-05-27 16:39:22 +0200749preinitialization and before the Python initialization.
750
751Example setting the program name::
752
753 void init_python(void)
754 {
755 PyStatus status;
756 PyConfig config;
Victor Stinner441b10c2019-09-28 04:28:35 +0200757 config.struct_size = sizeof(PyConfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200758
759 status = PyConfig_InitPythonConfig(&config);
760 if (PyStatus_Exception(status)) {
761 goto fail;
762 }
763
764 /* Set the program name. Implicitly preinitialize Python. */
765 status = PyConfig_SetString(&config, &config.program_name,
766 L"/path/to/my_program");
767 if (PyStatus_Exception(status)) {
768 goto fail;
769 }
770
771 status = Py_InitializeFromConfig(&config);
772 if (PyStatus_Exception(status)) {
773 goto fail;
774 }
775 PyConfig_Clear(&config);
776 return;
777
778 fail:
779 PyConfig_Clear(&config);
780 Py_ExitStatusException(status);
781 }
782
783More complete example modifying the default configuration, read the
784configuration, and then override some parameters::
785
786 PyStatus init_python(const char *program_name)
787 {
788 PyStatus status;
789 PyConfig config;
Victor Stinner441b10c2019-09-28 04:28:35 +0200790 config.struct_size = sizeof(PyConfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200791
792 status = PyConfig_InitPythonConfig(&config);
793 if (PyStatus_Exception(status)) {
794 goto done;
795 }
796
797 /* Set the program name before reading the configuraton
798 (decode byte string from the locale encoding).
799
800 Implicitly preinitialize Python. */
801 status = PyConfig_SetBytesString(&config, &config.program_name,
802 program_name);
803 if (PyStatus_Exception(status)) {
804 goto done;
805 }
806
807 /* Read all configuration at once */
808 status = PyConfig_Read(&config);
809 if (PyStatus_Exception(status)) {
810 goto done;
811 }
812
813 /* Append our custom search path to sys.path */
814 status = PyWideStringList_Append(&config.module_search_paths,
Victor Stinner88feaec2019-09-26 03:15:07 +0200815 L"/path/to/more/modules");
Victor Stinner331a6a52019-05-27 16:39:22 +0200816 if (PyStatus_Exception(status)) {
817 goto done;
818 }
819
820 /* Override executable computed by PyConfig_Read() */
821 status = PyConfig_SetString(&config, &config.executable,
822 L"/path/to/my_executable");
823 if (PyStatus_Exception(status)) {
824 goto done;
825 }
826
827 status = Py_InitializeFromConfig(&config);
828
829 done:
830 PyConfig_Clear(&config);
831 return status;
832 }
833
834
835.. _init-isolated-conf:
836
837Isolated Configuration
838----------------------
839
840:c:func:`PyPreConfig_InitIsolatedConfig` and
841:c:func:`PyConfig_InitIsolatedConfig` functions create a configuration to
842isolate Python from the system. For example, to embed Python into an
843application.
844
845This configuration ignores global configuration variables, environments
Victor Stinner88feaec2019-09-26 03:15:07 +0200846variables, command line arguments (:c:member:`PyConfig.argv` is not parsed)
847and user site directory. The C standard streams (ex: ``stdout``) and the
848LC_CTYPE locale are left unchanged. Signal handlers are not installed.
Victor Stinner331a6a52019-05-27 16:39:22 +0200849
850Configuration files are still used with this configuration. Set the
851:ref:`Path Configuration <init-path-config>` ("output fields") to ignore these
852configuration files and avoid the function computing the default path
853configuration.
854
855
856.. _init-python-config:
857
858Python Configuration
859--------------------
860
861:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
862functions create a configuration to build a customized Python which behaves as
863the regular Python.
864
865Environments variables and command line arguments are used to configure
866Python, whereas global configuration variables are ignored.
867
868This function enables C locale coercion (:pep:`538`) and UTF-8 Mode
869(:pep:`540`) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and
870:envvar:`PYTHONCOERCECLOCALE` environment variables.
871
872Example of customized Python always running in isolated mode::
873
874 int main(int argc, char **argv)
875 {
Victor Stinner331a6a52019-05-27 16:39:22 +0200876 PyStatus status;
Victor Stinner441b10c2019-09-28 04:28:35 +0200877 PyConfig config;
878 config.struct_size = sizeof(PyConfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200879
880 status = PyConfig_InitPythonConfig(&config);
881 if (PyStatus_Exception(status)) {
882 goto fail;
883 }
884
885 config.isolated = 1;
886
887 /* Decode command line arguments.
888 Implicitly preinitialize Python (in isolated mode). */
889 status = PyConfig_SetBytesArgv(&config, argc, argv);
890 if (PyStatus_Exception(status)) {
891 goto fail;
892 }
893
894 status = Py_InitializeFromConfig(&config);
895 if (PyStatus_Exception(status)) {
896 goto fail;
897 }
898 PyConfig_Clear(&config);
899
900 return Py_RunMain();
901
902 fail:
903 PyConfig_Clear(&config);
904 if (PyStatus_IsExit(status)) {
905 return status.exitcode;
906 }
907 /* Display the error message and exit the process with
908 non-zero exit code */
909 Py_ExitStatusException(status);
910 }
911
912
913.. _init-path-config:
914
915Path Configuration
916------------------
917
918:c:type:`PyConfig` contains multiple fields for the path configuration:
919
Victor Stinner8bf39b62019-09-26 02:22:35 +0200920* Path configuration inputs:
Victor Stinner331a6a52019-05-27 16:39:22 +0200921
922 * :c:member:`PyConfig.home`
Victor Stinner331a6a52019-05-27 16:39:22 +0200923 * :c:member:`PyConfig.pathconfig_warnings`
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200924 * :c:member:`PyConfig.program_name`
925 * :c:member:`PyConfig.pythonpath_env`
Victor Stinner8bf39b62019-09-26 02:22:35 +0200926 * current working directory: to get absolute paths
927 * ``PATH`` environment variable to get the program full path
928 (from :c:member:`PyConfig.program_name`)
929 * ``__PYVENV_LAUNCHER__`` environment variable
930 * (Windows only) Application paths in the registry under
931 "Software\Python\PythonCore\X.Y\PythonPath" of HKEY_CURRENT_USER and
932 HKEY_LOCAL_MACHINE (where X.Y is the Python version).
Victor Stinner331a6a52019-05-27 16:39:22 +0200933
934* Path configuration output fields:
935
Victor Stinner8bf39b62019-09-26 02:22:35 +0200936 * :c:member:`PyConfig.base_exec_prefix`
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200937 * :c:member:`PyConfig.base_executable`
Victor Stinner8bf39b62019-09-26 02:22:35 +0200938 * :c:member:`PyConfig.base_prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +0200939 * :c:member:`PyConfig.exec_prefix`
940 * :c:member:`PyConfig.executable`
Victor Stinner331a6a52019-05-27 16:39:22 +0200941 * :c:member:`PyConfig.module_search_paths_set`,
942 :c:member:`PyConfig.module_search_paths`
Victor Stinner8bf39b62019-09-26 02:22:35 +0200943 * :c:member:`PyConfig.prefix`
Victor Stinner331a6a52019-05-27 16:39:22 +0200944
Victor Stinner8bf39b62019-09-26 02:22:35 +0200945If at least one "output field" is not set, Python calculates the path
Victor Stinner331a6a52019-05-27 16:39:22 +0200946configuration to fill unset fields. If
947:c:member:`~PyConfig.module_search_paths_set` is equal to 0,
Min ho Kim39d87b52019-08-31 06:21:19 +1000948:c:member:`~PyConfig.module_search_paths` is overridden and
Victor Stinner331a6a52019-05-27 16:39:22 +0200949:c:member:`~PyConfig.module_search_paths_set` is set to 1.
950
Victor Stinner8bf39b62019-09-26 02:22:35 +0200951It is possible to completely ignore the function calculating the default
Victor Stinner331a6a52019-05-27 16:39:22 +0200952path configuration by setting explicitly all path configuration output
953fields listed above. A string is considered as set even if it is non-empty.
954``module_search_paths`` is considered as set if
955``module_search_paths_set`` is set to 1. In this case, path
956configuration input fields are ignored as well.
957
958Set :c:member:`~PyConfig.pathconfig_warnings` to 0 to suppress warnings when
Victor Stinner8bf39b62019-09-26 02:22:35 +0200959calculating the path configuration (Unix only, Windows does not log any warning).
Victor Stinner331a6a52019-05-27 16:39:22 +0200960
961If :c:member:`~PyConfig.base_prefix` or :c:member:`~PyConfig.base_exec_prefix`
962fields are not set, they inherit their value from :c:member:`~PyConfig.prefix`
963and :c:member:`~PyConfig.exec_prefix` respectively.
964
965:c:func:`Py_RunMain` and :c:func:`Py_Main` modify :data:`sys.path`:
966
967* If :c:member:`~PyConfig.run_filename` is set and is a directory which contains a
968 ``__main__.py`` script, prepend :c:member:`~PyConfig.run_filename` to
969 :data:`sys.path`.
970* If :c:member:`~PyConfig.isolated` is zero:
971
972 * If :c:member:`~PyConfig.run_module` is set, prepend the current directory
973 to :data:`sys.path`. Do nothing if the current directory cannot be read.
974 * If :c:member:`~PyConfig.run_filename` is set, prepend the directory of the
975 filename to :data:`sys.path`.
976 * Otherwise, prepend an empty string to :data:`sys.path`.
977
978If :c:member:`~PyConfig.site_import` is non-zero, :data:`sys.path` can be
979modified by the :mod:`site` module. If
980:c:member:`~PyConfig.user_site_directory` is non-zero and the user's
981site-package directory exists, the :mod:`site` module appends the user's
982site-package directory to :data:`sys.path`.
983
984The following configuration files are used by the path configuration:
985
986* ``pyvenv.cfg``
987* ``python._pth`` (Windows only)
988* ``pybuilddir.txt`` (Unix only)
989
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200990The ``__PYVENV_LAUNCHER__`` environment variable is used to set
991:c:member:`PyConfig.base_executable`
992
Victor Stinner331a6a52019-05-27 16:39:22 +0200993
994Py_RunMain()
995------------
996
997.. c:function:: int Py_RunMain(void)
998
999 Execute the command (:c:member:`PyConfig.run_command`), the script
1000 (:c:member:`PyConfig.run_filename`) or the module
1001 (:c:member:`PyConfig.run_module`) specified on the command line or in the
1002 configuration.
1003
1004 By default and when if :option:`-i` option is used, run the REPL.
1005
1006 Finally, finalizes Python and returns an exit status that can be passed to
1007 the ``exit()`` function.
1008
1009See :ref:`Python Configuration <init-python-config>` for an example of
1010customized Python always running in isolated mode using
1011:c:func:`Py_RunMain`.
1012
1013
1014Multi-Phase Initialization Private Provisional API
1015--------------------------------------------------
1016
1017This section is a private provisional API introducing multi-phase
1018initialization, the core feature of the :pep:`432`:
1019
1020* "Core" initialization phase, "bare minimum Python":
1021
1022 * Builtin types;
1023 * Builtin exceptions;
1024 * Builtin and frozen modules;
1025 * The :mod:`sys` module is only partially initialized
Victor Stinner88feaec2019-09-26 03:15:07 +02001026 (ex: :data:`sys.path` doesn't exist yet).
Victor Stinner331a6a52019-05-27 16:39:22 +02001027
1028* "Main" initialization phase, Python is fully initialized:
1029
1030 * Install and configure :mod:`importlib`;
1031 * Apply the :ref:`Path Configuration <init-path-config>`;
1032 * Install signal handlers;
1033 * Finish :mod:`sys` module initialization (ex: create :data:`sys.stdout`
1034 and :data:`sys.path`);
1035 * Enable optional features like :mod:`faulthandler` and :mod:`tracemalloc`;
1036 * Import the :mod:`site` module;
1037 * etc.
1038
1039Private provisional API:
1040
1041* :c:member:`PyConfig._init_main`: if set to 0,
1042 :c:func:`Py_InitializeFromConfig` stops at the "Core" initialization phase.
1043
1044.. c:function:: PyStatus _Py_InitializeMain(void)
1045
1046 Move to the "Main" initialization phase, finish the Python initialization.
1047
1048No module is imported during the "Core" phase and the ``importlib`` module is
1049not configured: the :ref:`Path Configuration <init-path-config>` is only
1050applied during the "Main" phase. It may allow to customize Python in Python to
1051override or tune the :ref:`Path Configuration <init-path-config>`, maybe
Victor Stinner88feaec2019-09-26 03:15:07 +02001052install a custom :data:`sys.meta_path` importer or an import hook, etc.
Victor Stinner331a6a52019-05-27 16:39:22 +02001053
Victor Stinner88feaec2019-09-26 03:15:07 +02001054It may become possible to calculatin the :ref:`Path Configuration
Victor Stinner331a6a52019-05-27 16:39:22 +02001055<init-path-config>` in Python, after the Core phase and before the Main phase,
1056which is one of the :pep:`432` motivation.
1057
1058The "Core" phase is not properly defined: what should be and what should
1059not be available at this phase is not specified yet. The API is marked
1060as private and provisional: the API can be modified or even be removed
1061anytime until a proper public API is designed.
1062
1063Example running Python code between "Core" and "Main" initialization
1064phases::
1065
1066 void init_python(void)
1067 {
1068 PyStatus status;
1069 PyConfig config;
Victor Stinner441b10c2019-09-28 04:28:35 +02001070 config.struct_size = sizeof(PyConfig);
Victor Stinner331a6a52019-05-27 16:39:22 +02001071
1072 status = PyConfig_InitPythonConfig(&config);
1073 if (PyStatus_Exception(status)) {
1074 PyConfig_Clear(&config);
1075 Py_ExitStatusException(status);
1076 }
1077
1078 config._init_main = 0;
1079
1080 /* ... customize 'config' configuration ... */
1081
1082 status = Py_InitializeFromConfig(&config);
1083 PyConfig_Clear(&config);
1084 if (PyStatus_Exception(status)) {
1085 Py_ExitStatusException(status);
1086 }
1087
1088 /* Use sys.stderr because sys.stdout is only created
1089 by _Py_InitializeMain() */
1090 int res = PyRun_SimpleString(
1091 "import sys; "
1092 "print('Run Python code before _Py_InitializeMain', "
1093 "file=sys.stderr)");
1094 if (res < 0) {
1095 exit(1);
1096 }
1097
1098 /* ... put more configuration code here ... */
1099
1100 status = _Py_InitializeMain();
1101 if (PyStatus_Exception(status)) {
1102 Py_ExitStatusException(status);
1103 }
1104 }