1PYTHON(1) General Commands Manual PYTHON(1)
2
3
4
6 python - an interpreted, interactive, object-oriented programming lan‐
7 guage
8
10 python [ -B ] [ -b ] [ -d ] [ -E ] [ -h ] [ -i ] [ -I ]
11 [ -m module-name ] [ -q ] [ -O ] [ -OO ] [ -s ] [ -S ] [ -u ]
12 [ -v ] [ -V ] [ -W argument ] [ -x ] [ -X option ] [ -? ]
13 [ --check-hash-based-pycs default | always | never ]
14 [ -c command | script | - ] [ arguments ]
15
17 Python is an interpreted, interactive, object-oriented programming lan‐
18 guage that combines remarkable power with very clear syntax. For an
19 introduction to programming in Python, see the Python Tutorial. The
20 Python Library Reference documents built-in and standard types, con‐
21 stants, functions and modules. Finally, the Python Reference Manual
22 describes the syntax and semantics of the core language in (perhaps
23 too) much detail. (These documents may be located via the INTERNET RE‐
24 SOURCES below; they may be installed on your system as well.)
25
26 Python's basic power can be extended with your own modules written in C
27 or C++. On most systems such modules may be dynamically loaded.
28 Python is also adaptable as an extension language for existing applica‐
29 tions. See the internal documentation for hints.
30
31 Documentation for installed Python modules and packages can be viewed
32 by running the pydoc program.
33
35 -B Don't write .pyc files on import. See also PYTHONDONTWRITEBYTE‐
36 CODE.
37
38 -b Issue warnings about str(bytes_instance), str(bytearray_in‐
39 stance) and comparing bytes/bytearray with str. (-bb: issue er‐
40 rors)
41
42 -c command
43 Specify the command to execute (see next section). This termi‐
44 nates the option list (following options are passed as arguments
45 to the command).
46
47 --check-hash-based-pycs mode
48 Configure how Python evaluates the up-to-dateness of hash-based
49 .pyc files.
50
51 -d Turn on parser debugging output (for expert only, depending on
52 compilation options).
53
54 -E Ignore environment variables like PYTHONPATH and PYTHONHOME that
55 modify the behavior of the interpreter.
56
57 -h , -? , --help
58 Prints the usage for the interpreter executable and exits.
59
60 -i When a script is passed as first argument or the -c option is
61 used, enter interactive mode after executing the script or the
62 command. It does not read the $PYTHONSTARTUP file. This can be
63 useful to inspect global variables or a stack trace when a
64 script raises an exception.
65
66 -I Run Python in isolated mode. This also implies -E and -s. In
67 isolated mode sys.path contains neither the script's directory
68 nor the user's site-packages directory. All PYTHON* environment
69 variables are ignored, too. Further restrictions may be imposed
70 to prevent the user from injecting malicious code.
71
72 -m module-name
73 Searches sys.path for the named module and runs the correspond‐
74 ing .py file as a script. This terminates the option list (fol‐
75 lowing options are passed as arguments to the module).
76
77 -O Remove assert statements and any code conditional on the value
78 of __debug__; augment the filename for compiled (bytecode) files
79 by adding .opt-1 before the .pyc extension.
80
81 -OO Do -O and also discard docstrings; change the filename for com‐
82 piled (bytecode) files by adding .opt-2 before the .pyc exten‐
83 sion.
84
85 -q Do not print the version and copyright messages. These messages
86 are also suppressed in non-interactive mode.
87
88 -s Don't add user site directory to sys.path.
89
90 -S Disable the import of the module site and the site-dependent ma‐
91 nipulations of sys.path that it entails. Also disable these ma‐
92 nipulations if site is explicitly imported later.
93
94 -u Force the stdout and stderr streams to be unbuffered. This op‐
95 tion has no effect on the stdin stream.
96
97 -v Print a message each time a module is initialized, showing the
98 place (filename or built-in module) from which it is loaded.
99 When given twice, print a message for each file that is checked
100 for when searching for a module. Also provides information on
101 module cleanup at exit.
102
103 -V , --version
104 Prints the Python version number of the executable and exits.
105 When given twice, print more information about the build.
106
107
108 -W argument
109 Warning control. Python's warning machinery by default prints
110 warning messages to sys.stderr.
111
112 The simplest settings apply a particular action unconditionally
113 to all warnings emitted by a process (even those that are other‐
114 wise ignored by default):
115
116 -Wdefault # Warn once per call location
117 -Werror # Convert to exceptions
118 -Walways # Warn every time
119 -Wmodule # Warn once per calling module
120 -Wonce # Warn once per Python process
121 -Wignore # Never warn
122
123 The action names can be abbreviated as desired and the inter‐
124 preter will resolve them to the appropriate action name. For ex‐
125 ample, -Wi is the same as -Wignore .
126
127 The full form of argument is: action:message:category:mod‐
128 ule:lineno
129
130 Empty fields match all values; trailing empty fields may be
131 omitted. For example -W ignore::DeprecationWarning ignores all
132 DeprecationWarning warnings.
133
134 The action field is as explained above but only applies to warn‐
135 ings that match the remaining fields.
136
137 The message field must match the whole printed warning message;
138 this match is case-insensitive.
139
140 The category field matches the warning category (ex: "Depreca‐
141 tionWarning"). This must be a class name; the match test whether
142 the actual warning category of the message is a subclass of the
143 specified warning category.
144
145 The module field matches the (fully-qualified) module name; this
146 match is case-sensitive.
147
148 The lineno field matches the line number, where zero matches all
149 line numbers and is thus equivalent to an omitted line number.
150
151 Multiple -W options can be given; when a warning matches more
152 than one option, the action for the last matching option is per‐
153 formed. Invalid -W options are ignored (though, a warning mes‐
154 sage is printed about invalid options when the first warning is
155 issued).
156
157 Warnings can also be controlled using the PYTHONWARNINGS envi‐
158 ronment variable and from within a Python program using the
159 warnings module. For example, the warnings.filterwarnings()
160 function can be used to use a regular expression on the warning
161 message.
162
163
164 -X option
165 Set implementation specific option. The following options are
166 available:
167
168 -X faulthandler: enable faulthandler
169
170 -X showrefcount: output the total reference count and number
171 of used
172 memory blocks when the program finishes or after each
173 statement in the
174 interactive interpreter. This only works on debug builds
175
176 -X tracemalloc: start tracing Python memory allocations us‐
177 ing the
178 tracemalloc module. By default, only the most recent
179 frame is stored in a
180 traceback of a trace. Use -X tracemalloc=NFRAME to start
181 tracing with a
182 traceback limit of NFRAME frames
183
184 -X importtime: show how long each import takes. It shows
185 module name,
186 cumulative time (including nested imports) and self time
187 (excluding
188 nested imports). Note that its output may be broken in
189 multi-threaded
190 application. Typical usage is python3 -X importtime -c
191 'import asyncio'
192
193 -X dev: enable CPython's "development mode", introducing ad‐
194 ditional runtime
195 checks which are too expensive to be enabled by default.
196 It will not be
197 more verbose than the default if the code is correct:
198 new warnings are
199 only emitted when an issue is detected. Effect of the
200 developer mode:
201 * Add default warning filter, as -W default
202 * Install debug hooks on memory allocators: see the
203 PyMem_SetupDebugHooks()
204 C function
205 * Enable the faulthandler module to dump the Python
206 traceback on a crash
207 * Enable asyncio debug mode
208 * Set the dev_mode attribute of sys.flags to True
209 * io.IOBase destructor logs close() exceptions
210
211 -X utf8: enable UTF-8 mode for operating system interfaces,
212 overriding the default
213 locale-aware mode. -X utf8=0 explicitly disables UTF-8
214 mode (even when it would
215 otherwise activate automatically). See PYTHONUTF8 for
216 more details
217
218 -X pycache_prefix=PATH: enable writing .pyc files to a par‐
219 allel tree rooted at the
220 given directory instead of to the code tree.
221
222 -X warn_default_encoding: enable opt-in EncodingWarning for
223 'encoding=None'
224
225
226 -x Skip the first line of the source. This is intended for a DOS
227 specific hack only. Warning: the line numbers in error messages
228 will be off by one!
229
231 The interpreter interface resembles that of the UNIX shell: when called
232 with standard input connected to a tty device, it prompts for commands
233 and executes them until an EOF is read; when called with a file name
234 argument or with a file as standard input, it reads and executes a
235 script from that file; when called with -c command, it executes the
236 Python statement(s) given as command. Here command may contain multi‐
237 ple statements separated by newlines. Leading whitespace is signifi‐
238 cant in Python statements! In non-interactive mode, the entire input
239 is parsed before it is executed.
240
241 If available, the script name and additional arguments thereafter are
242 passed to the script in the Python variable sys.argv, which is a list
243 of strings (you must first import sys to be able to access it). If no
244 script name is given, sys.argv[0] is an empty string; if -c is used,
245 sys.argv[0] contains the string '-c'. Note that options interpreted by
246 the Python interpreter itself are not placed in sys.argv.
247
248 In interactive mode, the primary prompt is `>>>'; the second prompt
249 (which appears when a command is not complete) is `...'. The prompts
250 can be changed by assignment to sys.ps1 or sys.ps2. The interpreter
251 quits when it reads an EOF at a prompt. When an unhandled exception
252 occurs, a stack trace is printed and control returns to the primary
253 prompt; in non-interactive mode, the interpreter exits after printing
254 the stack trace. The interrupt signal raises the KeyboardInterrupt ex‐
255 ception; other UNIX signals are not caught (except that SIGPIPE is
256 sometimes ignored, in favor of the IOError exception). Error messages
257 are written to stderr.
258
260 These are subject to difference depending on local installation conven‐
261 tions; ${prefix} and ${exec_prefix} are installation-dependent and
262 should be interpreted as for GNU software; they may be the same. The
263 default for both is /usr/local.
264
265 ${exec_prefix}/bin/python
266 Recommended location of the interpreter.
267
268 ${prefix}/lib/python<version>
269 ${exec_prefix}/lib/python<version>
270 Recommended locations of the directories containing the standard
271 modules.
272
273 ${prefix}/include/python<version>
274 ${exec_prefix}/include/python<version>
275 Recommended locations of the directories containing the include
276 files needed for developing Python extensions and embedding the
277 interpreter.
278
280 PYTHONHOME
281 Change the location of the standard Python libraries. By de‐
282 fault, the libraries are searched in ${prefix}/lib/python<ver‐
283 sion> and ${exec_prefix}/lib/python<version>, where ${prefix}
284 and ${exec_prefix} are installation-dependent directories, both
285 defaulting to /usr/local. When $PYTHONHOME is set to a single
286 directory, its value replaces both ${prefix} and ${exec_prefix}.
287 To specify different values for these, set $PYTHONHOME to ${pre‐
288 fix}:${exec_prefix}.
289
290 PYTHONPATH
291 Augments the default search path for module files. The format
292 is the same as the shell's $PATH: one or more directory path‐
293 names separated by colons. Non-existent directories are
294 silently ignored. The default search path is installation de‐
295 pendent, but generally begins with ${prefix}/lib/python<version>
296 (see PYTHONHOME above). The default search path is always ap‐
297 pended to $PYTHONPATH. If a script argument is given, the di‐
298 rectory containing the script is inserted in the path in front
299 of $PYTHONPATH. The search path can be manipulated from within
300 a Python program as the variable sys.path.
301
302 PYTHONPLATLIBDIR
303 Override sys.platlibdir.
304
305 PYTHONSTARTUP
306 If this is the name of a readable file, the Python commands in
307 that file are executed before the first prompt is displayed in
308 interactive mode. The file is executed in the same name space
309 where interactive commands are executed so that objects defined
310 or imported in it can be used without qualification in the in‐
311 teractive session. You can also change the prompts sys.ps1 and
312 sys.ps2 in this file.
313
314 PYTHONOPTIMIZE
315 If this is set to a non-empty string it is equivalent to speci‐
316 fying the -O option. If set to an integer, it is equivalent to
317 specifying -O multiple times.
318
319 PYTHONDEBUG
320 If this is set to a non-empty string it is equivalent to speci‐
321 fying the -d option. If set to an integer, it is equivalent to
322 specifying -d multiple times.
323
324 PYTHONDONTWRITEBYTECODE
325 If this is set to a non-empty string it is equivalent to speci‐
326 fying the -B option (don't try to write .pyc files).
327
328 PYTHONINSPECT
329 If this is set to a non-empty string it is equivalent to speci‐
330 fying the -i option.
331
332 PYTHONIOENCODING
333 If this is set before running the interpreter, it overrides the
334 encoding used for stdin/stdout/stderr, in the syntax encoding‐
335 name:errorhandler The errorhandler part is optional and has the
336 same meaning as in str.encode. For stderr, the errorhandler
337 part is ignored; the handler will always be ´backslashreplace´.
338
339 PYTHONNOUSERSITE
340 If this is set to a non-empty string it is equivalent to speci‐
341 fying the -s option (Don't add the user site directory to
342 sys.path).
343
344 PYTHONUNBUFFERED
345 If this is set to a non-empty string it is equivalent to speci‐
346 fying the -u option.
347
348 PYTHONVERBOSE
349 If this is set to a non-empty string it is equivalent to speci‐
350 fying the -v option. If set to an integer, it is equivalent to
351 specifying -v multiple times.
352
353 PYTHONWARNINGS
354 If this is set to a comma-separated string it is equivalent to
355 specifying the -W option for each separate value.
356
357 PYTHONHASHSEED
358 If this variable is set to "random", a random value is used to
359 seed the hashes of str and bytes objects.
360
361 If PYTHONHASHSEED is set to an integer value, it is used as a
362 fixed seed for generating the hash() of the types covered by the
363 hash randomization. Its purpose is to allow repeatable hashing,
364 such as for selftests for the interpreter itself, or to allow a
365 cluster of python processes to share hash values.
366
367 The integer must be a decimal number in the range
368 [0,4294967295]. Specifying the value 0 will disable hash ran‐
369 domization.
370
371 PYTHONMALLOC
372 Set the Python memory allocators and/or install debug hooks. The
373 available memory allocators are malloc and pymalloc. The avail‐
374 able debug hooks are debug, malloc_debug, and pymalloc_debug.
375
376 When Python is compiled in debug mode, the default is pymal‐
377 loc_debug and the debug hooks are automatically used. Otherwise,
378 the default is pymalloc.
379
380 PYTHONMALLOCSTATS
381 If set to a non-empty string, Python will print statistics of
382 the pymalloc memory allocator every time a new pymalloc object
383 arena is created, and on shutdown.
384
385 This variable is ignored if the $PYTHONMALLOC environment vari‐
386 able is used to force the malloc(3) allocator of the C library,
387 or if Python is configured without pymalloc support.
388
389 PYTHONASYNCIODEBUG
390 If this environment variable is set to a non-empty string, en‐
391 able the debug mode of the asyncio module.
392
393 PYTHONTRACEMALLOC
394 If this environment variable is set to a non-empty string, start
395 tracing Python memory allocations using the tracemalloc module.
396
397 The value of the variable is the maximum number of frames stored
398 in a traceback of a trace. For example, PYTHONTRACEMALLOC=1
399 stores only the most recent frame.
400
401 PYTHONFAULTHANDLER
402 If this environment variable is set to a non-empty string,
403 faulthandler.enable() is called at startup: install a handler
404 for SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL signals to dump
405 the Python traceback.
406
407 This is equivalent to the -X faulthandler option.
408
409 PYTHONEXECUTABLE
410 If this environment variable is set, sys.argv[0] will be set to
411 its value instead of the value got through the C runtime. Only
412 works on Mac OS X.
413
414 PYTHONUSERBASE
415 Defines the user base directory, which is used to compute the
416 path of the user site-packages directory and Distutils installa‐
417 tion paths for python setup.py install --user.
418
419 PYTHONPROFILEIMPORTTIME
420 If this environment variable is set to a non-empty string,
421 Python will show how long each import takes. This is exactly
422 equivalent to setting -X importtime on the command line.
423
424 PYTHONBREAKPOINT
425 If this environment variable is set to 0, it disables the de‐
426 fault debugger. It can be set to the callable of your debugger
427 of choice.
428
429 Debug-mode variables
430 Setting these variables only has an effect in a debug build of Python,
431 that is, if Python was configured with the --with-pydebug build option.
432
433 PYTHONTHREADDEBUG
434 If this environment variable is set, Python will print threading
435 debug info. The feature is deprecated in Python 3.10 and will
436 be removed in Python 3.12.
437
438 PYTHONDUMPREFS
439 If this environment variable is set, Python will dump objects
440 and reference counts still alive after shutting down the inter‐
441 preter.
442
444 The Python Software Foundation: https://www.python.org/psf/
445
447 Main website: https://www.python.org/
448 Documentation: https://docs.python.org/
449 Developer resources: https://devguide.python.org/
450 Downloads: https://www.python.org/downloads/
451 Module repository: https://pypi.org/
452 Newsgroups: comp.lang.python, comp.lang.python.announce
453
455 Python is distributed under an Open Source license. See the file "LI‐
456 CENSE" in the Python source distribution for information on terms &
457 conditions for accessing and otherwise using Python and for a DIS‐
458 CLAIMER OF ALL WARRANTIES.
459
460
461
462 PYTHON(1)