1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
<span id="using-on-general"></span><h1> Command line and environment</h1> <p>The CPython interpreter scans the command line and the environment for various settings.</p> <div class="impl-detail compound"> <p><strong>CPython implementation detail:</strong> Other implementations’ command line schemes may differ. See <a class="reference internal" href="../reference/introduction#implementations"><span class="std std-ref">Alternate Implementations</span></a> for further resources.</p> </div> <section id="command-line"> <span id="using-on-cmdline"></span><h2>
<span class="section-number">1.1. </span>Command line</h2> <p>When invoking Python, you may specify any of these options:</p> <pre data-language="sh">python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]
</pre> <p>The most common use case is, of course, a simple invocation of a script:</p> <pre data-language="sh">python myscript.py
</pre> <section id="interface-options"> <span id="using-on-interface-options"></span><h3>
<span class="section-number">1.1.1. </span>Interface options</h3> <p>The interpreter interface resembles that of the UNIX shell, but provides some additional methods of invocation:</p> <ul class="simple"> <li>When called with standard input connected to a tty device, it prompts for commands and executes them until an EOF (an end-of-file character, you can produce that with <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Ctrl</kbd>-<kbd class="kbd docutils literal notranslate">D</kbd></kbd> on UNIX or <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Ctrl</kbd>-<kbd class="kbd docutils literal notranslate">Z,</kbd> <kbd class="kbd docutils literal notranslate">Enter</kbd></kbd> on Windows) is read.</li> <li>When called with a file name argument or with a file as standard input, it reads and executes a script from that file.</li> <li>When called with a directory name argument, it reads and executes an appropriately named script from that directory.</li> <li>When called with <code>-c command</code>, it executes the Python statement(s) given as <em>command</em>. Here <em>command</em> may contain multiple statements separated by newlines. Leading whitespace is significant in Python statements!</li> <li>When called with <code>-m module-name</code>, the given module is located on the Python module path and executed as a script.</li> </ul> <p>In non-interactive mode, the entire input is parsed before it is executed.</p> <p>An interface option terminates the list of options consumed by the interpreter, all consecutive arguments will end up in <a class="reference internal" href="../library/sys#sys.argv" title="sys.argv"><code>sys.argv</code></a> – note that the first element, subscript zero (<code>sys.argv[0]</code>), is a string reflecting the program’s source.</p> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-c">
<code>-c <command></code> </dt> <dd>
<p>Execute the Python code in <em>command</em>. <em>command</em> can be one or more statements separated by newlines, with significant leading whitespace as in normal module code.</p> <p>If this option is given, the first element of <a class="reference internal" href="../library/sys#sys.argv" title="sys.argv"><code>sys.argv</code></a> will be <code>"-c"</code> and the current directory will be added to the start of <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a> (allowing modules in that directory to be imported as top level modules).</p> <p class="audit-hook">Raises an <a class="reference internal" href="../library/sys#auditing"><span class="std std-ref">auditing event</span></a> <code>cpython.run_command</code> with argument <code>command</code>.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-m">
<code>-m <module-name></code> </dt> <dd>
<p>Search <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a> for the named module and execute its contents as the <a class="reference internal" href="../library/__main__#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code>__main__</code></a> module.</p> <p>Since the argument is a <em>module</em> name, you must not give a file extension (<code>.py</code>). The module name should be a valid absolute Python module name, but the implementation may not always enforce this (e.g. it may allow you to use a name that includes a hyphen).</p> <p>Package names (including namespace packages) are also permitted. When a package name is supplied instead of a normal module, the interpreter will execute <code><pkg>.__main__</code> as the main module. This behaviour is deliberately similar to the handling of directories and zipfiles that are passed to the interpreter as the script argument.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This option cannot be used with built-in modules and extension modules written in C, since they do not have Python module files. However, it can still be used for precompiled modules, even if the original source file is not available.</p> </div> <p>If this option is given, the first element of <a class="reference internal" href="../library/sys#sys.argv" title="sys.argv"><code>sys.argv</code></a> will be the full path to the module file (while the module file is being located, the first element will be set to <code>"-m"</code>). As with the <a class="reference internal" href="#cmdoption-c"><code>-c</code></a> option, the current directory will be added to the start of <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a>.</p> <p><a class="reference internal" href="#cmdoption-I"><code>-I</code></a> option can be used to run the script in isolated mode where <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a> contains neither the current directory nor the user’s site-packages directory. All <code>PYTHON*</code> environment variables are ignored, too.</p> <p>Many standard library modules contain code that is invoked on their execution as a script. An example is the <a class="reference internal" href="../library/timeit#module-timeit" title="timeit: Measure the execution time of small code snippets."><code>timeit</code></a> module:</p> <pre data-language="sh">python -m timeit -s "setup here" "benchmarked code here"
python -m timeit -h # for details
</pre> <p class="audit-hook">Raises an <a class="reference internal" href="../library/sys#auditing"><span class="std std-ref">auditing event</span></a> <code>cpython.run_module</code> with argument <code>module-name</code>.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt>
<code></code> <a class="reference internal" href="../library/runpy#runpy.run_module" title="runpy.run_module"><code>runpy.run_module()</code></a>
</dt>
<dd>
<p>Equivalent functionality directly available to Python code</p> </dd> </dl> <p><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0338/"><strong>PEP 338</strong></a> – Executing modules as scripts</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.1: </span>Supply the package name to run a <code>__main__</code> submodule.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>namespace packages are also supported</p> </div> </dd>
</dl> <span class="target" id="cmdarg-dash"></span><dl class="describe"> <dt class="sig sig-object"> <span class="sig-name descname">-</span>
</dt> <dd>
<p>Read commands from standard input (<a class="reference internal" href="../library/sys#sys.stdin" title="sys.stdin"><code>sys.stdin</code></a>). If standard input is a terminal, <a class="reference internal" href="#cmdoption-i"><code>-i</code></a> is implied.</p> <p>If this option is given, the first element of <a class="reference internal" href="../library/sys#sys.argv" title="sys.argv"><code>sys.argv</code></a> will be <code>"-"</code> and the current directory will be added to the start of <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a>.</p> <p class="audit-hook" id="audit_event_cpython_run_stdin_0">Raises an <a class="reference internal" href="../library/sys#auditing"><span class="std std-ref">auditing event</span></a> <code>cpython.run_stdin</code> with no arguments.</p> </dd>
</dl> <span class="target" id="cmdarg-script"></span><dl class="describe"> <dt class="sig sig-object"> <span class="sig-name descname"><script></span>
</dt> <dd>
<p>Execute the Python code contained in <em>script</em>, which must be a filesystem path (absolute or relative) referring to either a Python file, a directory containing a <code>__main__.py</code> file, or a zipfile containing a <code>__main__.py</code> file.</p> <p>If this option is given, the first element of <a class="reference internal" href="../library/sys#sys.argv" title="sys.argv"><code>sys.argv</code></a> will be the script name as given on the command line.</p> <p>If the script name refers directly to a Python file, the directory containing that file is added to the start of <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a>, and the file is executed as the <a class="reference internal" href="../library/__main__#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code>__main__</code></a> module.</p> <p>If the script name refers to a directory or zipfile, the script name is added to the start of <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a> and the <code>__main__.py</code> file in that location is executed as the <a class="reference internal" href="../library/__main__#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code>__main__</code></a> module.</p> <p><a class="reference internal" href="#cmdoption-I"><code>-I</code></a> option can be used to run the script in isolated mode where <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a> contains neither the script’s directory nor the user’s site-packages directory. All <code>PYTHON*</code> environment variables are ignored, too.</p> <p class="audit-hook" id="audit_event_cpython_run_file_0">Raises an <a class="reference internal" href="../library/sys#auditing"><span class="std std-ref">auditing event</span></a> <code>cpython.run_file</code> with argument <code>filename</code>.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt>
<code></code> <a class="reference internal" href="../library/runpy#runpy.run_path" title="runpy.run_path"><code>runpy.run_path()</code></a>
</dt>
<dd>
<p>Equivalent functionality directly available to Python code</p> </dd> </dl> </div> </dd>
</dl> <p>If no interface option is given, <a class="reference internal" href="#cmdoption-i"><code>-i</code></a> is implied, <code>sys.argv[0]</code> is an empty string (<code>""</code>) and the current directory will be added to the start of <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a>. Also, tab-completion and history editing is automatically enabled, if available on your platform (see <a class="reference internal" href="../library/site#rlcompleter-config"><span class="std std-ref">Readline configuration</span></a>).</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="../tutorial/interpreter#tut-invoking"><span class="std std-ref">Invoking the Interpreter</span></a></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>Automatic enabling of tab-completion and history editing.</p> </div> </section> <section id="generic-options"> <span id="using-on-generic-options"></span><h3>
<span class="section-number">1.1.2. </span>Generic options</h3> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-0">
<code>-?</code> </dt> <dt class="sig sig-object std" id="cmdoption-h">
<code>-h</code> </dt> <dt class="sig sig-object std" id="cmdoption-help">
<code>--help</code> </dt> <dd>
<p>Print a short description of all command line options and corresponding environment variables and exit.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-help-env">
<code>--help-env</code> </dt> <dd>
<p>Print a short description of Python-specific environment variables and exit.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-help-xoptions">
<code>--help-xoptions</code> </dt> <dd>
<p>Print a description of implementation-specific <a class="reference internal" href="#cmdoption-X"><code>-X</code></a> options and exit.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-help-all">
<code>--help-all</code> </dt> <dd>
<p>Print complete usage information and exit.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-V">
<code>-V</code> </dt> <dt class="sig sig-object std" id="cmdoption-version">
<code>--version</code> </dt> <dd>
<p>Print the Python version number and exit. Example output could be:</p> <pre data-language="none">Python 3.8.0b2+
</pre> <p>When given twice, print more information about the build, like:</p> <pre data-language="none">Python 3.8.0b2+ (3.8:0c076caaa8, Apr 20 2019, 21:55:00)
[GCC 6.2.0 20161005]
</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6: </span>The <code>-VV</code> option.</p> </div> </dd>
</dl> </section> <section id="miscellaneous-options"> <span id="using-on-misc-options"></span><h3>
<span class="section-number">1.1.3. </span>Miscellaneous options</h3> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-b">
<code>-b</code> </dt> <dd>
<p>Issue a warning when comparing <a class="reference internal" href="../library/stdtypes#bytes" title="bytes"><code>bytes</code></a> or <a class="reference internal" href="../library/stdtypes#bytearray" title="bytearray"><code>bytearray</code></a> with <a class="reference internal" href="../library/stdtypes#str" title="str"><code>str</code></a> or <a class="reference internal" href="../library/stdtypes#bytes" title="bytes"><code>bytes</code></a> with <a class="reference internal" href="../library/functions#int" title="int"><code>int</code></a>. Issue an error when the option is given twice (<code>-bb</code>).</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Affects comparisons of <a class="reference internal" href="../library/stdtypes#bytes" title="bytes"><code>bytes</code></a> with <a class="reference internal" href="../library/functions#int" title="int"><code>int</code></a>.</p> </div> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-B">
<code>-B</code> </dt> <dd>
<p>If given, Python won’t try to write <code>.pyc</code> files on the import of source modules. See also <span class="target" id="index-1"></span><a class="reference internal" href="#envvar-PYTHONDONTWRITEBYTECODE"><code>PYTHONDONTWRITEBYTECODE</code></a>.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-check-hash-based-pycs">
<code>--check-hash-based-pycs default|always|never</code> </dt> <dd>
<p>Control the validation behavior of hash-based <code>.pyc</code> files. See <a class="reference internal" href="../reference/import#pyc-invalidation"><span class="std std-ref">Cached bytecode invalidation</span></a>. When set to <code>default</code>, checked and unchecked hash-based bytecode cache files are validated according to their default semantics. When set to <code>always</code>, all hash-based <code>.pyc</code> files, whether checked or unchecked, are validated against their corresponding source file. When set to <code>never</code>, hash-based <code>.pyc</code> files are not validated against their corresponding source files.</p> <p>The semantics of timestamp-based <code>.pyc</code> files are unaffected by this option.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-d">
<code>-d</code> </dt> <dd>
<p>Turn on parser debugging output (for expert only). See also the <span class="target" id="index-2"></span><a class="reference internal" href="#envvar-PYTHONDEBUG"><code>PYTHONDEBUG</code></a> environment variable.</p> <p>This option requires a <a class="reference internal" href="configure#debug-build"><span class="std std-ref">debug build of Python</span></a>, otherwise it’s ignored.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-E">
<code>-E</code> </dt> <dd>
<p>Ignore all <code>PYTHON*</code> environment variables, e.g. <span class="target" id="index-3"></span><a class="reference internal" href="#envvar-PYTHONPATH"><code>PYTHONPATH</code></a> and <span class="target" id="index-4"></span><a class="reference internal" href="#envvar-PYTHONHOME"><code>PYTHONHOME</code></a>, that might be set.</p> <p>See also the <a class="reference internal" href="#cmdoption-P"><code>-P</code></a> and <a class="reference internal" href="#cmdoption-I"><code>-I</code></a> (isolated) options.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-i">
<code>-i</code> </dt> <dd>
<p>When a script is passed as first argument or the <a class="reference internal" href="#cmdoption-c"><code>-c</code></a> option is used, enter interactive mode after executing the script or the command, even when <a class="reference internal" href="../library/sys#sys.stdin" title="sys.stdin"><code>sys.stdin</code></a> does not appear to be a terminal. The <span class="target" id="index-5"></span><a class="reference internal" href="#envvar-PYTHONSTARTUP"><code>PYTHONSTARTUP</code></a> file is not read.</p> <p>This can be useful to inspect global variables or a stack trace when a script raises an exception. See also <span class="target" id="index-6"></span><a class="reference internal" href="#envvar-PYTHONINSPECT"><code>PYTHONINSPECT</code></a>.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-I">
<code>-I</code> </dt> <dd>
<p>Run Python in isolated mode. This also implies <a class="reference internal" href="#cmdoption-E"><code>-E</code></a>, <a class="reference internal" href="#cmdoption-P"><code>-P</code></a> and <a class="reference internal" href="#cmdoption-s"><code>-s</code></a> options.</p> <p>In isolated mode <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a> contains neither the script’s directory nor the user’s site-packages directory. All <code>PYTHON*</code> environment variables are ignored, too. Further restrictions may be imposed to prevent the user from injecting malicious code.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-O">
<code>-O</code> </dt> <dd>
<p>Remove assert statements and any code conditional on the value of <a class="reference internal" href="../library/constants#debug__" title="__debug__"><code>__debug__</code></a>. Augment the filename for compiled (<a class="reference internal" href="../glossary#term-bytecode"><span class="xref std std-term">bytecode</span></a>) files by adding <code>.opt-1</code> before the <code>.pyc</code> extension (see <span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0488/"><strong>PEP 488</strong></a>). See also <span class="target" id="index-8"></span><a class="reference internal" href="#envvar-PYTHONOPTIMIZE"><code>PYTHONOPTIMIZE</code></a>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Modify <code>.pyc</code> filenames according to <span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-0488/"><strong>PEP 488</strong></a>.</p> </div> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-OO">
<code>-OO</code> </dt> <dd>
<p>Do <a class="reference internal" href="#cmdoption-O"><code>-O</code></a> and also discard docstrings. Augment the filename for compiled (<a class="reference internal" href="../glossary#term-bytecode"><span class="xref std std-term">bytecode</span></a>) files by adding <code>.opt-2</code> before the <code>.pyc</code> extension (see <span class="target" id="index-10"></span><a class="pep reference external" href="https://peps.python.org/pep-0488/"><strong>PEP 488</strong></a>).</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Modify <code>.pyc</code> filenames according to <span class="target" id="index-11"></span><a class="pep reference external" href="https://peps.python.org/pep-0488/"><strong>PEP 488</strong></a>.</p> </div> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-P">
<code>-P</code> </dt> <dd>
<p>Don’t prepend a potentially unsafe path to <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a>:</p> <ul class="simple"> <li>
<code>python -m module</code> command line: Don’t prepend the current working directory.</li> <li>
<code>python script.py</code> command line: Don’t prepend the script’s directory. If it’s a symbolic link, resolve symbolic links.</li> <li>
<code>python -c code</code> and <code>python</code> (REPL) command lines: Don’t prepend an empty string, which means the current working directory.</li> </ul> <p>See also the <span class="target" id="index-12"></span><a class="reference internal" href="#envvar-PYTHONSAFEPATH"><code>PYTHONSAFEPATH</code></a> environment variable, and <a class="reference internal" href="#cmdoption-E"><code>-E</code></a> and <a class="reference internal" href="#cmdoption-I"><code>-I</code></a> (isolated) options.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-q">
<code>-q</code> </dt> <dd>
<p>Don’t display the copyright and version messages even in interactive mode.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-R">
<code>-R</code> </dt> <dd>
<p>Turn on hash randomization. This option only has an effect if the <span class="target" id="index-13"></span><a class="reference internal" href="#envvar-PYTHONHASHSEED"><code>PYTHONHASHSEED</code></a> environment variable is set to <code>0</code>, since hash randomization is enabled by default.</p> <p>On previous versions of Python, this option turns on hash randomization, so that the <a class="reference internal" href="../reference/datamodel#object.__hash__" title="object.__hash__"><code>__hash__()</code></a> values of str and bytes objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python.</p> <p>Hash randomization is intended to provide protection against a denial-of-service caused by carefully chosen inputs that exploit the worst case performance of a dict construction, O(n<sup>2</sup>) complexity. See <a class="reference external" href="http://ocert.org/advisories/ocert-2011-003.html">http://ocert.org/advisories/ocert-2011-003.html</a> for details.</p> <p><span class="target" id="index-14"></span><a class="reference internal" href="#envvar-PYTHONHASHSEED"><code>PYTHONHASHSEED</code></a> allows you to set a fixed value for the hash seed secret.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>The option is no longer ignored.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.3.</span></p> </div> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-s">
<code>-s</code> </dt> <dd>
<p>Don’t add the <a class="reference internal" href="../library/site#site.USER_SITE" title="site.USER_SITE"><code>user site-packages directory</code></a> to <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a>.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><span class="target" id="index-15"></span><a class="pep reference external" href="https://peps.python.org/pep-0370/"><strong>PEP 370</strong></a> – Per user site-packages directory</p> </div> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-S">
<code>-S</code> </dt> <dd>
<p>Disable the import of the module <a class="reference internal" href="../library/site#module-site" title="site: Module responsible for site-specific configuration."><code>site</code></a> and the site-dependent manipulations of <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a> that it entails. Also disable these manipulations if <a class="reference internal" href="../library/site#module-site" title="site: Module responsible for site-specific configuration."><code>site</code></a> is explicitly imported later (call <a class="reference internal" href="../library/site#site.main" title="site.main"><code>site.main()</code></a> if you want them to be triggered).</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-u">
<code>-u</code> </dt> <dd>
<p>Force the stdout and stderr streams to be unbuffered. This option has no effect on the stdin stream.</p> <p>See also <span class="target" id="index-16"></span><a class="reference internal" href="#envvar-PYTHONUNBUFFERED"><code>PYTHONUNBUFFERED</code></a>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>The text layer of the stdout and stderr streams now is unbuffered.</p> </div> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-1">
<code>-v</code> </dt> <dd>
<p>Print a message each time a module is initialized, showing the place (filename or built-in module) from which it is loaded. When given twice (<code>-vv</code>), print a message for each file that is checked for when searching for a module. Also provides information on module cleanup at exit.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>The <a class="reference internal" href="../library/site#module-site" title="site: Module responsible for site-specific configuration."><code>site</code></a> module reports the site-specific paths and <code>.pth</code> files being processed.</p> </div> <p>See also <span class="target" id="index-17"></span><a class="reference internal" href="#envvar-PYTHONVERBOSE"><code>PYTHONVERBOSE</code></a>.</p> </dd>
</dl> <span class="target" id="using-on-warnings"></span><dl class="std option"> <dt class="sig sig-object std" id="cmdoption-W">
<code>-W arg</code> </dt> <dd>
<p>Warning control. Python’s warning machinery by default prints warning messages to <a class="reference internal" href="../library/sys#sys.stderr" title="sys.stderr"><code>sys.stderr</code></a>.</p> <p>The simplest settings apply a particular action unconditionally to all warnings emitted by a process (even those that are otherwise ignored by default):</p> <pre data-language="sh">-Wdefault # Warn once per call location
-Werror # Convert to exceptions
-Walways # Warn every time
-Wmodule # Warn once per calling module
-Wonce # Warn once per Python process
-Wignore # Never warn
</pre> <p>The action names can be abbreviated as desired and the interpreter will resolve them to the appropriate action name. For example, <code>-Wi</code> is the same as <code>-Wignore</code>.</p> <p>The full form of argument is:</p> <pre data-language="sh">action:message:category:module:lineno
</pre> <p>Empty fields match all values; trailing empty fields may be omitted. For example <code>-W ignore::DeprecationWarning</code> ignores all DeprecationWarning warnings.</p> <p>The <em>action</em> field is as explained above but only applies to warnings that match the remaining fields.</p> <p>The <em>message</em> field must match the whole warning message; this match is case-insensitive.</p> <p>The <em>category</em> field matches the warning category (ex: <code>DeprecationWarning</code>). This must be a class name; the match test whether the actual warning category of the message is a subclass of the specified warning category.</p> <p>The <em>module</em> field matches the (fully qualified) module name; this match is case-sensitive.</p> <p>The <em>lineno</em> field matches the line number, where zero matches all line numbers and is thus equivalent to an omitted line number.</p> <p>Multiple <a class="reference internal" href="#cmdoption-W"><code>-W</code></a> options can be given; when a warning matches more than one option, the action for the last matching option is performed. Invalid <a class="reference internal" href="#cmdoption-W"><code>-W</code></a> options are ignored (though, a warning message is printed about invalid options when the first warning is issued).</p> <p>Warnings can also be controlled using the <span class="target" id="index-18"></span><a class="reference internal" href="#envvar-PYTHONWARNINGS"><code>PYTHONWARNINGS</code></a> environment variable and from within a Python program using the <a class="reference internal" href="../library/warnings#module-warnings" title="warnings: Issue warning messages and control their disposition."><code>warnings</code></a> module. For example, the <a class="reference internal" href="../library/warnings#warnings.filterwarnings" title="warnings.filterwarnings"><code>warnings.filterwarnings()</code></a> function can be used to use a regular expression on the warning message.</p> <p>See <a class="reference internal" href="../library/warnings#warning-filter"><span class="std std-ref">The Warnings Filter</span></a> and <a class="reference internal" href="../library/warnings#describing-warning-filters"><span class="std std-ref">Describing Warning Filters</span></a> for more details.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-x">
<code>-x</code> </dt> <dd>
<p>Skip the first line of the source, allowing use of non-Unix forms of <code>#!cmd</code>. This is intended for a DOS specific hack only.</p> </dd>
</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-X">
<code>-X</code> </dt> <dd>
<p>Reserved for various implementation-specific options. CPython currently defines the following possible values:</p> <ul class="simple"> <li>
<code>-X faulthandler</code> to enable <a class="reference internal" href="../library/faulthandler#module-faulthandler" title="faulthandler: Dump the Python traceback."><code>faulthandler</code></a>. See also <span class="target" id="index-19"></span><a class="reference internal" href="#envvar-PYTHONFAULTHANDLER"><code>PYTHONFAULTHANDLER</code></a>.</li> <li>
<code>-X showrefcount</code> to output the total reference count and number of used memory blocks when the program finishes or after each statement in the interactive interpreter. This only works on <a class="reference internal" href="configure#debug-build"><span class="std std-ref">debug builds</span></a>.</li> <li>
<code>-X tracemalloc</code> to start tracing Python memory allocations using the <a class="reference internal" href="../library/tracemalloc#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code>tracemalloc</code></a> module. By default, only the most recent frame is stored in a traceback of a trace. Use <code>-X tracemalloc=NFRAME</code> to start tracing with a traceback limit of <em>NFRAME</em> frames. See <a class="reference internal" href="../library/tracemalloc#tracemalloc.start" title="tracemalloc.start"><code>tracemalloc.start()</code></a> and <span class="target" id="index-20"></span><a class="reference internal" href="#envvar-PYTHONTRACEMALLOC"><code>PYTHONTRACEMALLOC</code></a> for more information.</li> <li>
<code>-X int_max_str_digits</code> configures the <a class="reference internal" href="../library/stdtypes#int-max-str-digits"><span class="std std-ref">integer string conversion length limitation</span></a>. See also <span class="target" id="index-21"></span><a class="reference internal" href="#envvar-PYTHONINTMAXSTRDIGITS"><code>PYTHONINTMAXSTRDIGITS</code></a>.</li> <li>
<code>-X importtime</code> to show how long each import takes. It shows module name, cumulative time (including nested imports) and self time (excluding nested imports). Note that its output may be broken in multi-threaded application. Typical usage is <code>python3 -X importtime -c 'import
asyncio'</code>. See also <span class="target" id="index-22"></span><a class="reference internal" href="#envvar-PYTHONPROFILEIMPORTTIME"><code>PYTHONPROFILEIMPORTTIME</code></a>.</li> <li>
<code>-X dev</code>: enable <a class="reference internal" href="../library/devmode#devmode"><span class="std std-ref">Python Development Mode</span></a>, introducing additional runtime checks that are too expensive to be enabled by default.</li> <li>
<code>-X utf8</code> enables the <a class="reference internal" href="../library/os#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a>. <code>-X utf8=0</code> explicitly disables <a class="reference internal" href="../library/os#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> (even when it would otherwise activate automatically). See also <span class="target" id="index-23"></span><a class="reference internal" href="#envvar-PYTHONUTF8"><code>PYTHONUTF8</code></a>.</li> <li>
<code>-X pycache_prefix=PATH</code> enables writing <code>.pyc</code> files to a parallel tree rooted at the given directory instead of to the code tree. See also <span class="target" id="index-24"></span><a class="reference internal" href="#envvar-PYTHONPYCACHEPREFIX"><code>PYTHONPYCACHEPREFIX</code></a>.</li> <li>
<code>-X warn_default_encoding</code> issues a <a class="reference internal" href="../library/exceptions#EncodingWarning" title="EncodingWarning"><code>EncodingWarning</code></a> when the locale-specific default encoding is used for opening files. See also <span class="target" id="index-25"></span><a class="reference internal" href="#envvar-PYTHONWARNDEFAULTENCODING"><code>PYTHONWARNDEFAULTENCODING</code></a>.</li> <li>
<code>-X no_debug_ranges</code> disables the inclusion of the tables mapping extra location information (end line, start column offset and end column offset) to every instruction in code objects. This is useful when smaller code objects and pyc files are desired as well as suppressing the extra visual location indicators when the interpreter displays tracebacks. See also <span class="target" id="index-26"></span><a class="reference internal" href="#envvar-PYTHONNODEBUGRANGES"><code>PYTHONNODEBUGRANGES</code></a>.</li> <li>
<code>-X frozen_modules</code> determines whether or not frozen modules are ignored by the import machinery. A value of “on” means they get imported and “off” means they are ignored. The default is “on” if this is an installed Python (the normal case). If it’s under development (running from the source tree) then the default is “off”. Note that the “importlib_bootstrap” and “importlib_bootstrap_external” frozen modules are always used, even if this flag is set to “off”.</li> <li>
<code>-X perf</code> enables support for the Linux <code>perf</code> profiler. When this option is provided, the <code>perf</code> profiler will be able to report Python calls. This option is only available on some platforms and will do nothing if is not supported on the current system. The default value is “off”. See also <span class="target" id="index-27"></span><a class="reference internal" href="#envvar-PYTHONPERFSUPPORT"><code>PYTHONPERFSUPPORT</code></a> and <a class="reference internal" href="../howto/perf_profiling#perf-profiling"><span class="std std-ref">Python support for the Linux perf profiler</span></a>.</li> </ul> <p>It also allows passing arbitrary values and retrieving them through the <a class="reference internal" href="../library/sys#sys._xoptions" title="sys._xoptions"><code>sys._xoptions</code></a> dictionary.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>The <a class="reference internal" href="#cmdoption-X"><code>-X</code></a> option was added.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>The <code>-X faulthandler</code> option.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4: </span>The <code>-X showrefcount</code> and <code>-X tracemalloc</code> options.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6: </span>The <code>-X showalloccount</code> option.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7: </span>The <code>-X importtime</code>, <code>-X dev</code> and <code>-X utf8</code> options.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8: </span>The <code>-X pycache_prefix</code> option. The <code>-X dev</code> option now logs <code>close()</code> exceptions in <a class="reference internal" href="../library/io#io.IOBase" title="io.IOBase"><code>io.IOBase</code></a> destructor.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>Using <code>-X dev</code> option, check <em>encoding</em> and <em>errors</em> arguments on string encoding and decoding operations.</p> <p>The <code>-X showalloccount</code> option has been removed.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10: </span>The <code>-X warn_default_encoding</code> option. Removed the <code>-X oldparser</code> option.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11: </span>The <code>-X no_debug_ranges</code> option.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11: </span>The <code>-X frozen_modules</code> option.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11: </span>The <code>-X int_max_str_digits</code> option.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12: </span>The <code>-X perf</code> option.</p> </div> </dd>
</dl> </section> <section id="options-you-shouldn-t-use"> <h3>
<span class="section-number">1.1.4. </span>Options you shouldn’t use</h3> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-J">
<code>-J</code> </dt> <dd>
<p>Reserved for use by <a class="reference external" href="https://www.jython.org/">Jython</a>.</p> </dd>
</dl> </section> </section> <section id="environment-variables"> <span id="using-on-envvars"></span><h2>
<span class="section-number">1.2. </span>Environment variables</h2> <p>These environment variables influence Python’s behavior, they are processed before the command-line switches other than -E or -I. It is customary that command-line switches override environmental variables where there is a conflict.</p> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONHOME">
<code>PYTHONHOME</code> </dt> <dd>
<p>Change the location of the standard Python libraries. By default, the libraries are searched in <code><em>prefix</em>/lib/python<em>version</em></code> and <code><em>exec_prefix</em>/lib/python<em>version</em></code>, where <code><em>prefix</em></code> and <code><em>exec_prefix</em></code> are installation-dependent directories, both defaulting to <code>/usr/local</code>.</p> <p>When <span class="target" id="index-28"></span><a class="reference internal" href="#envvar-PYTHONHOME"><code>PYTHONHOME</code></a> is set to a single directory, its value replaces both <code><em>prefix</em></code> and <code><em>exec_prefix</em></code>. To specify different values for these, set <span class="target" id="index-29"></span><a class="reference internal" href="#envvar-PYTHONHOME"><code>PYTHONHOME</code></a> to <code><em>prefix</em>:<em>exec_prefix</em></code>.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONPATH">
<code>PYTHONPATH</code> </dt> <dd>
<p>Augment the default search path for module files. The format is the same as the shell’s <span class="target" id="index-30"></span><code>PATH</code>: one or more directory pathnames separated by <a class="reference internal" href="../library/os#os.pathsep" title="os.pathsep"><code>os.pathsep</code></a> (e.g. colons on Unix or semicolons on Windows). Non-existent directories are silently ignored.</p> <p>In addition to normal directories, individual <span class="target" id="index-31"></span><a class="reference internal" href="#envvar-PYTHONPATH"><code>PYTHONPATH</code></a> entries may refer to zipfiles containing pure Python modules (in either source or compiled form). Extension modules cannot be imported from zipfiles.</p> <p>The default search path is installation dependent, but generally begins with <code><em>prefix</em>/lib/python<em>version</em></code> (see <span class="target" id="index-32"></span><a class="reference internal" href="#envvar-PYTHONHOME"><code>PYTHONHOME</code></a> above). It is <em>always</em> appended to <span class="target" id="index-33"></span><a class="reference internal" href="#envvar-PYTHONPATH"><code>PYTHONPATH</code></a>.</p> <p>An additional directory will be inserted in the search path in front of <span class="target" id="index-34"></span><a class="reference internal" href="#envvar-PYTHONPATH"><code>PYTHONPATH</code></a> as described above under <a class="reference internal" href="#using-on-interface-options"><span class="std std-ref">Interface options</span></a>. The search path can be manipulated from within a Python program as the variable <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a>.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONSAFEPATH">
<code>PYTHONSAFEPATH</code> </dt> <dd>
<p>If this is set to a non-empty string, don’t prepend a potentially unsafe path to <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a>: see the <a class="reference internal" href="#cmdoption-P"><code>-P</code></a> option for details.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONPLATLIBDIR">
<code>PYTHONPLATLIBDIR</code> </dt> <dd>
<p>If this is set to a non-empty string, it overrides the <a class="reference internal" href="../library/sys#sys.platlibdir" title="sys.platlibdir"><code>sys.platlibdir</code></a> value.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.9.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONSTARTUP">
<code>PYTHONSTARTUP</code> </dt> <dd>
<p>If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session. You can also change the prompts <a class="reference internal" href="../library/sys#sys.ps1" title="sys.ps1"><code>sys.ps1</code></a> and <a class="reference internal" href="../library/sys#sys.ps2" title="sys.ps2"><code>sys.ps2</code></a> and the hook <a class="reference internal" href="../library/sys#sys.__interactivehook__" title="sys.__interactivehook__"><code>sys.__interactivehook__</code></a> in this file.</p> <p class="audit-hook"></p>
<p>Raises an <a class="reference internal" href="../library/sys#auditing"><span class="std std-ref">auditing event</span></a> <code>cpython.run_startup</code> with the filename as the argument when called on startup.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONOPTIMIZE">
<code>PYTHONOPTIMIZE</code> </dt> <dd>
<p>If this is set to a non-empty string it is equivalent to specifying the <a class="reference internal" href="#cmdoption-O"><code>-O</code></a> option. If set to an integer, it is equivalent to specifying <a class="reference internal" href="#cmdoption-O"><code>-O</code></a> multiple times.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONBREAKPOINT">
<code>PYTHONBREAKPOINT</code> </dt> <dd>
<p>If this is set, it names a callable using dotted-path notation. The module containing the callable will be imported and then the callable will be run by the default implementation of <a class="reference internal" href="../library/sys#sys.breakpointhook" title="sys.breakpointhook"><code>sys.breakpointhook()</code></a> which itself is called by built-in <a class="reference internal" href="../library/functions#breakpoint" title="breakpoint"><code>breakpoint()</code></a>. If not set, or set to the empty string, it is equivalent to the value “pdb.set_trace”. Setting this to the string “0” causes the default implementation of <a class="reference internal" href="../library/sys#sys.breakpointhook" title="sys.breakpointhook"><code>sys.breakpointhook()</code></a> to do nothing but return immediately.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONDEBUG">
<code>PYTHONDEBUG</code> </dt> <dd>
<p>If this is set to a non-empty string it is equivalent to specifying the <a class="reference internal" href="#cmdoption-d"><code>-d</code></a> option. If set to an integer, it is equivalent to specifying <a class="reference internal" href="#cmdoption-d"><code>-d</code></a> multiple times.</p> <p>This environment variable requires a <a class="reference internal" href="configure#debug-build"><span class="std std-ref">debug build of Python</span></a>, otherwise it’s ignored.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONINSPECT">
<code>PYTHONINSPECT</code> </dt> <dd>
<p>If this is set to a non-empty string it is equivalent to specifying the <a class="reference internal" href="#cmdoption-i"><code>-i</code></a> option.</p> <p>This variable can also be modified by Python code using <a class="reference internal" href="../library/os#os.environ" title="os.environ"><code>os.environ</code></a> to force inspect mode on program termination.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONUNBUFFERED">
<code>PYTHONUNBUFFERED</code> </dt> <dd>
<p>If this is set to a non-empty string it is equivalent to specifying the <a class="reference internal" href="#cmdoption-u"><code>-u</code></a> option.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONVERBOSE">
<code>PYTHONVERBOSE</code> </dt> <dd>
<p>If this is set to a non-empty string it is equivalent to specifying the <a class="reference internal" href="#cmdoption-1"><code>-v</code></a> option. If set to an integer, it is equivalent to specifying <a class="reference internal" href="#cmdoption-1"><code>-v</code></a> multiple times.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONCASEOK">
<code>PYTHONCASEOK</code> </dt> <dd>
<p>If this is set, Python ignores case in <a class="reference internal" href="../reference/simple_stmts#import"><code>import</code></a> statements. This only works on Windows and macOS.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONDONTWRITEBYTECODE">
<code>PYTHONDONTWRITEBYTECODE</code> </dt> <dd>
<p>If this is set to a non-empty string, Python won’t try to write <code>.pyc</code> files on the import of source modules. This is equivalent to specifying the <a class="reference internal" href="#cmdoption-B"><code>-B</code></a> option.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONPYCACHEPREFIX">
<code>PYTHONPYCACHEPREFIX</code> </dt> <dd>
<p>If this is set, Python will write <code>.pyc</code> files in a mirror directory tree at this path, instead of in <code>__pycache__</code> directories within the source tree. This is equivalent to specifying the <a class="reference internal" href="#cmdoption-X"><code>-X</code></a> <code>pycache_prefix=PATH</code> option.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONHASHSEED">
<code>PYTHONHASHSEED</code> </dt> <dd>
<p>If this variable is not set or set to <code>random</code>, a random value is used to seed the hashes of str and bytes objects.</p> <p>If <span class="target" id="index-35"></span><a class="reference internal" href="#envvar-PYTHONHASHSEED"><code>PYTHONHASHSEED</code></a> is set to an integer value, it is used as a fixed seed for generating the hash() of the types covered by the hash randomization.</p> <p>Its purpose is to allow repeatable hashing, such as for selftests for the interpreter itself, or to allow a cluster of python processes to share hash values.</p> <p>The integer must be a decimal number in the range [0,4294967295]. Specifying the value 0 will disable hash randomization.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.3.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONINTMAXSTRDIGITS">
<code>PYTHONINTMAXSTRDIGITS</code> </dt> <dd>
<p>If this variable is set to an integer, it is used to configure the interpreter’s global <a class="reference internal" href="../library/stdtypes#int-max-str-digits"><span class="std std-ref">integer string conversion length limitation</span></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONIOENCODING">
<code>PYTHONIOENCODING</code> </dt> <dd>
<p>If this is set before running the interpreter, it overrides the encoding used for stdin/stdout/stderr, in the syntax <code>encodingname:errorhandler</code>. Both the <code>encodingname</code> and the <code>:errorhandler</code> parts are optional and have the same meaning as in <a class="reference internal" href="../library/stdtypes#str.encode" title="str.encode"><code>str.encode()</code></a>.</p> <p>For stderr, the <code>:errorhandler</code> part is ignored; the handler will always be <code>'backslashreplace'</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>The <code>encodingname</code> part is now optional.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>On Windows, the encoding specified by this variable is ignored for interactive console buffers unless <span class="target" id="index-36"></span><a class="reference internal" href="#envvar-PYTHONLEGACYWINDOWSSTDIO"><code>PYTHONLEGACYWINDOWSSTDIO</code></a> is also specified. Files and pipes redirected through the standard streams are not affected.</p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONNOUSERSITE">
<code>PYTHONNOUSERSITE</code> </dt> <dd>
<p>If this is set, Python won’t add the <a class="reference internal" href="../library/site#site.USER_SITE" title="site.USER_SITE"><code>user site-packages directory</code></a> to <a class="reference internal" href="../library/sys#sys.path" title="sys.path"><code>sys.path</code></a>.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><span class="target" id="index-37"></span><a class="pep reference external" href="https://peps.python.org/pep-0370/"><strong>PEP 370</strong></a> – Per user site-packages directory</p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONUSERBASE">
<code>PYTHONUSERBASE</code> </dt> <dd>
<p>Defines the <a class="reference internal" href="../library/site#site.USER_BASE" title="site.USER_BASE"><code>user base directory</code></a>, which is used to compute the path of the <a class="reference internal" href="../library/site#site.USER_SITE" title="site.USER_SITE"><code>user site-packages directory</code></a> and <a class="reference internal" href="../library/sysconfig#sysconfig-user-scheme"><span class="std std-ref">installation paths</span></a> for <code>python -m pip install --user</code>.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><span class="target" id="index-38"></span><a class="pep reference external" href="https://peps.python.org/pep-0370/"><strong>PEP 370</strong></a> – Per user site-packages directory</p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONEXECUTABLE">
<code>PYTHONEXECUTABLE</code> </dt> <dd>
<p>If this environment variable is set, <code>sys.argv[0]</code> will be set to its value instead of the value got through the C runtime. Only works on macOS.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONWARNINGS">
<code>PYTHONWARNINGS</code> </dt> <dd>
<p>This is equivalent to the <a class="reference internal" href="#cmdoption-W"><code>-W</code></a> option. If set to a comma separated string, it is equivalent to specifying <a class="reference internal" href="#cmdoption-W"><code>-W</code></a> multiple times, with filters later in the list taking precedence over those earlier in the list.</p> <p>The simplest settings apply a particular action unconditionally to all warnings emitted by a process (even those that are otherwise ignored by default):</p> <pre data-language="sh">PYTHONWARNINGS=default # Warn once per call location
PYTHONWARNINGS=error # Convert to exceptions
PYTHONWARNINGS=always # Warn every time
PYTHONWARNINGS=module # Warn once per calling module
PYTHONWARNINGS=once # Warn once per Python process
PYTHONWARNINGS=ignore # Never warn
</pre> <p>See <a class="reference internal" href="../library/warnings#warning-filter"><span class="std std-ref">The Warnings Filter</span></a> and <a class="reference internal" href="../library/warnings#describing-warning-filters"><span class="std std-ref">Describing Warning Filters</span></a> for more details.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONFAULTHANDLER">
<code>PYTHONFAULTHANDLER</code> </dt> <dd>
<p>If this environment variable is set to a non-empty string, <a class="reference internal" href="../library/faulthandler#faulthandler.enable" title="faulthandler.enable"><code>faulthandler.enable()</code></a> is called at startup: install a handler for <a class="reference internal" href="../library/signal#signal.SIGSEGV" title="signal.SIGSEGV"><code>SIGSEGV</code></a>, <a class="reference internal" href="../library/signal#signal.SIGFPE" title="signal.SIGFPE"><code>SIGFPE</code></a>, <a class="reference internal" href="../library/signal#signal.SIGABRT" title="signal.SIGABRT"><code>SIGABRT</code></a>, <a class="reference internal" href="../library/signal#signal.SIGBUS" title="signal.SIGBUS"><code>SIGBUS</code></a> and <a class="reference internal" href="../library/signal#signal.SIGILL" title="signal.SIGILL"><code>SIGILL</code></a> signals to dump the Python traceback. This is equivalent to <a class="reference internal" href="#cmdoption-X"><code>-X</code></a> <code>faulthandler</code> option.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONTRACEMALLOC">
<code>PYTHONTRACEMALLOC</code> </dt> <dd>
<p>If this environment variable is set to a non-empty string, start tracing Python memory allocations using the <a class="reference internal" href="../library/tracemalloc#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code>tracemalloc</code></a> module. The value of the variable is the maximum number of frames stored in a traceback of a trace. For example, <code>PYTHONTRACEMALLOC=1</code> stores only the most recent frame. See the <a class="reference internal" href="../library/tracemalloc#tracemalloc.start" title="tracemalloc.start"><code>tracemalloc.start()</code></a> function for more information. This is equivalent to setting the <a class="reference internal" href="#cmdoption-X"><code>-X</code></a> <code>tracemalloc</code> option.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONPROFILEIMPORTTIME">
<code>PYTHONPROFILEIMPORTTIME</code> </dt> <dd>
<p>If this environment variable is set to a non-empty string, Python will show how long each import takes. This is equivalent to setting the <a class="reference internal" href="#cmdoption-X"><code>-X</code></a> <code>importtime</code> option.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONASYNCIODEBUG">
<code>PYTHONASYNCIODEBUG</code> </dt> <dd>
<p>If this environment variable is set to a non-empty string, enable the <a class="reference internal" href="../library/asyncio-dev#asyncio-debug-mode"><span class="std std-ref">debug mode</span></a> of the <a class="reference internal" href="../library/asyncio#module-asyncio" title="asyncio: Asynchronous I/O."><code>asyncio</code></a> module.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONMALLOC">
<code>PYTHONMALLOC</code> </dt> <dd>
<p>Set the Python memory allocators and/or install debug hooks.</p> <p>Set the family of memory allocators used by Python:</p> <ul class="simple"> <li>
<code>default</code>: use the <a class="reference internal" href="../c-api/memory#default-memory-allocators"><span class="std std-ref">default memory allocators</span></a>.</li> <li>
<code>malloc</code>: use the <code>malloc()</code> function of the C library for all domains (<a class="reference internal" href="../c-api/memory#c.PYMEM_DOMAIN_RAW" title="PYMEM_DOMAIN_RAW"><code>PYMEM_DOMAIN_RAW</code></a>, <a class="reference internal" href="../c-api/memory#c.PYMEM_DOMAIN_MEM" title="PYMEM_DOMAIN_MEM"><code>PYMEM_DOMAIN_MEM</code></a>, <a class="reference internal" href="../c-api/memory#c.PYMEM_DOMAIN_OBJ" title="PYMEM_DOMAIN_OBJ"><code>PYMEM_DOMAIN_OBJ</code></a>).</li> <li>
<code>pymalloc</code>: use the <a class="reference internal" href="../c-api/memory#pymalloc"><span class="std std-ref">pymalloc allocator</span></a> for <a class="reference internal" href="../c-api/memory#c.PYMEM_DOMAIN_MEM" title="PYMEM_DOMAIN_MEM"><code>PYMEM_DOMAIN_MEM</code></a> and <a class="reference internal" href="../c-api/memory#c.PYMEM_DOMAIN_OBJ" title="PYMEM_DOMAIN_OBJ"><code>PYMEM_DOMAIN_OBJ</code></a> domains and use the <code>malloc()</code> function for the <a class="reference internal" href="../c-api/memory#c.PYMEM_DOMAIN_RAW" title="PYMEM_DOMAIN_RAW"><code>PYMEM_DOMAIN_RAW</code></a> domain.</li> </ul> <p>Install <a class="reference internal" href="../c-api/memory#pymem-debug-hooks"><span class="std std-ref">debug hooks</span></a>:</p> <ul class="simple"> <li>
<code>debug</code>: install debug hooks on top of the <a class="reference internal" href="../c-api/memory#default-memory-allocators"><span class="std std-ref">default memory allocators</span></a>.</li> <li>
<code>malloc_debug</code>: same as <code>malloc</code> but also install debug hooks.</li> <li>
<code>pymalloc_debug</code>: same as <code>pymalloc</code> but also install debug hooks.</li> </ul> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <code>"default"</code> allocator.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONMALLOCSTATS">
<code>PYTHONMALLOCSTATS</code> </dt> <dd>
<p>If set to a non-empty string, Python will print statistics of the <a class="reference internal" href="../c-api/memory#pymalloc"><span class="std std-ref">pymalloc memory allocator</span></a> every time a new pymalloc object arena is created, and on shutdown.</p> <p>This variable is ignored if the <span class="target" id="index-39"></span><a class="reference internal" href="#envvar-PYTHONMALLOC"><code>PYTHONMALLOC</code></a> environment variable is used to force the <code>malloc()</code> allocator of the C library, or if Python is configured without <code>pymalloc</code> support.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>This variable can now also be used on Python compiled in release mode. It now has no effect if set to an empty string.</p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONLEGACYWINDOWSFSENCODING">
<code>PYTHONLEGACYWINDOWSFSENCODING</code> </dt> <dd>
<p>If set to a non-empty string, the default <a class="reference internal" href="../glossary#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a> mode will revert to their pre-3.6 values of ‘mbcs’ and ‘replace’, respectively. Otherwise, the new defaults ‘utf-8’ and ‘surrogatepass’ are used.</p> <p>This may also be enabled at runtime with <a class="reference internal" href="../library/sys#sys._enablelegacywindowsfsencoding" title="sys._enablelegacywindowsfsencoding"><code>sys._enablelegacywindowsfsencoding()</code></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6: </span>See <span class="target" id="index-40"></span><a class="pep reference external" href="https://peps.python.org/pep-0529/"><strong>PEP 529</strong></a> for more details.</p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONLEGACYWINDOWSSTDIO">
<code>PYTHONLEGACYWINDOWSSTDIO</code> </dt> <dd>
<p>If set to a non-empty string, does not use the new console reader and writer. This means that Unicode characters will be encoded according to the active console code page, rather than using utf-8.</p> <p>This variable is ignored if the standard streams are redirected (to files or pipes) rather than referring to console buffers.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONCOERCECLOCALE">
<code>PYTHONCOERCECLOCALE</code> </dt> <dd>
<p>If set to the value <code>0</code>, causes the main Python command line application to skip coercing the legacy ASCII-based C and POSIX locales to a more capable UTF-8 based alternative.</p> <p>If this variable is <em>not</em> set (or is set to a value other than <code>0</code>), the <code>LC_ALL</code> locale override environment variable is also not set, and the current locale reported for the <code>LC_CTYPE</code> category is either the default <code>C</code> locale, or else the explicitly ASCII-based <code>POSIX</code> locale, then the Python CLI will attempt to configure the following locales for the <code>LC_CTYPE</code> category in the order listed before loading the interpreter runtime:</p> <ul class="simple"> <li><code>C.UTF-8</code></li> <li><code>C.utf8</code></li> <li><code>UTF-8</code></li> </ul> <p>If setting one of these locale categories succeeds, then the <code>LC_CTYPE</code> environment variable will also be set accordingly in the current process environment before the Python runtime is initialized. This ensures that in addition to being seen by both the interpreter itself and other locale-aware components running in the same process (such as the GNU <code>readline</code> library), the updated setting is also seen in subprocesses (regardless of whether or not those processes are running a Python interpreter), as well as in operations that query the environment rather than the current C locale (such as Python’s own <a class="reference internal" href="../library/locale#locale.getdefaultlocale" title="locale.getdefaultlocale"><code>locale.getdefaultlocale()</code></a>).</p> <p>Configuring one of these locales (either explicitly or via the above implicit locale coercion) automatically enables the <code>surrogateescape</code> <a class="reference internal" href="../library/codecs#error-handlers"><span class="std std-ref">error handler</span></a> for <a class="reference internal" href="../library/sys#sys.stdin" title="sys.stdin"><code>sys.stdin</code></a> and <a class="reference internal" href="../library/sys#sys.stdout" title="sys.stdout"><code>sys.stdout</code></a> (<a class="reference internal" href="../library/sys#sys.stderr" title="sys.stderr"><code>sys.stderr</code></a> continues to use <code>backslashreplace</code> as it does in any other locale). This stream handling behavior can be overridden using <span class="target" id="index-41"></span><a class="reference internal" href="#envvar-PYTHONIOENCODING"><code>PYTHONIOENCODING</code></a> as usual.</p> <p>For debugging purposes, setting <code>PYTHONCOERCECLOCALE=warn</code> will cause Python to emit warning messages on <code>stderr</code> if either the locale coercion activates, or else if a locale that <em>would</em> have triggered coercion is still active when the Python runtime is initialized.</p> <p>Also note that even when locale coercion is disabled, or when it fails to find a suitable target locale, <span class="target" id="index-42"></span><a class="reference internal" href="#envvar-PYTHONUTF8"><code>PYTHONUTF8</code></a> will still activate by default in legacy ASCII-based locales. Both features must be disabled in order to force the interpreter to use <code>ASCII</code> instead of <code>UTF-8</code> for system interfaces.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7: </span>See <span class="target" id="index-43"></span><a class="pep reference external" href="https://peps.python.org/pep-0538/"><strong>PEP 538</strong></a> for more details.</p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONDEVMODE">
<code>PYTHONDEVMODE</code> </dt> <dd>
<p>If this environment variable is set to a non-empty string, enable <a class="reference internal" href="../library/devmode#devmode"><span class="std std-ref">Python Development Mode</span></a>, introducing additional runtime checks that are too expensive to be enabled by default. This is equivalent to setting the <a class="reference internal" href="#cmdoption-X"><code>-X</code></a> <code>dev</code> option.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONUTF8">
<code>PYTHONUTF8</code> </dt> <dd>
<p>If set to <code>1</code>, enable the <a class="reference internal" href="../library/os#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a>.</p> <p>If set to <code>0</code>, disable the <a class="reference internal" href="../library/os#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a>.</p> <p>Setting any other non-empty string causes an error during interpreter initialisation.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONWARNDEFAULTENCODING">
<code>PYTHONWARNDEFAULTENCODING</code> </dt> <dd>
<p>If this environment variable is set to a non-empty string, issue a <a class="reference internal" href="../library/exceptions#EncodingWarning" title="EncodingWarning"><code>EncodingWarning</code></a> when the locale-specific default encoding is used.</p> <p>See <a class="reference internal" href="../library/io#io-encoding-warning"><span class="std std-ref">Opt-in EncodingWarning</span></a> for details.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONNODEBUGRANGES">
<code>PYTHONNODEBUGRANGES</code> </dt> <dd>
<p>If this variable is set, it disables the inclusion of the tables mapping extra location information (end line, start column offset and end column offset) to every instruction in code objects. This is useful when smaller code objects and pyc files are desired as well as suppressing the extra visual location indicators when the interpreter displays tracebacks.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONPERFSUPPORT">
<code>PYTHONPERFSUPPORT</code> </dt> <dd>
<p>If this variable is set to a nonzero value, it enables support for the Linux <code>perf</code> profiler so Python calls can be detected by it.</p> <p>If set to <code>0</code>, disable Linux <code>perf</code> profiler support.</p> <p>See also the <a class="reference internal" href="#cmdoption-X"><code>-X perf</code></a> command-line option and <a class="reference internal" href="../howto/perf_profiling#perf-profiling"><span class="std std-ref">Python support for the Linux perf profiler</span></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd>
</dl> <section id="debug-mode-variables"> <h3>
<span class="section-number">1.2.1. </span>Debug-mode variables</h3> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONDUMPREFS">
<code>PYTHONDUMPREFS</code> </dt> <dd>
<p>If set, Python will dump objects and reference counts still alive after shutting down the interpreter.</p> <p>Need Python configured with the <a class="reference internal" href="configure#cmdoption-with-trace-refs"><code>--with-trace-refs</code></a> build option.</p> </dd>
</dl> <dl class="std envvar"> <dt class="sig sig-object std" id="envvar-PYTHONDUMPREFSFILE-FILENAME">
<code>PYTHONDUMPREFSFILE=FILENAME</code> </dt> <dd>
<p>If set, Python will dump objects and reference counts still alive after shutting down the interpreter into a file called <em>FILENAME</em>.</p> <p>Need Python configured with the <a class="reference internal" href="configure#cmdoption-with-trace-refs"><code>--with-trace-refs</code></a> build option.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> </dd>
</dl> </section> </section> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/using/cmdline.html" class="_attribution-link">https://docs.python.org/3.12/using/cmdline.html</a>
</p>
</div>
|