CINXE.COM

PEP 587 – Python Initialization Configuration | peps.python.org

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="color-scheme" content="light dark"> <title>PEP 587 – Python Initialization Configuration | peps.python.org</title> <link rel="shortcut icon" href="../_static/py.png"> <link rel="canonical" href="https://peps.python.org/pep-0587/"> <link rel="stylesheet" href="../_static/style.css" type="text/css"> <link rel="stylesheet" href="../_static/mq.css" type="text/css"> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" media="(prefers-color-scheme: light)" id="pyg-light"> <link rel="stylesheet" href="../_static/pygments_dark.css" type="text/css" media="(prefers-color-scheme: dark)" id="pyg-dark"> <link rel="alternate" type="application/rss+xml" title="Latest PEPs" href="https://peps.python.org/peps.rss"> <meta property="og:title" content='PEP 587 – Python Initialization Configuration | peps.python.org'> <meta property="og:description" content="Add a new C API to configure the Python Initialization providing finer control on the whole configuration and better error reporting."> <meta property="og:type" content="website"> <meta property="og:url" content="https://peps.python.org/pep-0587/"> <meta property="og:site_name" content="Python Enhancement Proposals (PEPs)"> <meta property="og:image" content="https://peps.python.org/_static/og-image.png"> <meta property="og:image:alt" content="Python PEPs"> <meta property="og:image:width" content="200"> <meta property="og:image:height" content="200"> <meta name="description" content="Add a new C API to configure the Python Initialization providing finer control on the whole configuration and better error reporting."> <meta name="theme-color" content="#3776ab"> </head> <body> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <symbol id="svg-sun-half" viewBox="0 0 24 24" pointer-events="all"> <title>Following system colour scheme</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <circle cx="12" cy="12" r="9"></circle> <path d="M12 3v18m0-12l4.65-4.65M12 14.3l7.37-7.37M12 19.6l8.85-8.85"></path> </svg> </symbol> <symbol id="svg-moon" viewBox="0 0 24 24" pointer-events="all"> <title>Selected dark colour scheme</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none"></path> <path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z"></path> </svg> </symbol> <symbol id="svg-sun" viewBox="0 0 24 24" pointer-events="all"> <title>Selected light colour scheme</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <circle cx="12" cy="12" r="5"></circle> <line x1="12" y1="1" x2="12" y2="3"></line> <line x1="12" y1="21" x2="12" y2="23"></line> <line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line> <line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line> <line x1="1" y1="12" x2="3" y2="12"></line> <line x1="21" y1="12" x2="23" y2="12"></line> <line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line> <line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line> </svg> </symbol> </svg> <script> document.documentElement.dataset.colour_scheme = localStorage.getItem("colour_scheme") || "auto" </script> <section id="pep-page-section"> <header> <h1>Python Enhancement Proposals</h1> <ul class="breadcrumbs"> <li><a href="https://www.python.org/" title="The Python Programming Language">Python</a> &raquo; </li> <li><a href="../pep-0000/">PEP Index</a> &raquo; </li> <li>PEP 587</li> </ul> <button id="colour-scheme-cycler" onClick="setColourScheme(nextColourScheme())"> <svg aria-hidden="true" class="colour-scheme-icon-when-auto"><use href="#svg-sun-half"></use></svg> <svg aria-hidden="true" class="colour-scheme-icon-when-dark"><use href="#svg-moon"></use></svg> <svg aria-hidden="true" class="colour-scheme-icon-when-light"><use href="#svg-sun"></use></svg> <span class="visually-hidden">Toggle light / dark / auto colour theme</span> </button> </header> <article> <section id="pep-content"> <h1 class="page-title">PEP 587 – Python Initialization Configuration</h1> <dl class="rfc2822 field-list simple"> <dt class="field-odd">Author<span class="colon">:</span></dt> <dd class="field-odd">Victor Stinner &lt;vstinner&#32;&#97;t&#32;python.org&gt;, Alyssa Coghlan &lt;ncoghlan&#32;&#97;t&#32;gmail.com&gt;</dd> <dt class="field-even">BDFL-Delegate<span class="colon">:</span></dt> <dd class="field-even">Thomas Wouters &lt;thomas&#32;&#97;t&#32;python.org&gt;</dd> <dt class="field-odd">Discussions-To<span class="colon">:</span></dt> <dd class="field-odd"><a class="reference external" href="https://mail.python.org/archives/list/python-dev&#64;python.org/">Python-Dev list</a></dd> <dt class="field-even">Status<span class="colon">:</span></dt> <dd class="field-even"><abbr title="Accepted and implementation complete, or no longer active">Final</abbr></dd> <dt class="field-odd">Type<span class="colon">:</span></dt> <dd class="field-odd"><abbr title="Normative PEP with a new feature for Python, implementation change for CPython or interoperability standard for the ecosystem">Standards Track</abbr></dd> <dt class="field-even">Created<span class="colon">:</span></dt> <dd class="field-even">27-Mar-2019</dd> <dt class="field-odd">Python-Version<span class="colon">:</span></dt> <dd class="field-odd">3.8</dd> </dl> <hr class="docutils" /> <section id="contents"> <details><summary>Table of Contents</summary><ul class="simple"> <li><a class="reference internal" href="#abstract">Abstract</a></li> <li><a class="reference internal" href="#rationale">Rationale</a></li> <li><a class="reference internal" href="#python-initialization-c-api">Python Initialization C API</a><ul> <li><a class="reference internal" href="#pywidestringlist">PyWideStringList</a></li> <li><a class="reference internal" href="#pystatus">PyStatus</a></li> <li><a class="reference internal" href="#preinitialization-with-pypreconfig">Preinitialization with PyPreConfig</a></li> <li><a class="reference internal" href="#initialization-with-pyconfig">Initialization with PyConfig</a></li> <li><a class="reference internal" href="#isolated-configuration">Isolated Configuration</a></li> <li><a class="reference internal" href="#python-configuration">Python Configuration</a></li> <li><a class="reference internal" href="#path-configuration">Path Configuration</a></li> <li><a class="reference internal" href="#py-bytesmain">Py_BytesMain()</a></li> <li><a class="reference internal" href="#py-runmain">Py_RunMain()</a></li> <li><a class="reference internal" href="#multi-phase-initialization-private-provisional-api">Multi-Phase Initialization Private Provisional API</a></li> </ul> </li> <li><a class="reference internal" href="#backwards-compatibility">Backwards Compatibility</a></li> <li><a class="reference internal" href="#annexes">Annexes</a><ul> <li><a class="reference internal" href="#comparison-of-python-and-isolated-configurations">Comparison of Python and Isolated Configurations</a></li> <li><a class="reference internal" href="#priority-and-rules">Priority and Rules</a></li> <li><a class="reference internal" href="#configuration-files">Configuration Files</a></li> <li><a class="reference internal" href="#global-configuration-variables">Global Configuration Variables</a></li> <li><a class="reference internal" href="#command-line-arguments">Command Line Arguments</a></li> <li><a class="reference internal" href="#x-options">-X Options</a></li> <li><a class="reference internal" href="#environment-variables">Environment Variables</a></li> <li><a class="reference internal" href="#default-python-configuration">Default Python Configuration</a></li> <li><a class="reference internal" href="#default-isolated-configuration">Default Isolated Configuration</a></li> <li><a class="reference internal" href="#python-3-7-api">Python 3.7 API</a></li> </ul> </li> <li><a class="reference internal" href="#python-issues">Python Issues</a></li> <li><a class="reference internal" href="#discussions">Discussions</a></li> <li><a class="reference internal" href="#version-history">Version History</a></li> <li><a class="reference internal" href="#acceptance">Acceptance</a></li> <li><a class="reference internal" href="#copyright">Copyright</a></li> </ul> </details></section> <section id="abstract"> <h2><a class="toc-backref" href="#abstract" role="doc-backlink">Abstract</a></h2> <p>Add a new C API to configure the Python Initialization providing finer control on the whole configuration and better error reporting.</p> <p>It becomes possible to read the configuration and then override some computed parameters before it is applied. It also becomes possible to completely override how Python computes the module search paths (<code class="docutils literal notranslate"><span class="pre">sys.path</span></code>).</p> <p>The new <a class="reference internal" href="#isolated-configuration">Isolated Configuration</a> provides sane default values to isolate Python from the system. For example, to embed Python into an application. Using the environment are now opt-in options, rather than an opt-out options. For example, environment variables, command line arguments and global configuration variables are ignored by default.</p> <p>Building a customized Python which behaves as the regular Python becomes easier using the new <code class="docutils literal notranslate"><span class="pre">Py_RunMain()</span></code> function. Moreover, using the <a class="reference internal" href="#python-configuration">Python Configuration</a>, <code class="docutils literal notranslate"><span class="pre">PyConfig.argv</span></code> arguments are now parsed the same way the regular Python parses command line arguments, and <code class="docutils literal notranslate"><span class="pre">PyConfig.xoptions</span></code> are handled as <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">opt</span></code> command line options.</p> <p>This extracts a subset of the API design from the <a class="pep reference internal" href="../pep-0432/" title="PEP 432 – Restructuring the CPython startup sequence">PEP 432</a> development and refactoring work that is now considered sufficiently stable to make public (allowing 3rd party embedding applications access to the same configuration APIs that the native CPython CLI is now using).</p> </section> <section id="rationale"> <h2><a class="toc-backref" href="#rationale" role="doc-backlink">Rationale</a></h2> <p>Python is highly configurable but its configuration evolved organically. The initialization configuration is scattered all around the code using different ways to set them: global configuration variables (ex: <code class="docutils literal notranslate"><span class="pre">Py_IsolatedFlag</span></code>), environment variables (ex: <code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code>), command line arguments (ex: <code class="docutils literal notranslate"><span class="pre">-b</span></code>), configuration files (ex: <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code>), function calls (ex: <code class="docutils literal notranslate"><span class="pre">Py_SetProgramName()</span></code>). A straightforward and reliable way to configure Python is needed.</p> <p>Some configuration parameters are not accessible from the C API, or not easily. For example, there is no API to override the default values of <code class="docutils literal notranslate"><span class="pre">sys.executable</span></code>.</p> <p>Some options like <code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code> can only be set using an environment variable which has a side effect on Python child processes if not unset properly.</p> <p>Some options also depends on other options: see <a class="reference internal" href="#priority-and-rules">Priority and Rules</a>. Python 3.7 API does not provide a consistent view of the overall configuration.</p> <p>The C API of Python 3.7 Initialization takes <code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code> strings as input whereas the Python filesystem encoding is set during the initialization which can lead to mojibake.</p> <p>Python 3.7 APIs like <code class="docutils literal notranslate"><span class="pre">Py_Initialize()</span></code> aborts the process on memory allocation failure which is not convenient when Python is embedded. Moreover, <code class="docutils literal notranslate"><span class="pre">Py_Main()</span></code> could exit directly the process rather than returning an exit code. Proposed new API reports the error or exit code to the caller which can decide how to handle it.</p> <p>Implementing the <a class="pep reference internal" href="../pep-0540/" title="PEP 540 – Add a new UTF-8 Mode">PEP 540</a> (UTF-8 Mode) and the new <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">dev</span></code> correctly was almost impossible in Python 3.6. The code base has been deeply reworked in Python 3.7 and then in Python 3.8 to read the configuration into a structure with no side effect. It becomes possible to clear the configuration (release memory) and read again the configuration if the encoding changed . It is required to implement properly the UTF-8 which changes the encoding using <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code> command line option. Internally, bytes <code class="docutils literal notranslate"><span class="pre">argv</span></code> strings are decoded from the filesystem encoding. The <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">dev</span></code> changes the memory allocator (behaves as <code class="docutils literal notranslate"><span class="pre">PYTHONMALLOC=debug</span></code>), whereas it was not possible to change the memory allocation <em>while</em> parsing the command line arguments. The new design of the internal implementation not only allowed to implement properly <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code> and <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">dev</span></code>, it also allows to change the Python behavior way more easily, especially for corner cases like that, and ensure that the configuration remains consistent: see <a class="reference internal" href="#priority-and-rules">Priority and Rules</a>.</p> <p>This PEP is a partial implementation of <a class="pep reference internal" href="../pep-0432/" title="PEP 432 – Restructuring the CPython startup sequence">PEP 432</a> which is the overall design. New fields can be added later to <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> structure to finish the implementation of the <a class="pep reference internal" href="../pep-0432/" title="PEP 432 – Restructuring the CPython startup sequence">PEP 432</a> (e.g. by adding a new partial initialization API which allows to configure Python using Python objects to finish the full initialization). However, those features are omitted from this PEP as even the native CPython CLI doesn’t work that way - the public API proposal in this PEP is limited to features which have already been implemented and adopted as private APIs for us in the native CPython CLI.</p> </section> <section id="python-initialization-c-api"> <h2><a class="toc-backref" href="#python-initialization-c-api" role="doc-backlink">Python Initialization C API</a></h2> <p>This PEP proposes to add the following new structures and functions.</p> <p>New structures:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PyConfig</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyWideStringList</span></code></li> </ul> <p>New functions:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PyConfig_Clear(config)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyConfig_InitIsolatedConfig()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyConfig_InitPythonConfig()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyConfig_Read(config)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyConfig_SetArgv(config,</span> <span class="pre">argc,</span> <span class="pre">argv)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyConfig_SetBytesArgv(config,</span> <span class="pre">argc,</span> <span class="pre">argv)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyConfig_SetBytesString(config,</span> <span class="pre">config_str,</span> <span class="pre">str)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyConfig_SetString(config,</span> <span class="pre">config_str,</span> <span class="pre">str)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyPreConfig_InitIsolatedConfig(preconfig)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyPreConfig_InitPythonConfig(preconfig)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_Error(err_msg)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_Exception(status)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_Exit(exitcode)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_IsError(status)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_IsExit(status)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_NoMemory()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_Ok()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyWideStringList_Append(list,</span> <span class="pre">item)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyWideStringList_Insert(list,</span> <span class="pre">index,</span> <span class="pre">item)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">Py_BytesMain(argc,</span> <span class="pre">argv)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">Py_ExitStatusException(status)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">Py_InitializeFromConfig(config)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">Py_PreInitialize(preconfig)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">Py_PreInitializeFromArgs(preconfig,</span> <span class="pre">argc,</span> <span class="pre">argv)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">Py_PreInitializeFromBytesArgs(preconfig,</span> <span class="pre">argc,</span> <span class="pre">argv)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">Py_RunMain()</span></code></li> </ul> <p>This PEP also adds <code class="docutils literal notranslate"><span class="pre">_PyRuntimeState.preconfig</span></code> (<code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> type) and <code class="docutils literal notranslate"><span class="pre">PyInterpreterState.config</span></code> (<code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> type) fields to these internal structures. <code class="docutils literal notranslate"><span class="pre">PyInterpreterState.config</span></code> becomes the new reference configuration, replacing global configuration variables and other private variables.</p> <section id="pywidestringlist"> <h3><a class="toc-backref" href="#pywidestringlist" role="doc-backlink">PyWideStringList</a></h3> <p><code class="docutils literal notranslate"><span class="pre">PyWideStringList</span></code> is a list of <code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code> strings.</p> <p><code class="docutils literal notranslate"><span class="pre">PyWideStringList</span></code> structure fields:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">length</span></code> (<code class="docutils literal notranslate"><span class="pre">Py_ssize_t</span></code>)</li> <li><code class="docutils literal notranslate"><span class="pre">items</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t**</span></code>)</li> </ul> <p>Methods:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">PyWideStringList_Append(PyWideStringList</span> <span class="pre">*list,</span> <span class="pre">const</span> <span class="pre">wchar_t</span> <span class="pre">*item)</span></code>: Append <em>item</em> to <em>list</em>.</li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">PyWideStringList_Insert(PyWideStringList</span> <span class="pre">*list,</span> <span class="pre">Py_ssize_t</span> <span class="pre">index,</span> <span class="pre">const</span> <span class="pre">wchar_t</span> <span class="pre">*item)</span></code>: Insert <em>item</em> into <em>list</em> at <em>index</em>. If <em>index</em> is greater than <em>list</em> length, just append <em>item</em> to <em>list</em>.</li> </ul> <p>If <em>length</em> is non-zero, <em>items</em> must be non-NULL and all strings must be non-NULL.</p> </section> <section id="pystatus"> <h3><a class="toc-backref" href="#pystatus" role="doc-backlink">PyStatus</a></h3> <p><code class="docutils literal notranslate"><span class="pre">PyStatus</span></code> is a structure to store the status of an initialization function: success, error or exit. For an error, it can store the C function name which created the error.</p> <p>Example:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">PyStatus</span> <span class="n">alloc</span><span class="p">(</span><span class="n">void</span> <span class="o">**</span><span class="n">ptr</span><span class="p">,</span> <span class="n">size_t</span> <span class="n">size</span><span class="p">)</span> <span class="p">{</span> <span class="o">*</span><span class="n">ptr</span> <span class="o">=</span> <span class="n">PyMem_RawMalloc</span><span class="p">(</span><span class="n">size</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="o">*</span><span class="n">ptr</span> <span class="o">==</span> <span class="n">NULL</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">PyStatus_NoMemory</span><span class="p">();</span> <span class="p">}</span> <span class="k">return</span> <span class="n">PyStatus_Ok</span><span class="p">();</span> <span class="p">}</span> <span class="nb">int</span> <span class="n">main</span><span class="p">(</span><span class="nb">int</span> <span class="n">argc</span><span class="p">,</span> <span class="n">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span> <span class="p">{</span> <span class="n">void</span> <span class="o">*</span><span class="n">ptr</span><span class="p">;</span> <span class="n">PyStatus</span> <span class="n">status</span> <span class="o">=</span> <span class="n">alloc</span><span class="p">(</span><span class="o">&amp;</span><span class="n">ptr</span><span class="p">,</span> <span class="mi">16</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="n">Py_ExitStatusException</span><span class="p">(</span><span class="n">status</span><span class="p">);</span> <span class="p">}</span> <span class="n">PyMem_Free</span><span class="p">(</span><span class="n">ptr</span><span class="p">);</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <p><code class="docutils literal notranslate"><span class="pre">PyStatus</span></code> fields:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">exitcode</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Argument passed to <code class="docutils literal notranslate"><span class="pre">exit()</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">err_msg</span></code> (<code class="docutils literal notranslate"><span class="pre">const</span> <span class="pre">char*</span></code>): Error message.</li> <li><code class="docutils literal notranslate"><span class="pre">func</span></code> (<code class="docutils literal notranslate"><span class="pre">const</span> <span class="pre">char</span> <span class="pre">*</span></code>): Name of the function which created an error, can be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</li> <li>private <code class="docutils literal notranslate"><span class="pre">_type</span></code> field: for internal usage only.</li> </ul> <p>Functions to create a status:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_Ok()</span></code>: Success.</li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_Error(err_msg)</span></code>: Initialization error with a message.</li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_NoMemory()</span></code>: Memory allocation failure (out of memory).</li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_Exit(exitcode)</span></code>: Exit Python with the specified exit code.</li> </ul> <p>Functions to handle a status:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_Exception(status)</span></code>: Is the result an error or an exit? If true, the exception must be handled; by calling <code class="docutils literal notranslate"><span class="pre">Py_ExitStatusException(status)</span></code> for example.</li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_IsError(status)</span></code>: Is the result an error?</li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus_IsExit(status)</span></code>: Is the result an exit?</li> <li><code class="docutils literal notranslate"><span class="pre">Py_ExitStatusException(status)</span></code>: Call <code class="docutils literal notranslate"><span class="pre">exit(exitcode)</span></code> if <em>status</em> is an exit. Print the error messageand exit with a non-zero exit code if <em>status</em> is an error. Must only be called if <code class="docutils literal notranslate"><span class="pre">PyStatus_Exception(status)</span></code> is true.</li> </ul> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Internally, Python uses macros which set <code class="docutils literal notranslate"><span class="pre">PyStatus.func</span></code>, whereas functions to create a status set <code class="docutils literal notranslate"><span class="pre">func</span></code> to <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p> </div> </section> <section id="preinitialization-with-pypreconfig"> <h3><a class="toc-backref" href="#preinitialization-with-pypreconfig" role="doc-backlink">Preinitialization with PyPreConfig</a></h3> <p>The <code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> structure is used to preinitialize Python:</p> <ul class="simple"> <li>Set the Python memory allocator</li> <li>Configure the LC_CTYPE locale</li> <li>Set the UTF-8 mode</li> </ul> <p>Example using the preinitialization to enable the UTF-8 Mode:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">PyStatus</span> <span class="n">status</span><span class="p">;</span> <span class="n">PyPreConfig</span> <span class="n">preconfig</span><span class="p">;</span> <span class="n">PyPreConfig_InitPythonConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">preconfig</span><span class="p">);</span> <span class="n">preconfig</span><span class="o">.</span><span class="n">utf8_mode</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">status</span> <span class="o">=</span> <span class="n">Py_PreInitialize</span><span class="p">(</span><span class="o">&amp;</span><span class="n">preconfig</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="n">Py_ExitStatusException</span><span class="p">(</span><span class="n">status</span><span class="p">);</span> <span class="p">}</span> <span class="o">/*</span> <span class="n">at</span> <span class="n">this</span> <span class="n">point</span><span class="p">,</span> <span class="n">Python</span> <span class="n">will</span> <span class="n">speak</span> <span class="n">UTF</span><span class="o">-</span><span class="mi">8</span> <span class="o">*/</span> <span class="n">Py_Initialize</span><span class="p">();</span> <span class="o">/*</span> <span class="o">...</span> <span class="n">use</span> <span class="n">Python</span> <span class="n">API</span> <span class="n">here</span> <span class="o">...</span> <span class="o">*/</span> <span class="n">Py_Finalize</span><span class="p">();</span> </pre></div> </div> <p>Function to initialize a preconfiguration:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">PyPreConfig_InitIsolatedConfig(PyPreConfig</span> <span class="pre">*preconfig)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">PyPreConfig_InitPythonConfig(PyPreConfig</span> <span class="pre">*preconfig)</span></code></li> </ul> <p>Functions to preinitialize Python:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">Py_PreInitialize(const</span> <span class="pre">PyPreConfig</span> <span class="pre">*preconfig)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">Py_PreInitializeFromBytesArgs(const</span> <span class="pre">PyPreConfig</span> <span class="pre">*preconfig,</span> <span class="pre">int</span> <span class="pre">argc,</span> <span class="pre">char</span> <span class="pre">*</span> <span class="pre">const</span> <span class="pre">*argv)</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">Py_PreInitializeFromArgs(const</span> <span class="pre">PyPreConfig</span> <span class="pre">*preconfig,</span> <span class="pre">int</span> <span class="pre">argc,</span> <span class="pre">wchar_t</span> <span class="pre">*</span> <span class="pre">const</span> <span class="pre">*</span> <span class="pre">argv)</span></code></li> </ul> <p>The caller is responsible to handle exceptions (error or exit) using <code class="docutils literal notranslate"><span class="pre">PyStatus_Exception()</span></code> and <code class="docutils literal notranslate"><span class="pre">Py_ExitStatusException()</span></code>.</p> <p>For <a class="reference internal" href="#python-configuration">Python Configuration</a> (<code class="docutils literal notranslate"><span class="pre">PyPreConfig_InitPythonConfig()</span></code>), if Python is initialized with command line arguments, the command line arguments must also be passed to preinitialize Python, since they have an effect on the pre-configuration like encodings. For example, the <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code> command line option enables the UTF-8 Mode.</p> <p><code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> fields:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">allocator</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Name of the memory allocator (ex: <code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_MALLOC</span></code>). Valid values:<ul> <li><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_NOT_SET</span></code> (<code class="docutils literal notranslate"><span class="pre">0</span></code>): don’t change memory allocators (use defaults)</li> <li><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_DEFAULT</span></code> (<code class="docutils literal notranslate"><span class="pre">1</span></code>): default memory allocators</li> <li><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_DEBUG</span></code> (<code class="docutils literal notranslate"><span class="pre">2</span></code>): default memory allocators with debug hooks</li> <li><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_MALLOC</span></code> (<code class="docutils literal notranslate"><span class="pre">3</span></code>): force usage of <code class="docutils literal notranslate"><span class="pre">malloc()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_MALLOC_DEBUG</span></code> (<code class="docutils literal notranslate"><span class="pre">4</span></code>): force usage of <code class="docutils literal notranslate"><span class="pre">malloc()</span></code> with debug hooks</li> <li><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_PYMALLOC</span></code> (<code class="docutils literal notranslate"><span class="pre">5</span></code>): Python “pymalloc” allocator</li> <li><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_PYMALLOC_DEBUG</span></code> (<code class="docutils literal notranslate"><span class="pre">6</span></code>): Python “pymalloc” allocator with debug hooks</li> <li>Note: <code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_PYMALLOC</span></code> and <code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_PYMALLOC_DEBUG</span></code> are not supported if Python is configured using <code class="docutils literal notranslate"><span class="pre">--without-pymalloc</span></code></li> </ul> </li> <li><code class="docutils literal notranslate"><span class="pre">configure_locale</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Set the LC_CTYPE locale to the user preferred locale? If equals to 0, set <code class="docutils literal notranslate"><span class="pre">coerce_c_locale</span></code> and <code class="docutils literal notranslate"><span class="pre">coerce_c_locale_warn</span></code> to 0.</li> <li><code class="docutils literal notranslate"><span class="pre">coerce_c_locale</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If equals to 2, coerce the C locale; if equals to 1, read the LC_CTYPE locale to decide if it should be coerced.</li> <li><code class="docutils literal notranslate"><span class="pre">coerce_c_locale_warn</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, emit a warning if the C locale is coerced.</li> <li><code class="docutils literal notranslate"><span class="pre">dev_mode</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): See <code class="docutils literal notranslate"><span class="pre">PyConfig.dev_mode</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">isolated</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): See <code class="docutils literal notranslate"><span class="pre">PyConfig.isolated</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">legacy_windows_fs_encoding</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>, Windows only): If non-zero, disable UTF-8 Mode, set the Python filesystem encoding to <code class="docutils literal notranslate"><span class="pre">mbcs</span></code>, set the filesystem error handler to <code class="docutils literal notranslate"><span class="pre">replace</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">parse_argv</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, <code class="docutils literal notranslate"><span class="pre">Py_PreInitializeFromArgs()</span></code> and <code class="docutils literal notranslate"><span class="pre">Py_PreInitializeFromBytesArgs()</span></code> parse their <code class="docutils literal notranslate"><span class="pre">argv</span></code> argument the same way the regular Python parses command line arguments: see <a class="reference internal" href="#command-line-arguments">Command Line Arguments</a>.</li> <li><code class="docutils literal notranslate"><span class="pre">use_environment</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): See <code class="docutils literal notranslate"><span class="pre">PyConfig.use_environment</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">utf8_mode</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, enable the UTF-8 mode.</li> </ul> <p>The <code class="docutils literal notranslate"><span class="pre">legacy_windows_fs_encoding</span></code> field is only available on Windows. <code class="docutils literal notranslate"><span class="pre">#ifdef</span> <span class="pre">MS_WINDOWS</span></code> macro can be used for Windows specific code.</p> <p><code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> private fields, for internal use only:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">_config_init</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Function used to initialize <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code>, used for preinitialization.</li> </ul> <p><code class="docutils literal notranslate"><span class="pre">PyMem_SetAllocator()</span></code> can be called after <code class="docutils literal notranslate"><span class="pre">Py_PreInitialize()</span></code> and before <code class="docutils literal notranslate"><span class="pre">Py_InitializeFromConfig()</span></code> to install a custom memory allocator. It can be called before <code class="docutils literal notranslate"><span class="pre">Py_PreInitialize()</span></code> if <code class="docutils literal notranslate"><span class="pre">allocator</span></code> is set to <code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_NOT_SET</span></code> (default value).</p> <p>Python memory allocation functions like <code class="docutils literal notranslate"><span class="pre">PyMem_RawMalloc()</span></code> must not be used before Python preinitialization, whereas calling directly <code class="docutils literal notranslate"><span class="pre">malloc()</span></code> and <code class="docutils literal notranslate"><span class="pre">free()</span></code> is always safe. <code class="docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code> must not be called before the preinitialization.</p> </section> <section id="initialization-with-pyconfig"> <h3><a class="toc-backref" href="#initialization-with-pyconfig" role="doc-backlink">Initialization with PyConfig</a></h3> <p>The <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> structure contains most parameters to configure Python.</p> <p>Example setting the program name:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">void</span> <span class="n">init_python</span><span class="p">(</span><span class="n">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyStatus</span> <span class="n">status</span><span class="p">;</span> <span class="n">PyConfig</span> <span class="n">config</span><span class="p">;</span> <span class="n">PyConfig_InitPythonConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="o">/*</span> <span class="n">Set</span> <span class="n">the</span> <span class="n">program</span> <span class="n">name</span><span class="o">.</span> <span class="n">Implicitly</span> <span class="n">preinitialize</span> <span class="n">Python</span><span class="o">.</span> <span class="o">*/</span> <span class="n">status</span> <span class="o">=</span> <span class="n">PyConfig_SetString</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">config</span><span class="o">.</span><span class="n">program_name</span><span class="p">,</span> <span class="n">L</span><span class="s2">&quot;/path/to/my_program&quot;</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="n">goto</span> <span class="n">fail</span><span class="p">;</span> <span class="p">}</span> <span class="n">status</span> <span class="o">=</span> <span class="n">Py_InitializeFromConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="n">goto</span> <span class="n">fail</span><span class="p">;</span> <span class="p">}</span> <span class="n">PyConfig_Clear</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="k">return</span><span class="p">;</span> <span class="n">fail</span><span class="p">:</span> <span class="n">PyConfig_Clear</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="n">Py_ExitStatusException</span><span class="p">(</span><span class="n">status</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p><code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> methods:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">void</span> <span class="pre">PyConfig_InitPythonConfig(PyConfig</span> <span class="pre">*config)</span></code> Initialize configuration with <a class="reference internal" href="#python-configuration">Python Configuration</a>.</li> <li><code class="docutils literal notranslate"><span class="pre">void</span> <span class="pre">PyConfig_InitIsolatedConfig(PyConfig</span> <span class="pre">*config)</span></code>: Initialize configuration with <a class="reference internal" href="#isolated-configuration">Isolated Configuration</a>.</li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">PyConfig_SetString(PyConfig</span> <span class="pre">*config,</span> <span class="pre">wchar_t</span> <span class="pre">*</span> <span class="pre">const</span> <span class="pre">*config_str,</span> <span class="pre">const</span> <span class="pre">wchar_t</span> <span class="pre">*str)</span></code>: Copy the wide character string <em>str</em> into <code class="docutils literal notranslate"><span class="pre">*config_str</span></code>. Preinitialize Python if needed.</li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">PyConfig_SetBytesString(PyConfig</span> <span class="pre">*config,</span> <span class="pre">wchar_t</span> <span class="pre">*</span> <span class="pre">const</span> <span class="pre">*config_str,</span> <span class="pre">const</span> <span class="pre">char</span> <span class="pre">*str)</span></code>: Decode <em>str</em> using <code class="docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code> and set the result into <code class="docutils literal notranslate"><span class="pre">*config_str</span></code>. Preinitialize Python if needed.</li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">PyConfig_SetArgv(PyConfig</span> <span class="pre">*config,</span> <span class="pre">int</span> <span class="pre">argc,</span> <span class="pre">wchar_t</span> <span class="pre">*</span> <span class="pre">const</span> <span class="pre">*argv)</span></code>: Set command line arguments from wide character strings. Preinitialize Python if needed.</li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">PyConfig_SetBytesArgv(PyConfig</span> <span class="pre">*config,</span> <span class="pre">int</span> <span class="pre">argc,</span> <span class="pre">char</span> <span class="pre">*</span> <span class="pre">const</span> <span class="pre">*argv)</span></code>: Set command line arguments: decode bytes using <code class="docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code>. Preinitialize Python if needed.</li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">PyConfig_Read(PyConfig</span> <span class="pre">*config)</span></code>: Read all Python configuration. Fields which are already initialized are left unchanged. Preinitialize Python if needed.</li> <li><code class="docutils literal notranslate"><span class="pre">void</span> <span class="pre">PyConfig_Clear(PyConfig</span> <span class="pre">*config)</span></code>: Release configuration memory.</li> </ul> <p>Most <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> methods preinitialize Python if needed. In that case, the Python preinitialization configuration in based on the <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code>. If configuration fields which are in common with <code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> are tuned, they must be set before calling a <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> method:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">dev_mode</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">isolated</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">parse_argv</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">use_environment</span></code></li> </ul> <p>Moreover, if <code class="docutils literal notranslate"><span class="pre">PyConfig_SetArgv()</span></code> or <code class="docutils literal notranslate"><span class="pre">PyConfig_SetBytesArgv()</span></code> is used, this method must be called first, before other methods, since the preinitialization configuration depends on command line arguments (if <code class="docutils literal notranslate"><span class="pre">parse_argv</span></code> is non-zero).</p> <p>Functions to initialize Python:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">Py_InitializeFromConfig(const</span> <span class="pre">PyConfig</span> <span class="pre">*config)</span></code>: Initialize Python from <em>config</em> configuration.</li> </ul> <p>The caller of these methods and functions is responsible to handle exceptions (error or exit) using <code class="docutils literal notranslate"><span class="pre">PyStatus_Exception()</span></code> and <code class="docutils literal notranslate"><span class="pre">Py_ExitStatusException()</span></code>.</p> <p><code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> fields:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">argv</span></code> (<code class="docutils literal notranslate"><span class="pre">PyWideStringList</span></code>): Command line arguments, <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code>. See <code class="docutils literal notranslate"><span class="pre">parse_argv</span></code> to parse <code class="docutils literal notranslate"><span class="pre">argv</span></code> the same way the regular Python parses Python command line arguments. If <code class="docutils literal notranslate"><span class="pre">argv</span></code> is empty, an empty string is added to ensure that <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code> always exists and is never empty.</li> <li><code class="docutils literal notranslate"><span class="pre">base_exec_prefix</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): <code class="docutils literal notranslate"><span class="pre">sys.base_exec_prefix</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">base_prefix</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): <code class="docutils literal notranslate"><span class="pre">sys.base_prefix</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">buffered_stdio</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If equals to 0, enable unbuffered mode, making the stdout and stderr streams unbuffered.</li> <li><code class="docutils literal notranslate"><span class="pre">bytes_warning</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If equals to 1, issue a warning when comparing <code class="docutils literal notranslate"><span class="pre">bytes</span></code> or <code class="docutils literal notranslate"><span class="pre">bytearray</span></code> with <code class="docutils literal notranslate"><span class="pre">str</span></code>, or comparing <code class="docutils literal notranslate"><span class="pre">bytes</span></code> with <code class="docutils literal notranslate"><span class="pre">int</span></code>. If equal or greater to 2, raise a <code class="docutils literal notranslate"><span class="pre">BytesWarning</span></code> exception.</li> <li><code class="docutils literal notranslate"><span class="pre">check_hash_pycs_mode</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): <code class="docutils literal notranslate"><span class="pre">--check-hash-based-pycs</span></code> command line option value (see <a class="pep reference internal" href="../pep-0552/" title="PEP 552 – Deterministic pycs">PEP 552</a>). Valid values: <code class="docutils literal notranslate"><span class="pre">always</span></code>, <code class="docutils literal notranslate"><span class="pre">never</span></code> and <code class="docutils literal notranslate"><span class="pre">default</span></code>. The default value is <code class="docutils literal notranslate"><span class="pre">default</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">configure_c_stdio</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, configure C standard streams (<code class="docutils literal notranslate"><span class="pre">stdio</span></code>, <code class="docutils literal notranslate"><span class="pre">stdout</span></code>, <code class="docutils literal notranslate"><span class="pre">stdout</span></code>). For example, set their mode to <code class="docutils literal notranslate"><span class="pre">O_BINARY</span></code> on Windows.</li> <li><code class="docutils literal notranslate"><span class="pre">dev_mode</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Development mode</li> <li><code class="docutils literal notranslate"><span class="pre">dump_refs</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, dump all objects which are still alive at exit. Require a special Python build with <code class="docutils literal notranslate"><span class="pre">Py_REF_DEBUG</span></code> macro defined.</li> <li><code class="docutils literal notranslate"><span class="pre">exec_prefix</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): <code class="docutils literal notranslate"><span class="pre">sys.exec_prefix</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">executable</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): <code class="docutils literal notranslate"><span class="pre">sys.executable</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">faulthandler</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, call <code class="docutils literal notranslate"><span class="pre">faulthandler.enable()</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">filesystem_encoding</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): Filesystem encoding, <code class="docutils literal notranslate"><span class="pre">sys.getfilesystemencoding()</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">filesystem_errors</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): Filesystem encoding errors, <code class="docutils literal notranslate"><span class="pre">sys.getfilesystemencodeerrors()</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">use_hash_seed</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>), <code class="docutils literal notranslate"><span class="pre">hash_seed</span></code> (<code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">long</span></code>): Randomized hash function seed.</li> <li><code class="docutils literal notranslate"><span class="pre">home</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): Python home directory.</li> <li><code class="docutils literal notranslate"><span class="pre">import_time</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, profile import time.</li> <li><code class="docutils literal notranslate"><span class="pre">inspect</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Enter interactive mode after executing a script or a command.</li> <li><code class="docutils literal notranslate"><span class="pre">install_signal_handlers</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Install signal handlers?</li> <li><code class="docutils literal notranslate"><span class="pre">interactive</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Interactive mode.</li> <li><code class="docutils literal notranslate"><span class="pre">isolated</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If greater than 0, enable isolated mode:<ul> <li><code class="docutils literal notranslate"><span class="pre">sys.path</span></code> contains neither the script’s directory (computed from <code class="docutils literal notranslate"><span class="pre">argv[0]</span></code> or the current directory) nor the user’s site-packages directory.</li> <li>Python REPL doesn’t import <code class="docutils literal notranslate"><span class="pre">readline</span></code> nor enable default readline configuration on interactive prompts.</li> <li>Set <code class="docutils literal notranslate"><span class="pre">use_environment</span></code> and <code class="docutils literal notranslate"><span class="pre">user_site_directory</span></code> to 0.</li> </ul> </li> <li><code class="docutils literal notranslate"><span class="pre">legacy_windows_stdio</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>, Windows only): If non-zero, use <code class="docutils literal notranslate"><span class="pre">io.FileIO</span></code> instead of <code class="docutils literal notranslate"><span class="pre">WindowsConsoleIO</span></code> for <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>, <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">malloc_stats</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, dump statistics on <code class="docutils literal notranslate"><span class="pre">pymalloc</span></code> memory allocator at exit. The option is ignored if Python is built using <code class="docutils literal notranslate"><span class="pre">--without-pymalloc</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">pythonpath_env</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): Module search paths as a string separated by DELIM (usually <code class="docutils literal notranslate"><span class="pre">:</span></code> character). Initialized from <code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code> environment variable value by default.</li> <li><code class="docutils literal notranslate"><span class="pre">module_search_paths_set</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>), <code class="docutils literal notranslate"><span class="pre">module_search_paths</span></code> (<code class="docutils literal notranslate"><span class="pre">PyWideStringList</span></code>): <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>. If <code class="docutils literal notranslate"><span class="pre">module_search_paths_set</span></code> is equal to 0, the <code class="docutils literal notranslate"><span class="pre">module_search_paths</span></code> is overridden by the function computing the <a class="reference internal" href="#path-configuration">Path Configuration</a>.</li> <li><code class="docutils literal notranslate"><span class="pre">optimization_level</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Compilation optimization level:<ul> <li>0: Peephole optimizer (and <code class="docutils literal notranslate"><span class="pre">__debug__</span></code> is set to <code class="docutils literal notranslate"><span class="pre">True</span></code>)</li> <li>1: Remove assertions, set <code class="docutils literal notranslate"><span class="pre">__debug__</span></code> to <code class="docutils literal notranslate"><span class="pre">False</span></code></li> <li>2: Strip docstrings</li> </ul> </li> <li><code class="docutils literal notranslate"><span class="pre">parse_argv</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, parse <code class="docutils literal notranslate"><span class="pre">argv</span></code> the same way the regular Python command line arguments, and strip Python arguments from <code class="docutils literal notranslate"><span class="pre">argv</span></code>: see <a class="reference internal" href="#command-line-arguments">Command Line Arguments</a>.</li> <li><code class="docutils literal notranslate"><span class="pre">parser_debug</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, turn on parser debugging output (for expert only, depending on compilation options).</li> <li><code class="docutils literal notranslate"><span class="pre">pathconfig_warnings</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If equal to 0, suppress warnings when computing the path configuration (Unix only, Windows does not log any warning). Otherwise, warnings are written into stderr.</li> <li><code class="docutils literal notranslate"><span class="pre">prefix</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): <code class="docutils literal notranslate"><span class="pre">sys.prefix</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">program_name</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): Program name.</li> <li><code class="docutils literal notranslate"><span class="pre">pycache_prefix</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> cache prefix.</li> <li><code class="docutils literal notranslate"><span class="pre">quiet</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Quiet mode. For example, don’t display the copyright and version messages even in interactive mode.</li> <li><code class="docutils literal notranslate"><span class="pre">run_command</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): <code class="docutils literal notranslate"><span class="pre">python3</span> <span class="pre">-c</span> <span class="pre">COMMAND</span></code> argument.</li> <li><code class="docutils literal notranslate"><span class="pre">run_filename</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): <code class="docutils literal notranslate"><span class="pre">python3</span> <span class="pre">FILENAME</span></code> argument.</li> <li><code class="docutils literal notranslate"><span class="pre">run_module</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): <code class="docutils literal notranslate"><span class="pre">python3</span> <span class="pre">-m</span> <span class="pre">MODULE</span></code> argument.</li> <li><code class="docutils literal notranslate"><span class="pre">show_alloc_count</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Show allocation counts at exit? Need a special Python build with <code class="docutils literal notranslate"><span class="pre">COUNT_ALLOCS</span></code> macro defined.</li> <li><code class="docutils literal notranslate"><span class="pre">show_ref_count</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Show total reference count at exit? Need a debug build of Python (<code class="docutils literal notranslate"><span class="pre">Py_REF_DEBUG</span></code> macro should be defined).</li> <li><code class="docutils literal notranslate"><span class="pre">site_import</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Import the <code class="docutils literal notranslate"><span class="pre">site</span></code> module at startup?</li> <li><code class="docutils literal notranslate"><span class="pre">skip_source_first_line</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Skip the first line of the source?</li> <li><code class="docutils literal notranslate"><span class="pre">stdio_encoding</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>), <code class="docutils literal notranslate"><span class="pre">stdio_errors</span></code> (<code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code>): Encoding and encoding errors of <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>, <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">tracemalloc</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, call <code class="docutils literal notranslate"><span class="pre">tracemalloc.start(value)</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">user_site_directory</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, add user site directory to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">verbose</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, enable verbose mode.</li> <li><code class="docutils literal notranslate"><span class="pre">warnoptions</span></code> (<code class="docutils literal notranslate"><span class="pre">PyWideStringList</span></code>): Options of the <code class="docutils literal notranslate"><span class="pre">warnings</span></code> module to build warnings filters.</li> <li><code class="docutils literal notranslate"><span class="pre">write_bytecode</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If non-zero, write <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files.</li> <li><code class="docutils literal notranslate"><span class="pre">xoptions</span></code> (<code class="docutils literal notranslate"><span class="pre">PyWideStringList</span></code>): <code class="docutils literal notranslate"><span class="pre">sys._xoptions</span></code>.</li> </ul> <p>The <code class="docutils literal notranslate"><span class="pre">legacy_windows_stdio</span></code> field is only available on Windows. <code class="docutils literal notranslate"><span class="pre">#ifdef</span> <span class="pre">MS_WINDOWS</span></code> macro can be used for Windows specific code.</p> <p>If <code class="docutils literal notranslate"><span class="pre">parse_argv</span></code> is non-zero, <code class="docutils literal notranslate"><span class="pre">argv</span></code> arguments are parsed the same way the regular Python parses command line arguments, and Python arguments are stripped from <code class="docutils literal notranslate"><span class="pre">argv</span></code>: see <a class="reference internal" href="#command-line-arguments">Command Line Arguments</a>.</p> <p>The <code class="docutils literal notranslate"><span class="pre">xoptions</span></code> options are parsed to set other options: see <a class="reference internal" href="#x-options">-X Options</a>.</p> <p><code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> private fields, for internal use only:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">_config_init</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Function used to initialize <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code>, used for preinitialization.</li> <li><code class="docutils literal notranslate"><span class="pre">_install_importlib</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): Install importlib?</li> <li><code class="docutils literal notranslate"><span class="pre">_init_main</span></code> (<code class="docutils literal notranslate"><span class="pre">int</span></code>): If equal to 0, stop Python initialization before the “main” phase (see <a class="pep reference internal" href="../pep-0432/" title="PEP 432 – Restructuring the CPython startup sequence">PEP 432</a>).</li> </ul> <p>More complete example modifying the default configuration, read the configuration, and then override some parameters:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">PyStatus</span> <span class="n">init_python</span><span class="p">(</span><span class="n">const</span> <span class="n">char</span> <span class="o">*</span><span class="n">program_name</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyStatus</span> <span class="n">status</span><span class="p">;</span> <span class="n">PyConfig</span> <span class="n">config</span><span class="p">;</span> <span class="n">PyConfig_InitPythonConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="o">/*</span> <span class="n">Set</span> <span class="n">the</span> <span class="n">program</span> <span class="n">name</span> <span class="n">before</span> <span class="n">reading</span> <span class="n">the</span> <span class="n">configuration</span> <span class="p">(</span><span class="n">decode</span> <span class="n">byte</span> <span class="n">string</span> <span class="kn">from</span><span class="w"> </span><span class="nn">the</span> <span class="n">locale</span> <span class="n">encoding</span><span class="p">)</span><span class="o">.</span> <span class="n">Implicitly</span> <span class="n">preinitialize</span> <span class="n">Python</span><span class="o">.</span> <span class="o">*/</span> <span class="n">status</span> <span class="o">=</span> <span class="n">PyConfig_SetBytesString</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">config</span><span class="o">.</span><span class="n">program_name</span><span class="p">,</span> <span class="n">program_name</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="n">goto</span> <span class="n">done</span><span class="p">;</span> <span class="p">}</span> <span class="o">/*</span> <span class="n">Read</span> <span class="nb">all</span> <span class="n">configuration</span> <span class="n">at</span> <span class="n">once</span> <span class="o">*/</span> <span class="n">status</span> <span class="o">=</span> <span class="n">PyConfig_Read</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="n">goto</span> <span class="n">done</span><span class="p">;</span> <span class="p">}</span> <span class="o">/*</span> <span class="n">Append</span> <span class="n">our</span> <span class="n">custom</span> <span class="n">search</span> <span class="n">path</span> <span class="n">to</span> <span class="n">sys</span><span class="o">.</span><span class="n">path</span> <span class="o">*/</span> <span class="n">status</span> <span class="o">=</span> <span class="n">PyWideStringList_Append</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="o">.</span><span class="n">module_search_paths</span><span class="p">,</span> <span class="n">L</span><span class="s2">&quot;/path/to/more/modules&quot;</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="n">goto</span> <span class="n">done</span><span class="p">;</span> <span class="p">}</span> <span class="o">/*</span> <span class="n">Override</span> <span class="n">executable</span> <span class="n">computed</span> <span class="n">by</span> <span class="n">PyConfig_Read</span><span class="p">()</span> <span class="o">*/</span> <span class="n">status</span> <span class="o">=</span> <span class="n">PyConfig_SetString</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">config</span><span class="o">.</span><span class="n">executable</span><span class="p">,</span> <span class="n">L</span><span class="s2">&quot;/path/to/my_executable&quot;</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="n">goto</span> <span class="n">done</span><span class="p">;</span> <span class="p">}</span> <span class="n">status</span> <span class="o">=</span> <span class="n">Py_InitializeFromConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="n">done</span><span class="p">:</span> <span class="n">PyConfig_Clear</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="k">return</span> <span class="n">status</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p><code class="docutils literal notranslate"><span class="pre">PyImport_FrozenModules</span></code>, <code class="docutils literal notranslate"><span class="pre">PyImport_AppendInittab()</span></code> and <code class="docutils literal notranslate"><span class="pre">PyImport_ExtendInittab()</span></code> functions are still relevant and continue to work as previously. They should be set or called after Python preinitialization and before the Python initialization.</p> </div> </section> <section id="isolated-configuration"> <h3><a class="toc-backref" href="#isolated-configuration" role="doc-backlink">Isolated Configuration</a></h3> <p><code class="docutils literal notranslate"><span class="pre">PyPreConfig_InitIsolatedConfig()</span></code> and <code class="docutils literal notranslate"><span class="pre">PyConfig_InitIsolatedConfig()</span></code> functions create a configuration to isolate Python from the system. For example, to embed Python into an application.</p> <p>This configuration ignores global configuration variables, environments variables and command line arguments (<code class="docutils literal notranslate"><span class="pre">argv</span></code> is not parsed). The C standard streams (ex: <code class="docutils literal notranslate"><span class="pre">stdout</span></code>) and the LC_CTYPE locale are left unchanged by default.</p> <p>Configuration files are still used with this configuration. Set the <a class="reference internal" href="#path-configuration">Path Configuration</a> (“output fields”) to ignore these configuration files and avoid the function computing the default path configuration.</p> </section> <section id="python-configuration"> <h3><a class="toc-backref" href="#python-configuration" role="doc-backlink">Python Configuration</a></h3> <p><code class="docutils literal notranslate"><span class="pre">PyPreConfig_InitPythonConfig()</span></code> and <code class="docutils literal notranslate"><span class="pre">PyConfig_InitPythonConfig()</span></code> functions create a configuration to build a customized Python which behaves as the regular Python.</p> <p>Environments variables and command line arguments are used to configure Python, whereas global configuration variables are ignored.</p> <p>This function enables C locale coercion (<a class="pep reference internal" href="../pep-0538/" title="PEP 538 – Coercing the legacy C locale to a UTF-8 based locale">PEP 538</a>) and UTF-8 Mode (PEP 540) depending on the LC_CTYPE locale, <code class="docutils literal notranslate"><span class="pre">PYTHONUTF8</span></code> and <code class="docutils literal notranslate"><span class="pre">PYTHONCOERCECLOCALE</span></code> environment variables.</p> <p>Example of customized Python always running in isolated mode:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">int</span> <span class="n">main</span><span class="p">(</span><span class="nb">int</span> <span class="n">argc</span><span class="p">,</span> <span class="n">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyStatus</span> <span class="n">status</span><span class="p">;</span> <span class="n">PyConfig</span> <span class="n">config</span><span class="p">;</span> <span class="n">PyConfig_InitPythonConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="n">config</span><span class="o">.</span><span class="n">isolated</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="o">/*</span> <span class="n">Decode</span> <span class="n">command</span> <span class="n">line</span> <span class="n">arguments</span><span class="o">.</span> <span class="n">Implicitly</span> <span class="n">preinitialize</span> <span class="n">Python</span> <span class="p">(</span><span class="ow">in</span> <span class="n">isolated</span> <span class="n">mode</span><span class="p">)</span><span class="o">.</span> <span class="o">*/</span> <span class="n">status</span> <span class="o">=</span> <span class="n">PyConfig_SetBytesArgv</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">,</span> <span class="n">argc</span><span class="p">,</span> <span class="n">argv</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="n">goto</span> <span class="n">fail</span><span class="p">;</span> <span class="p">}</span> <span class="n">status</span> <span class="o">=</span> <span class="n">Py_InitializeFromConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="n">goto</span> <span class="n">fail</span><span class="p">;</span> <span class="p">}</span> <span class="n">PyConfig_Clear</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="k">return</span> <span class="n">Py_RunMain</span><span class="p">();</span> <span class="n">fail</span><span class="p">:</span> <span class="n">PyConfig_Clear</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_IsExit</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="k">return</span> <span class="n">status</span><span class="o">.</span><span class="n">exitcode</span><span class="p">;</span> <span class="p">}</span> <span class="o">/*</span> <span class="n">Display</span> <span class="n">the</span> <span class="n">error</span> <span class="n">message</span> <span class="ow">and</span> <span class="n">exit</span> <span class="n">the</span> <span class="n">process</span> <span class="k">with</span> <span class="n">non</span><span class="o">-</span><span class="n">zero</span> <span class="n">exit</span> <span class="n">code</span> <span class="o">*/</span> <span class="n">Py_ExitStatusException</span><span class="p">(</span><span class="n">status</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>This example is a basic implementation of the “System Python Executable” discussed in <a class="pep reference internal" href="../pep-0432/" title="PEP 432 – Restructuring the CPython startup sequence">PEP 432</a>.</p> </section> <section id="path-configuration"> <h3><a class="toc-backref" href="#path-configuration" role="doc-backlink">Path Configuration</a></h3> <p><code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> contains multiple fields for the path configuration:</p> <ul class="simple"> <li>Path configuration input fields:<ul> <li><code class="docutils literal notranslate"><span class="pre">home</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">pythonpath_env</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">pathconfig_warnings</span></code></li> </ul> </li> <li>Path configuration output fields:<ul> <li><code class="docutils literal notranslate"><span class="pre">exec_prefix</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">executable</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">prefix</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">module_search_paths_set</span></code>, <code class="docutils literal notranslate"><span class="pre">module_search_paths</span></code></li> </ul> </li> </ul> <p>If at least one “output field” is not set, Python computes the path configuration to fill unset fields. If <code class="docutils literal notranslate"><span class="pre">module_search_paths_set</span></code> is equal to 0, <code class="docutils literal notranslate"><span class="pre">module_search_paths</span></code> is overridden and <code class="docutils literal notranslate"><span class="pre">module_search_paths_set</span></code> is set to 1.</p> <p>It is possible to completely ignore the function computing the default path configuration by setting explicitly all path configuration output fields listed above. A string is considered as set even if it is non-empty. <code class="docutils literal notranslate"><span class="pre">module_search_paths</span></code> is considered as set if <code class="docutils literal notranslate"><span class="pre">module_search_paths_set</span></code> is set to 1. In this case, path configuration input fields are ignored as well.</p> <p>Set <code class="docutils literal notranslate"><span class="pre">pathconfig_warnings</span></code> to 0 to suppress warnings when computing the path configuration (Unix only, Windows does not log any warning).</p> <p>If <code class="docutils literal notranslate"><span class="pre">base_prefix</span></code> or <code class="docutils literal notranslate"><span class="pre">base_exec_prefix</span></code> fields are not set, they inherit their value from <code class="docutils literal notranslate"><span class="pre">prefix</span></code> and <code class="docutils literal notranslate"><span class="pre">exec_prefix</span></code> respectively.</p> <p><code class="docutils literal notranslate"><span class="pre">Py_RunMain()</span></code> and <code class="docutils literal notranslate"><span class="pre">Py_Main()</span></code> modify <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>:</p> <ul class="simple"> <li>If <code class="docutils literal notranslate"><span class="pre">run_filename</span></code> is set and is a directory which contains a <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> script, prepend <code class="docutils literal notranslate"><span class="pre">run_filename</span></code> to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>.</li> <li>If <code class="docutils literal notranslate"><span class="pre">isolated</span></code> is zero:<ul> <li>If <code class="docutils literal notranslate"><span class="pre">run_module</span></code> is set, prepend the current directory to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>. Do nothing if the current directory cannot be read.</li> <li>If <code class="docutils literal notranslate"><span class="pre">run_filename</span></code> is set, prepends the directory of the filename to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>.</li> <li>Otherwise, prepends an empty string to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>.</li> </ul> </li> </ul> <p>If <code class="docutils literal notranslate"><span class="pre">site_import</span></code> is non-zero, <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> can be modified by the <code class="docutils literal notranslate"><span class="pre">site</span></code> module. If <code class="docutils literal notranslate"><span class="pre">user_site_directory</span></code> is non-zero and the user’s site-package directory exists, the <code class="docutils literal notranslate"><span class="pre">site</span></code> module appends the user’s site-package directory to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>.</p> <p>See also <a class="reference internal" href="#configuration-files">Configuration Files</a> used by the path configuration.</p> </section> <section id="py-bytesmain"> <h3><a class="toc-backref" href="#py-bytesmain" role="doc-backlink">Py_BytesMain()</a></h3> <p>Python 3.7 provides a high-level <code class="docutils literal notranslate"><span class="pre">Py_Main()</span></code> function which requires to pass command line arguments as <code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code> strings. It is non-trivial to use the correct encoding to decode bytes. Python has its own set of issues with C locale coercion and UTF-8 Mode.</p> <p>This PEP adds a new <code class="docutils literal notranslate"><span class="pre">Py_BytesMain()</span></code> function which takes command line arguments as bytes:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">int</span> <span class="n">Py_BytesMain</span><span class="p">(</span><span class="nb">int</span> <span class="n">argc</span><span class="p">,</span> <span class="n">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span> </pre></div> </div> </section> <section id="py-runmain"> <h3><a class="toc-backref" href="#py-runmain" role="doc-backlink">Py_RunMain()</a></h3> <p>The new <code class="docutils literal notranslate"><span class="pre">Py_RunMain()</span></code> function executes the command (<code class="docutils literal notranslate"><span class="pre">PyConfig.run_command</span></code>), the script (<code class="docutils literal notranslate"><span class="pre">PyConfig.run_filename</span></code>) or the module (<code class="docutils literal notranslate"><span class="pre">PyConfig.run_module</span></code>) specified on the command line or in the configuration, and then finalizes Python. It returns an exit status that can be passed to the <code class="docutils literal notranslate"><span class="pre">exit()</span></code> function.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">int</span> <span class="n">Py_RunMain</span><span class="p">(</span><span class="n">void</span><span class="p">);</span> </pre></div> </div> <p>See <a class="reference internal" href="#python-configuration">Python Configuration</a> for an example of customized Python always running in isolated mode using <code class="docutils literal notranslate"><span class="pre">Py_RunMain()</span></code>.</p> </section> <section id="multi-phase-initialization-private-provisional-api"> <h3><a class="toc-backref" href="#multi-phase-initialization-private-provisional-api" role="doc-backlink">Multi-Phase Initialization Private Provisional API</a></h3> <p>This section is a private provisional API introducing multi-phase initialization, the core feature of the <a class="pep reference internal" href="../pep-0432/" title="PEP 432 – Restructuring the CPython startup sequence">PEP 432</a>:</p> <ul class="simple"> <li>“Core” initialization phase, “bare minimum Python”:<ul> <li>Builtin types;</li> <li>Builtin exceptions;</li> <li>Builtin and frozen modules;</li> <li>The <code class="docutils literal notranslate"><span class="pre">sys</span></code> module is only partially initialized (ex: <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> doesn’t exist yet);</li> </ul> </li> <li>“Main” initialization phase, Python is fully initialized:<ul> <li>Install and configure <code class="docutils literal notranslate"><span class="pre">importlib</span></code>;</li> <li>Apply the <a class="reference internal" href="#path-configuration">Path Configuration</a>;</li> <li>Install signal handlers;</li> <li>Finish <code class="docutils literal notranslate"><span class="pre">sys</span></code> module initialization (ex: create <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>);</li> <li>Enable optional features like <code class="docutils literal notranslate"><span class="pre">faulthandler</span></code> and <code class="docutils literal notranslate"><span class="pre">tracemalloc</span></code>;</li> <li>Import the <code class="docutils literal notranslate"><span class="pre">site</span></code> module;</li> <li>etc.</li> </ul> </li> </ul> <p>Private provisional API:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PyConfig._init_main</span></code>: if set to 0, <code class="docutils literal notranslate"><span class="pre">Py_InitializeFromConfig()</span></code> stops at the “Core” initialization phase.</li> <li><code class="docutils literal notranslate"><span class="pre">PyStatus</span> <span class="pre">_Py_InitializeMain(void)</span></code>: move to the “Main” initialization phase, finish the Python initialization.</li> </ul> <p>No module is imported during the “Core” phase and the <code class="docutils literal notranslate"><span class="pre">importlib</span></code> module is not configured: the <a class="reference internal" href="#path-configuration">Path Configuration</a> is only applied during the “Main” phase. It may allow to customize Python in Python to override or tune the <a class="reference internal" href="#path-configuration">Path Configuration</a>, maybe install a custom sys.meta_path importer or an import hook, etc.</p> <p>It may become possible to compute the <a class="reference internal" href="#path-configuration">Path Configuration</a> in Python, after the Core phase and before the Main phase, which is one of the PEP 432 motivation.</p> <p>The “Core” phase is not properly defined: what should be and what should not be available at this phase is not specified yet. The API is marked as private and provisional: the API can be modified or even be removed anytime until a proper public API is designed.</p> <p>Example running Python code between “Core” and “Main” initialization phases:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">void</span> <span class="n">init_python</span><span class="p">(</span><span class="n">void</span><span class="p">)</span> <span class="p">{</span> <span class="n">PyStatus</span> <span class="n">status</span><span class="p">;</span> <span class="n">PyConfig</span> <span class="n">config</span><span class="p">;</span> <span class="n">PyConfig_InitPythonConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="n">config</span><span class="o">.</span><span class="n">_init_main</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="o">/*</span> <span class="o">...</span> <span class="n">customize</span> <span class="s1">&#39;config&#39;</span> <span class="n">configuration</span> <span class="o">...</span> <span class="o">*/</span> <span class="n">status</span> <span class="o">=</span> <span class="n">Py_InitializeFromConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="n">PyConfig_Clear</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="n">Py_ExitStatusException</span><span class="p">(</span><span class="n">status</span><span class="p">);</span> <span class="p">}</span> <span class="o">/*</span> <span class="n">Use</span> <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span> <span class="n">because</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span> <span class="ow">is</span> <span class="n">only</span> <span class="n">created</span> <span class="n">by</span> <span class="n">_Py_InitializeMain</span><span class="p">()</span> <span class="o">*/</span> <span class="nb">int</span> <span class="n">res</span> <span class="o">=</span> <span class="n">PyRun_SimpleString</span><span class="p">(</span> <span class="s2">&quot;import sys; &quot;</span> <span class="s2">&quot;print(&#39;Run Python code before _Py_InitializeMain&#39;, &quot;</span> <span class="s2">&quot;file=sys.stderr)&quot;</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">res</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> <span class="p">}</span> <span class="o">/*</span> <span class="o">...</span> <span class="n">put</span> <span class="n">more</span> <span class="n">configuration</span> <span class="n">code</span> <span class="n">here</span> <span class="o">...</span> <span class="o">*/</span> <span class="n">status</span> <span class="o">=</span> <span class="n">_Py_InitializeMain</span><span class="p">();</span> <span class="k">if</span> <span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span> <span class="p">{</span> <span class="n">Py_ExitStatusException</span><span class="p">(</span><span class="n">status</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> </pre></div> </div> </section> </section> <section id="backwards-compatibility"> <h2><a class="toc-backref" href="#backwards-compatibility" role="doc-backlink">Backwards Compatibility</a></h2> <p>This PEP only adds a new API: it leaves the existing API unchanged and has no impact on the backwards compatibility.</p> <p>The Python 3.7 <code class="docutils literal notranslate"><span class="pre">Py_Initialize()</span></code> function now disable the C locale coercion (<a class="pep reference internal" href="../pep-0538/" title="PEP 538 – Coercing the legacy C locale to a UTF-8 based locale">PEP 538</a>) and the UTF-8 Mode (<a class="pep reference internal" href="../pep-0540/" title="PEP 540 – Add a new UTF-8 Mode">PEP 540</a>) by default to prevent mojibake. The new API using the <a class="reference internal" href="#python-configuration">Python Configuration</a> is needed to enable them automatically.</p> </section> <section id="annexes"> <h2><a class="toc-backref" href="#annexes" role="doc-backlink">Annexes</a></h2> <section id="comparison-of-python-and-isolated-configurations"> <h3><a class="toc-backref" href="#comparison-of-python-and-isolated-configurations" role="doc-backlink">Comparison of Python and Isolated Configurations</a></h3> <p>Differences between <code class="docutils literal notranslate"><span class="pre">PyPreConfig_InitPythonConfig()</span></code> and <code class="docutils literal notranslate"><span class="pre">PyPreConfig_InitIsolatedConfig()</span></code>:</p> <table class="docutils align-default"> <thead> <tr class="row-odd"><th class="head">PyPreConfig</th> <th class="head">Python</th> <th class="head">Isolated</th> </tr> </thead> <tbody> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">coerce_c_locale_warn</span></code></td> <td>-1</td> <td>0</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">coerce_c_locale</span></code></td> <td>-1</td> <td>0</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">configure_locale</span></code></td> <td><strong>1</strong></td> <td>0</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">dev_mode</span></code></td> <td>-1</td> <td>0</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">isolated</span></code></td> <td>0</td> <td><strong>1</strong></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">legacy_windows_fs_encoding</span></code></td> <td>-1</td> <td>0</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">use_environment</span></code></td> <td>0</td> <td>0</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">parse_argv</span></code></td> <td><strong>1</strong></td> <td>0</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">utf8_mode</span></code></td> <td>-1</td> <td>0</td> </tr> </tbody> </table> <p>Differences between <code class="docutils literal notranslate"><span class="pre">PyConfig_InitPythonConfig()</span></code> and <code class="docutils literal notranslate"><span class="pre">PyConfig_InitIsolatedConfig()</span></code>:</p> <table class="docutils align-default"> <thead> <tr class="row-odd"><th class="head">PyConfig</th> <th class="head">Python</th> <th class="head">Isolated</th> </tr> </thead> <tbody> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">configure_c_stdio</span></code></td> <td><strong>1</strong></td> <td>0</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">install_signal_handlers</span></code></td> <td><strong>1</strong></td> <td>0</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">isolated</span></code></td> <td>0</td> <td><strong>1</strong></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">parse_argv</span></code></td> <td><strong>1</strong></td> <td>0</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">pathconfig_warnings</span></code></td> <td><strong>1</strong></td> <td>0</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">use_environment</span></code></td> <td><strong>1</strong></td> <td>0</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">user_site_directory</span></code></td> <td><strong>1</strong></td> <td>0</td> </tr> </tbody> </table> </section> <section id="priority-and-rules"> <h3><a class="toc-backref" href="#priority-and-rules" role="doc-backlink">Priority and Rules</a></h3> <p>Priority of configuration parameters, highest to lowest:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PyConfig</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code></li> <li>Configuration files</li> <li>Command line options</li> <li>Environment variables</li> <li>Global configuration variables</li> </ul> <p>Priority of warning options, highest to lowest:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PyConfig.warnoptions</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PySys_AddWarnOption()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyConfig.bytes_warning</span></code> (add <code class="docutils literal notranslate"><span class="pre">&quot;error::BytesWarning&quot;</span></code> filter if greater than 1, add <code class="docutils literal notranslate"><span class="pre">&quot;default::BytesWarning</span></code> filter if equals to 1)</li> <li><code class="docutils literal notranslate"><span class="pre">-W</span> <span class="pre">opt</span></code> command line argument</li> <li><code class="docutils literal notranslate"><span class="pre">PYTHONWARNINGS</span></code> environment variable</li> <li><code class="docutils literal notranslate"><span class="pre">PyConfig.dev_mode</span></code> (add <code class="docutils literal notranslate"><span class="pre">&quot;default&quot;</span></code> filter)</li> </ul> <p>Rules on <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> parameters:</p> <ul class="simple"> <li>If <code class="docutils literal notranslate"><span class="pre">isolated</span></code> is non-zero, <code class="docutils literal notranslate"><span class="pre">use_environment</span></code> and <code class="docutils literal notranslate"><span class="pre">user_site_directory</span></code> are set to 0.</li> <li>If <code class="docutils literal notranslate"><span class="pre">dev_mode</span></code> is non-zero, <code class="docutils literal notranslate"><span class="pre">allocator</span></code> is set to <code class="docutils literal notranslate"><span class="pre">&quot;debug&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">faulthandler</span></code> is set to 1, and <code class="docutils literal notranslate"><span class="pre">&quot;default&quot;</span></code> filter is added to <code class="docutils literal notranslate"><span class="pre">warnoptions</span></code>. But the <code class="docutils literal notranslate"><span class="pre">PYTHONMALLOC</span></code> environment variable has the priority over <code class="docutils literal notranslate"><span class="pre">dev_mode</span></code> to set the memory allocator.</li> <li>If <code class="docutils literal notranslate"><span class="pre">base_prefix</span></code> is not set, it inherits <code class="docutils literal notranslate"><span class="pre">prefix</span></code> value.</li> <li>If <code class="docutils literal notranslate"><span class="pre">base_exec_prefix</span></code> is not set, it inherits <code class="docutils literal notranslate"><span class="pre">exec_prefix</span></code> value.</li> <li>If the <code class="docutils literal notranslate"><span class="pre">python._pth</span></code> configuration file is present, <code class="docutils literal notranslate"><span class="pre">isolated</span></code> is set to 1 and <code class="docutils literal notranslate"><span class="pre">site_import</span></code> is set to 0; but <code class="docutils literal notranslate"><span class="pre">site_import</span></code> is set to 1 if <code class="docutils literal notranslate"><span class="pre">python._pth</span></code> contains <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">site</span></code>.</li> </ul> <p>Rules on <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> and <code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> parameters:</p> <ul class="simple"> <li>If <code class="docutils literal notranslate"><span class="pre">PyPreConfig.legacy_windows_fs_encoding</span></code> is non-zero, set <code class="docutils literal notranslate"><span class="pre">PyPreConfig.utf8_mode</span></code> to 0, set <code class="docutils literal notranslate"><span class="pre">PyConfig.filesystem_encoding</span></code> to <code class="docutils literal notranslate"><span class="pre">mbcs</span></code>, and set <code class="docutils literal notranslate"><span class="pre">PyConfig.filesystem_errors</span></code> to <code class="docutils literal notranslate"><span class="pre">replace</span></code>.</li> </ul> </section> <section id="configuration-files"> <h3><a class="toc-backref" href="#configuration-files" role="doc-backlink">Configuration Files</a></h3> <p>Python configuration files used by the <a class="reference internal" href="#path-configuration">Path Configuration</a>:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">python._pth</span></code> (Windows only)</li> <li><code class="docutils literal notranslate"><span class="pre">pybuilddir.txt</span></code> (Unix only)</li> </ul> </section> <section id="global-configuration-variables"> <h3><a class="toc-backref" href="#global-configuration-variables" role="doc-backlink">Global Configuration Variables</a></h3> <p>Global configuration variables mapped to <code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> fields:</p> <table class="docutils align-default"> <thead> <tr class="row-odd"><th class="head">Variable</th> <th class="head">Field</th> </tr> </thead> <tbody> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">Py_IgnoreEnvironmentFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">use_environment</span></code> (NOT)</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Py_IsolatedFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">isolated</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">Py_LegacyWindowsFSEncodingFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">legacy_windows_fs_encoding</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Py_UTF8Mode</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">utf8_mode</span></code></td> </tr> </tbody> </table> <p>(NOT) means that the <code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> value is the opposite of the global configuration variable value. <code class="docutils literal notranslate"><span class="pre">Py_LegacyWindowsFSEncodingFlag</span></code> is only available on Windows.</p> <p>Global configuration variables mapped to <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> fields:</p> <table class="docutils align-default"> <thead> <tr class="row-odd"><th class="head">Variable</th> <th class="head">Field</th> </tr> </thead> <tbody> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">Py_BytesWarningFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">bytes_warning</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Py_DebugFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">parser_debug</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">Py_DontWriteBytecodeFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">write_bytecode</span></code> (NOT)</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Py_FileSystemDefaultEncodeErrors</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">filesystem_errors</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">Py_FileSystemDefaultEncoding</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">filesystem_encoding</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Py_FrozenFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">pathconfig_warnings</span></code> (NOT)</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">Py_HasFileSystemDefaultEncoding</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">filesystem_encoding</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Py_HashRandomizationFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">use_hash_seed</span></code>, <code class="docutils literal notranslate"><span class="pre">hash_seed</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">Py_IgnoreEnvironmentFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">use_environment</span></code> (NOT)</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Py_InspectFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">inspect</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">Py_InteractiveFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">interactive</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Py_IsolatedFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">isolated</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">Py_LegacyWindowsStdioFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">legacy_windows_stdio</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Py_NoSiteFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">site_import</span></code> (NOT)</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">Py_NoUserSiteDirectory</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">user_site_directory</span></code> (NOT)</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Py_OptimizeFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">optimization_level</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">Py_QuietFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">quiet</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Py_UnbufferedStdioFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">buffered_stdio</span></code> (NOT)</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">Py_VerboseFlag</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">verbose</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">_Py_HasFileSystemDefaultEncodeErrors</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">filesystem_errors</span></code></td> </tr> </tbody> </table> <p>(NOT) means that the <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> value is the opposite of the global configuration variable value. <code class="docutils literal notranslate"><span class="pre">Py_LegacyWindowsStdioFlag</span></code> is only available on Windows.</p> </section> <section id="command-line-arguments"> <h3><a class="toc-backref" href="#command-line-arguments" role="doc-backlink">Command Line Arguments</a></h3> <p>Usage:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python3</span> <span class="p">[</span><span class="n">options</span><span class="p">]</span> <span class="n">python3</span> <span class="p">[</span><span class="n">options</span><span class="p">]</span> <span class="o">-</span><span class="n">c</span> <span class="n">COMMAND</span> <span class="n">python3</span> <span class="p">[</span><span class="n">options</span><span class="p">]</span> <span class="o">-</span><span class="n">m</span> <span class="n">MODULE</span> <span class="n">python3</span> <span class="p">[</span><span class="n">options</span><span class="p">]</span> <span class="n">SCRIPT</span> </pre></div> </div> <p>Command line options mapped to pseudo-action on <code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> fields:</p> <table class="docutils align-default"> <thead> <tr class="row-odd"><th class="head">Option</th> <th class="head"><code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> field</th> </tr> </thead> <tbody> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-E</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">use_environment</span> <span class="pre">=</span> <span class="pre">0</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-I</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">isolated</span> <span class="pre">=</span> <span class="pre">1</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">dev</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">dev_mode</span> <span class="pre">=</span> <span class="pre">1</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">utf8_mode</span> <span class="pre">=</span> <span class="pre">1</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8=VALUE</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">utf8_mode</span> <span class="pre">=</span> <span class="pre">VALUE</span></code></td> </tr> </tbody> </table> <p>Command line options mapped to pseudo-action on <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> fields:</p> <table class="docutils align-default"> <thead> <tr class="row-odd"><th class="head">Option</th> <th class="head"><code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> field</th> </tr> </thead> <tbody> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-b</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">bytes_warning++</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-B</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">write_bytecode</span> <span class="pre">=</span> <span class="pre">0</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-c</span> <span class="pre">COMMAND</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">run_command</span> <span class="pre">=</span> <span class="pre">COMMAND</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">--check-hash-based-pycs=MODE</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">check_hash_pycs_mode</span> <span class="pre">=</span> <span class="pre">MODE</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-d</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">parser_debug++</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-E</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">use_environment</span> <span class="pre">=</span> <span class="pre">0</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-i</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">inspect++</span></code> and <code class="docutils literal notranslate"><span class="pre">interactive++</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-I</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">isolated</span> <span class="pre">=</span> <span class="pre">1</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-m</span> <span class="pre">MODULE</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">run_module</span> <span class="pre">=</span> <span class="pre">MODULE</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-O</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">optimization_level++</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-q</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">quiet++</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-R</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">use_hash_seed</span> <span class="pre">=</span> <span class="pre">0</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-s</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">user_site_directory</span> <span class="pre">=</span> <span class="pre">0</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-S</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">site_import</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-t</span></code></td> <td>ignored (kept for backwards compatibility)</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-u</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">buffered_stdio</span> <span class="pre">=</span> <span class="pre">0</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-v</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">verbose++</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-W</span> <span class="pre">WARNING</span></code></td> <td>add <code class="docutils literal notranslate"><span class="pre">WARNING</span></code> to <code class="docutils literal notranslate"><span class="pre">warnoptions</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-x</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">skip_source_first_line</span> <span class="pre">=</span> <span class="pre">1</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">OPTION</span></code></td> <td>add <code class="docutils literal notranslate"><span class="pre">OPTION</span></code> to <code class="docutils literal notranslate"><span class="pre">xoptions</span></code></td> </tr> </tbody> </table> <p><code class="docutils literal notranslate"><span class="pre">-h</span></code>, <code class="docutils literal notranslate"><span class="pre">-?</span></code> and <code class="docutils literal notranslate"><span class="pre">-V</span></code> options are handled without <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code>.</p> </section> <section id="x-options"> <h3><a class="toc-backref" href="#x-options" role="doc-backlink">-X Options</a></h3> <p>-X options mapped to pseudo-action on <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> fields:</p> <table class="docutils align-default"> <thead> <tr class="row-odd"><th class="head">Option</th> <th class="head"><code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> field</th> </tr> </thead> <tbody> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">dev</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">dev_mode</span> <span class="pre">=</span> <span class="pre">1</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">faulthandler</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">faulthandler</span> <span class="pre">=</span> <span class="pre">1</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">importtime</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">import_time</span> <span class="pre">=</span> <span class="pre">1</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">pycache_prefix=PREFIX</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">pycache_prefix</span> <span class="pre">=</span> <span class="pre">PREFIX</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">showalloccount</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">show_alloc_count</span> <span class="pre">=</span> <span class="pre">1</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">showrefcount</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">show_ref_count</span> <span class="pre">=</span> <span class="pre">1</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">tracemalloc=N</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">tracemalloc</span> <span class="pre">=</span> <span class="pre">N</span></code></td> </tr> </tbody> </table> </section> <section id="environment-variables"> <h3><a class="toc-backref" href="#environment-variables" role="doc-backlink">Environment Variables</a></h3> <p>Environment variables mapped to <code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> fields:</p> <table class="docutils align-default"> <thead> <tr class="row-odd"><th class="head">Variable</th> <th class="head"><code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> field</th> </tr> </thead> <tbody> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONCOERCECLOCALE</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">coerce_c_locale</span></code>, <code class="docutils literal notranslate"><span class="pre">coerce_c_locale_warn</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">PYTHONDEVMODE</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">dev_mode</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONLEGACYWINDOWSFSENCODING</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">legacy_windows_fs_encoding</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">PYTHONMALLOC</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">allocator</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONUTF8</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">utf8_mode</span></code></td> </tr> </tbody> </table> <p>Environment variables mapped to <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> fields:</p> <table class="docutils align-default"> <thead> <tr class="row-odd"><th class="head">Variable</th> <th class="head"><code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> field</th> </tr> </thead> <tbody> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONDEBUG</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">parser_debug</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">PYTHONDEVMODE</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">dev_mode</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONDONTWRITEBYTECODE</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">write_bytecode</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">PYTHONDUMPREFS</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">dump_refs</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONEXECUTABLE</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">program_name</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">PYTHONFAULTHANDLER</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">faulthandler</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONHASHSEED</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">use_hash_seed</span></code>, <code class="docutils literal notranslate"><span class="pre">hash_seed</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">home</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONINSPECT</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">inspect</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">PYTHONIOENCODING</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">stdio_encoding</span></code>, <code class="docutils literal notranslate"><span class="pre">stdio_errors</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONLEGACYWINDOWSSTDIO</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">legacy_windows_stdio</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">PYTHONMALLOCSTATS</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">malloc_stats</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONNOUSERSITE</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">user_site_directory</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">PYTHONOPTIMIZE</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">optimization_level</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">pythonpath_env</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">PYTHONPROFILEIMPORTTIME</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">import_time</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONPYCACHEPREFIX,</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">pycache_prefix</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">PYTHONTRACEMALLOC</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">tracemalloc</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONUNBUFFERED</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">buffered_stdio</span></code></td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">PYTHONVERBOSE</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">verbose</span></code></td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">PYTHONWARNINGS</span></code></td> <td><code class="docutils literal notranslate"><span class="pre">warnoptions</span></code></td> </tr> </tbody> </table> <p><code class="docutils literal notranslate"><span class="pre">PYTHONLEGACYWINDOWSFSENCODING</span></code> and <code class="docutils literal notranslate"><span class="pre">PYTHONLEGACYWINDOWSSTDIO</span></code> are specific to Windows.</p> </section> <section id="default-python-configuration"> <h3><a class="toc-backref" href="#default-python-configuration" role="doc-backlink">Default Python Configuration</a></h3> <p><code class="docutils literal notranslate"><span class="pre">PyPreConfig_InitPythonConfig()</span></code>:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">allocator</span></code> = <code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_NOT_SET</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">coerce_c_locale_warn</span></code> = -1</li> <li><code class="docutils literal notranslate"><span class="pre">coerce_c_locale</span></code> = -1</li> <li><code class="docutils literal notranslate"><span class="pre">configure_locale</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">dev_mode</span></code> = -1</li> <li><code class="docutils literal notranslate"><span class="pre">isolated</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">legacy_windows_fs_encoding</span></code> = -1</li> <li><code class="docutils literal notranslate"><span class="pre">use_environment</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">utf8_mode</span></code> = -1</li> </ul> <p><code class="docutils literal notranslate"><span class="pre">PyConfig_InitPythonConfig()</span></code>:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">argv</span></code> = []</li> <li><code class="docutils literal notranslate"><span class="pre">base_exec_prefix</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">base_prefix</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">buffered_stdio</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">bytes_warning</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">check_hash_pycs_mode</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">configure_c_stdio</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">dev_mode</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">dump_refs</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">exec_prefix</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">executable</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">faulthandler</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">filesystem_encoding</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">filesystem_errors</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">hash_seed</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">home</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">import_time</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">inspect</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">install_signal_handlers</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">interactive</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">isolated</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">malloc_stats</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">module_search_path_env</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">module_search_paths</span></code> = []</li> <li><code class="docutils literal notranslate"><span class="pre">optimization_level</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">parse_argv</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">parser_debug</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">pathconfig_warnings</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">prefix</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">program_name</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">pycache_prefix</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">quiet</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">run_command</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">run_filename</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">run_module</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">show_alloc_count</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">show_ref_count</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">site_import</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">skip_source_first_line</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">stdio_encoding</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">stdio_errors</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">tracemalloc</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">use_environment</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">use_hash_seed</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">user_site_directory</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">verbose</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">warnoptions</span></code> = []</li> <li><code class="docutils literal notranslate"><span class="pre">write_bytecode</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">xoptions</span></code> = []</li> <li><code class="docutils literal notranslate"><span class="pre">_init_main</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">_install_importlib</span></code> = 1</li> </ul> </section> <section id="default-isolated-configuration"> <h3><a class="toc-backref" href="#default-isolated-configuration" role="doc-backlink">Default Isolated Configuration</a></h3> <p><code class="docutils literal notranslate"><span class="pre">PyPreConfig_InitIsolatedConfig()</span></code>:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">allocator</span></code> = <code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_NOT_SET</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">coerce_c_locale_warn</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">coerce_c_locale</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">configure_locale</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">dev_mode</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">isolated</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">legacy_windows_fs_encoding</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">use_environment</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">utf8_mode</span></code> = 0</li> </ul> <p><code class="docutils literal notranslate"><span class="pre">PyConfig_InitIsolatedConfig()</span></code>:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">argv</span></code> = []</li> <li><code class="docutils literal notranslate"><span class="pre">base_exec_prefix</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">base_prefix</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">buffered_stdio</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">bytes_warning</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">check_hash_pycs_mode</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">configure_c_stdio</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">dev_mode</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">dump_refs</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">exec_prefix</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">executable</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">faulthandler</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">filesystem_encoding</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">filesystem_errors</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">hash_seed</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">home</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">import_time</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">inspect</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">install_signal_handlers</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">interactive</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">isolated</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">malloc_stats</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">module_search_path_env</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">module_search_paths</span></code> = []</li> <li><code class="docutils literal notranslate"><span class="pre">optimization_level</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">parse_argv</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">parser_debug</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">pathconfig_warnings</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">prefix</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">program_name</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">pycache_prefix</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">quiet</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">run_command</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">run_filename</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">run_module</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">show_alloc_count</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">show_ref_count</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">site_import</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">skip_source_first_line</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">stdio_encoding</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">stdio_errors</span></code> = <code class="docutils literal notranslate"><span class="pre">NULL</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">tracemalloc</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">use_environment</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">use_hash_seed</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">user_site_directory</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">verbose</span></code> = 0</li> <li><code class="docutils literal notranslate"><span class="pre">warnoptions</span></code> = []</li> <li><code class="docutils literal notranslate"><span class="pre">write_bytecode</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">xoptions</span></code> = []</li> <li><code class="docutils literal notranslate"><span class="pre">_init_main</span></code> = 1</li> <li><code class="docutils literal notranslate"><span class="pre">_install_importlib</span></code> = 1</li> </ul> </section> <section id="python-3-7-api"> <h3><a class="toc-backref" href="#python-3-7-api" role="doc-backlink">Python 3.7 API</a></h3> <p>Python 3.7 has 4 functions in its C API to initialize and finalize Python:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">Py_Initialize()</span></code>, <code class="docutils literal notranslate"><span class="pre">Py_InitializeEx()</span></code>: initialize Python</li> <li><code class="docutils literal notranslate"><span class="pre">Py_Finalize()</span></code>, <code class="docutils literal notranslate"><span class="pre">Py_FinalizeEx()</span></code>: finalize Python</li> </ul> <p>Python 3.7 can be configured using <a class="reference internal" href="#global-configuration-variables">Global Configuration Variables</a>, <a class="reference internal" href="#environment-variables">Environment Variables</a>, and the following functions:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">PyImport_AppendInittab()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyImport_ExtendInittab()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyMem_SetAllocator()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyMem_SetupDebugHooks()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyObject_SetArenaAllocator()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">Py_SetPath()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">Py_SetProgramName()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">Py_SetPythonHome()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">Py_SetStandardStreamEncoding()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PySys_AddWarnOption()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PySys_AddXOption()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PySys_ResetWarnOptions()</span></code></li> </ul> <p>There is also a high-level <code class="docutils literal notranslate"><span class="pre">Py_Main()</span></code> function and <code class="docutils literal notranslate"><span class="pre">PyImport_FrozenModules</span></code> variable which can be overridden.</p> <p>See <a class="reference external" href="https://docs.python.org/dev/c-api/init.html">Initialization, Finalization, and Threads</a> documentation.</p> </section> </section> <section id="python-issues"> <h2><a class="toc-backref" href="#python-issues" role="doc-backlink">Python Issues</a></h2> <p>Issues that will be fixed by this PEP, directly or indirectly:</p> <ul class="simple"> <li><a class="reference external" href="https://bugs.python.org/issue1195571">bpo-1195571</a>: “simple callback system for Py_FatalError”</li> <li><a class="reference external" href="https://bugs.python.org/issue11320">bpo-11320</a>: “Usage of API method Py_SetPath causes errors in Py_Initialize() (Posix only)”</li> <li><a class="reference external" href="https://bugs.python.org/issue13533">bpo-13533</a>: “Would like Py_Initialize to play friendly with host app”</li> <li><a class="reference external" href="https://bugs.python.org/issue14956">bpo-14956</a>: “custom PYTHONPATH may break apps embedding Python”</li> <li><a class="reference external" href="https://bugs.python.org/issue19983">bpo-19983</a>: “When interrupted during startup, Python should not call abort() but exit()”</li> <li><a class="reference external" href="https://bugs.python.org/issue22213">bpo-22213</a>: “Make pyvenv style virtual environments easier to configure when embedding Python”.</li> <li><a class="reference external" href="https://bugs.python.org/issue29778">bpo-29778</a>: “_Py_CheckPython3 uses uninitialized dllpath when embedder sets module path with Py_SetPath”</li> <li><a class="reference external" href="https://bugs.python.org/issue30560">bpo-30560</a>: “Add Py_SetFatalErrorAbortFunc: Allow embedding program to handle fatal errors”.</li> <li><a class="reference external" href="https://bugs.python.org/issue31745">bpo-31745</a>: “Overloading “Py_GetPath” does not work”</li> <li><a class="reference external" href="https://bugs.python.org/issue32573">bpo-32573</a>: “All sys attributes (.argv, …) should exist in embedded environments”.</li> <li><a class="reference external" href="https://bugs.python.org/issue33135">bpo-33135</a>: “Define field prefixes for the various config structs”. The PEP now defines well how warnings options are handled.</li> <li><a class="reference external" href="https://bugs.python.org/issue34725">bpo-34725</a>: “Py_GetProgramFullPath() odd behaviour in Windows”</li> <li><a class="reference external" href="https://bugs.python.org/issue36204">bpo-36204</a>: “Deprecate calling Py_Main() after Py_Initialize()? Add Py_InitializeFromArgv()?”</li> </ul> <p>Issues of the PEP implementation:</p> <ul class="simple"> <li><a class="reference external" href="https://bugs.python.org/issue16961">bpo-16961</a>: “No regression tests for -E and individual environment vars”</li> <li><a class="reference external" href="https://bugs.python.org/issue20361">bpo-20361</a>: “-W command line options and PYTHONWARNINGS environmental variable should not override -b / -bb command line options”</li> <li><a class="reference external" href="https://bugs.python.org/issue26122">bpo-26122</a>: “Isolated mode doesn’t ignore PYTHONHASHSEED”</li> <li><a class="reference external" href="https://bugs.python.org/issue29818">bpo-29818</a>: “Py_SetStandardStreamEncoding leads to a memory error in debug mode”</li> <li><a class="reference external" href="https://bugs.python.org/issue31845">bpo-31845</a>: “PYTHONDONTWRITEBYTECODE and PYTHONOPTIMIZE have no effect”</li> <li><a class="reference external" href="https://bugs.python.org/issue32030">bpo-32030</a>: “PEP 432: Rewrite Py_Main()”</li> <li><a class="reference external" href="https://bugs.python.org/issue32124">bpo-32124</a>: “Document functions safe to be called before Py_Initialize()”</li> <li><a class="reference external" href="https://bugs.python.org/issue33042">bpo-33042</a>: “New 3.7 startup sequence crashes PyInstaller”</li> <li><a class="reference external" href="https://bugs.python.org/issue33932">bpo-33932</a>: “Calling Py_Initialize() twice now triggers a fatal error (Python 3.7)”</li> <li><a class="reference external" href="https://bugs.python.org/issue34008">bpo-34008</a>: “Do we support calling Py_Main() after Py_Initialize()?”</li> <li><a class="reference external" href="https://bugs.python.org/issue34170">bpo-34170</a>: “Py_Initialize(): computing path configuration must not have side effect (PEP 432)”</li> <li><a class="reference external" href="https://bugs.python.org/issue34589">bpo-34589</a>: “Py_Initialize() and Py_Main() should not enable C locale coercion”</li> <li><a class="reference external" href="https://bugs.python.org/issue34639">bpo-34639</a>: “PYTHONCOERCECLOCALE is ignored when using -E or -I option”</li> <li><a class="reference external" href="https://bugs.python.org/issue36142">bpo-36142</a>: “Add a new _PyPreConfig step to Python initialization to setup memory allocator and encodings”</li> <li><a class="reference external" href="https://bugs.python.org/issue36202">bpo-36202</a>: “Calling Py_DecodeLocale() before _PyPreConfig_Write() can produce mojibake”</li> <li><a class="reference external" href="https://bugs.python.org/issue36301">bpo-36301</a>: “Add _Py_PreInitialize() function”</li> <li><a class="reference external" href="https://bugs.python.org/issue36443">bpo-36443</a>: “Disable coerce_c_locale and utf8_mode by default in _PyPreConfig?”</li> <li><a class="reference external" href="https://bugs.python.org/issue36444">bpo-36444</a>: “Python initialization: remove _PyMainInterpreterConfig”</li> <li><a class="reference external" href="https://bugs.python.org/issue36471">bpo-36471</a>: “PEP 432, PEP 587: Add _Py_RunMain()”</li> <li><a class="reference external" href="https://bugs.python.org/issue36763">bpo-36763</a>: “PEP 587: Rework initialization API to prepare second version of the PEP”</li> <li><a class="reference external" href="https://bugs.python.org/issue36775">bpo-36775</a>: “Rework filesystem codec implementation”</li> <li><a class="reference external" href="https://bugs.python.org/issue36900">bpo-36900</a>: “Use _PyCoreConfig rather than global configuration variables”</li> </ul> <p>Issues related to this PEP:</p> <ul class="simple"> <li><a class="reference external" href="https://bugs.python.org/issue12598">bpo-12598</a>: “Move sys variable initialization from import.c to sysmodule.c”</li> <li><a class="reference external" href="https://bugs.python.org/issue15577">bpo-15577</a>: “Real argc and argv in embedded interpreter”</li> <li><a class="reference external" href="https://bugs.python.org/issue16202">bpo-16202</a>: “sys.path[0] security issues”</li> <li><a class="reference external" href="https://bugs.python.org/issue18309">bpo-18309</a>: “Make python slightly more relocatable”</li> <li><a class="reference external" href="https://bugs.python.org/issue22257">bpo-22257</a>: “PEP 432: Redesign the interpreter startup sequence”</li> <li><a class="reference external" href="https://bugs.python.org/issue25631">bpo-25631</a>: “Segmentation fault with invalid Unicode command-line arguments in embedded Python”</li> <li><a class="reference external" href="https://bugs.python.org/issue26007">bpo-26007</a>: “Support embedding the standard library in an executable”</li> <li><a class="reference external" href="https://bugs.python.org/issue31210">bpo-31210</a>: “Can not import modules if sys.prefix contains DELIM”.</li> <li><a class="reference external" href="https://bugs.python.org/issue31349">bpo-31349</a>: “Embedded initialization ignores Py_SetProgramName()”</li> <li><a class="reference external" href="https://bugs.python.org/issue33919">bpo-33919</a>: “Expose _PyCoreConfig structure to Python”</li> <li><a class="reference external" href="https://bugs.python.org/issue35173">bpo-35173</a>: “Re-use already existing functionality to allow Python 2.7.x (both embedded and standalone) to locate the module path according to the shared library”</li> </ul> </section> <section id="discussions"> <h2><a class="toc-backref" href="#discussions" role="doc-backlink">Discussions</a></h2> <ul class="simple"> <li>May 2019:<ul> <li><a class="reference external" href="https://mail.python.org/pipermail/python-dev/2019-May/157492.html">[Python-Dev] PEP 587 “Python Initialization Configuration” version 4</a></li> <li><a class="reference external" href="https://mail.python.org/pipermail/python-dev/2019-May/157435.html">[Python-Dev] RFC: PEP 587 “Python Initialization Configuration”: 3rd version</a></li> <li><a class="reference external" href="https://mail.python.org/pipermail/python-dev/2019-May/157385.html">Study on applications embedding Python</a></li> <li><a class="reference external" href="https://mail.python.org/pipermail/python-dev/2019-May/157290.html">[Python-Dev] RFC: PEP 587 “Python Initialization Configuration”: 2nd version</a></li> </ul> </li> <li>March 2019:<ul> <li><a class="reference external" href="https://mail.python.org/pipermail/python-dev/2019-March/156892.html">[Python-Dev] PEP 587: Python Initialization Configuration</a></li> <li><a class="reference external" href="https://mail.python.org/pipermail/python-dev/2019-March/156884.html">[Python-Dev] New Python Initialization API</a></li> </ul> </li> <li>February 2019:<ul> <li><a class="reference external" href="https://discuss.python.org/t/adding-char-based-apis-for-unix/916">Adding char* based APIs for Unix</a></li> </ul> </li> <li>July-August 2018:<ul> <li>July: <a class="reference external" href="https://mail.python.org/pipermail/python-dev/2018-July/154882.html">[Python-Dev] New _Py_InitializeFromConfig() function (PEP 432)</a></li> <li>August: <a class="reference external" href="https://mail.python.org/pipermail/python-dev/2018-August/154896.html">[Python-Dev] New _Py_InitializeFromConfig() function (PEP 432)</a></li> </ul> </li> </ul> </section> <section id="version-history"> <h2><a class="toc-backref" href="#version-history" role="doc-backlink">Version History</a></h2> <ul class="simple"> <li>Version 5:<ul> <li>Rename <code class="docutils literal notranslate"><span class="pre">PyInitError</span></code> to <code class="docutils literal notranslate"><span class="pre">PyStatus</span></code></li> <li>Rename <code class="docutils literal notranslate"><span class="pre">PyInitError_Failed()</span></code> to <code class="docutils literal notranslate"><span class="pre">PyStatus_Exception()</span></code></li> <li>Rename <code class="docutils literal notranslate"><span class="pre">Py_ExitInitError()</span></code> to <code class="docutils literal notranslate"><span class="pre">Py_ExitStatusException()</span></code></li> <li>Add <code class="docutils literal notranslate"><span class="pre">PyPreConfig._config_init</span></code> private field.</li> <li>Fix Python Configuration default values: isolated=0 and use_environment=1, instead of -1.</li> <li>Add “Multi-Phase Initialization Private Provisional API” and “Discussions” sections</li> </ul> </li> <li>Version 4:<ul> <li>Introduce “Python Configuration” and “Isolated Configuration” which are well better defined. Replace all macros with functions.</li> <li>Replace <code class="docutils literal notranslate"><span class="pre">PyPreConfig_INIT</span></code> and <code class="docutils literal notranslate"><span class="pre">PyConfig_INIT</span></code> macros with functions:<ul> <li><code class="docutils literal notranslate"><span class="pre">PyPreConfig_InitIsolatedConfig()</span></code>, <code class="docutils literal notranslate"><span class="pre">PyConfig_InitIsolatedConfig()</span></code></li> <li><code class="docutils literal notranslate"><span class="pre">PyPreConfig_InitPythonConfig()</span></code>, <code class="docutils literal notranslate"><span class="pre">PyConfig_InitPythonConfig()</span></code></li> </ul> </li> <li><code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> no longer uses dynamic memory, the <code class="docutils literal notranslate"><span class="pre">allocator</span></code> field type becomes an int, add <code class="docutils literal notranslate"><span class="pre">configure_locale</span></code> and <code class="docutils literal notranslate"><span class="pre">parse_argv</span></code> field.</li> <li><code class="docutils literal notranslate"><span class="pre">PyConfig</span></code>: rename <code class="docutils literal notranslate"><span class="pre">module_search_path_env</span></code> to <code class="docutils literal notranslate"><span class="pre">pythonpath_env</span></code>, rename <code class="docutils literal notranslate"><span class="pre">use_module_search_paths</span></code> to <code class="docutils literal notranslate"><span class="pre">module_search_paths_set</span></code>, remove <code class="docutils literal notranslate"><span class="pre">program</span></code> and <code class="docutils literal notranslate"><span class="pre">dll_path</span></code>.</li> <li>Replace <code class="docutils literal notranslate"><span class="pre">Py_INIT_xxx()</span></code> macros with <code class="docutils literal notranslate"><span class="pre">PyInitError_xxx()</span></code> functions.</li> <li>Remove the “Constant PyConfig” section. Remove <code class="docutils literal notranslate"><span class="pre">Py_InitializeFromArgs()</span></code> and <code class="docutils literal notranslate"><span class="pre">Py_InitializeFromBytesArgs()</span></code> functions.</li> </ul> </li> <li>Version 3:<ul> <li><code class="docutils literal notranslate"><span class="pre">PyConfig</span></code>: Add <code class="docutils literal notranslate"><span class="pre">configure_c_stdio</span></code> and <code class="docutils literal notranslate"><span class="pre">parse_argv</span></code>; rename <code class="docutils literal notranslate"><span class="pre">_frozen</span></code> to <code class="docutils literal notranslate"><span class="pre">pathconfig_warnings</span></code>.</li> <li>Rename functions using bytes strings and wide character strings. For example, <code class="docutils literal notranslate"><span class="pre">Py_PreInitializeFromWideArgs()</span></code> becomes <code class="docutils literal notranslate"><span class="pre">Py_PreInitializeFromArgs()</span></code>, and <code class="docutils literal notranslate"><span class="pre">PyConfig_SetArgv()</span></code> becomes <code class="docutils literal notranslate"><span class="pre">PyConfig_SetBytesArgv()</span></code>.</li> <li>Add <code class="docutils literal notranslate"><span class="pre">PyWideStringList_Insert()</span></code> function.</li> <li>New “Path configuration”, “Isolate Python”, “Python Issues” and “Version History” sections.</li> <li><code class="docutils literal notranslate"><span class="pre">PyConfig_SetString()</span></code> and <code class="docutils literal notranslate"><span class="pre">PyConfig_SetBytesString()</span></code> now requires the configuration as the first argument.</li> <li>Rename <code class="docutils literal notranslate"><span class="pre">Py_UnixMain()</span></code> to <code class="docutils literal notranslate"><span class="pre">Py_BytesMain()</span></code></li> </ul> </li> <li>Version 2: Add <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> methods (ex: <code class="docutils literal notranslate"><span class="pre">PyConfig_Read()</span></code>), add <code class="docutils literal notranslate"><span class="pre">PyWideStringList_Append()</span></code>, rename <code class="docutils literal notranslate"><span class="pre">PyWideCharList</span></code> to <code class="docutils literal notranslate"><span class="pre">PyWideStringList</span></code>.</li> <li>Version 1: Initial version.</li> </ul> </section> <section id="acceptance"> <h2><a class="toc-backref" href="#acceptance" role="doc-backlink">Acceptance</a></h2> <p><a class="pep reference internal" href="../pep-0587/" title="PEP 587 – Python Initialization Configuration">PEP 587</a> was <a class="reference external" href="https://mail.python.org/pipermail/python-dev/2019-May/157721.html">accepted by Thomas Wouters on May 26, 2019</a>.</p> </section> <section id="copyright"> <h2><a class="toc-backref" href="#copyright" role="doc-backlink">Copyright</a></h2> <p>This document has been placed in the public domain.</p> </section> </section> <hr class="docutils" /> <p>Source: <a class="reference external" href="https://github.com/python/peps/blob/main/peps/pep-0587.rst">https://github.com/python/peps/blob/main/peps/pep-0587.rst</a></p> <p>Last modified: <a class="reference external" href="https://github.com/python/peps/commits/main/peps/pep-0587.rst">2025-02-01 08:55:40 GMT</a></p> </article> <nav id="pep-sidebar"> <h2>Contents</h2> <ul> <li><a class="reference internal" href="#abstract">Abstract</a></li> <li><a class="reference internal" href="#rationale">Rationale</a></li> <li><a class="reference internal" href="#python-initialization-c-api">Python Initialization C API</a><ul> <li><a class="reference internal" href="#pywidestringlist">PyWideStringList</a></li> <li><a class="reference internal" href="#pystatus">PyStatus</a></li> <li><a class="reference internal" href="#preinitialization-with-pypreconfig">Preinitialization with PyPreConfig</a></li> <li><a class="reference internal" href="#initialization-with-pyconfig">Initialization with PyConfig</a></li> <li><a class="reference internal" href="#isolated-configuration">Isolated Configuration</a></li> <li><a class="reference internal" href="#python-configuration">Python Configuration</a></li> <li><a class="reference internal" href="#path-configuration">Path Configuration</a></li> <li><a class="reference internal" href="#py-bytesmain">Py_BytesMain()</a></li> <li><a class="reference internal" href="#py-runmain">Py_RunMain()</a></li> <li><a class="reference internal" href="#multi-phase-initialization-private-provisional-api">Multi-Phase Initialization Private Provisional API</a></li> </ul> </li> <li><a class="reference internal" href="#backwards-compatibility">Backwards Compatibility</a></li> <li><a class="reference internal" href="#annexes">Annexes</a><ul> <li><a class="reference internal" href="#comparison-of-python-and-isolated-configurations">Comparison of Python and Isolated Configurations</a></li> <li><a class="reference internal" href="#priority-and-rules">Priority and Rules</a></li> <li><a class="reference internal" href="#configuration-files">Configuration Files</a></li> <li><a class="reference internal" href="#global-configuration-variables">Global Configuration Variables</a></li> <li><a class="reference internal" href="#command-line-arguments">Command Line Arguments</a></li> <li><a class="reference internal" href="#x-options">-X Options</a></li> <li><a class="reference internal" href="#environment-variables">Environment Variables</a></li> <li><a class="reference internal" href="#default-python-configuration">Default Python Configuration</a></li> <li><a class="reference internal" href="#default-isolated-configuration">Default Isolated Configuration</a></li> <li><a class="reference internal" href="#python-3-7-api">Python 3.7 API</a></li> </ul> </li> <li><a class="reference internal" href="#python-issues">Python Issues</a></li> <li><a class="reference internal" href="#discussions">Discussions</a></li> <li><a class="reference internal" href="#version-history">Version History</a></li> <li><a class="reference internal" href="#acceptance">Acceptance</a></li> <li><a class="reference internal" href="#copyright">Copyright</a></li> </ul> <br> <a id="source" href="https://github.com/python/peps/blob/main/peps/pep-0587.rst">Page Source (GitHub)</a> </nav> </section> <script src="../_static/colour_scheme.js"></script> <script src="../_static/wrap_tables.js"></script> <script src="../_static/sticky_banner.js"></script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10