PYTHON(1)

๐Ÿ“ฆ
version 3.12.16 ยท 2 versions
  • 3.12.16 ยท alpine 3.22, alpine 3.23, centos-stream 10, centos-stream 9, fedora 43, fedora 44, nixpkgs unstable (latest)
  • 3.12.3 ยท ubuntu noble
, in alpine 3.22, alpine 3.23, centos-stream 10, centos-stream 9, fedora 43, fedora 44, nixpkgs unstable

๐Ÿ’… PYTHON(1) โ€” General Commands Manual

okay bestie, gather round, because we are about to get into python โ€” the General Commands Manual girlie, section 1, the one and only. this is the interpreter that runs your whole life and honestly? she ate. โœจ


๐Ÿ‘‘ NAME

python โ€” an interpreted, interactive, object-oriented programming language ๐Ÿ๐Ÿ’–

no cap, that's the whole vibe: interpreted, interactive, object-oriented. three personality traits, all iconic.


โœจ SYNOPSIS

python [ -B ] [ -b ] [ -d ] [ -E ] [ -h ] [ -i ] [ -I ]
       [ -m module-name ] [ -q ] [ -O ] [ -OO ] [ -P ] [ -s ] [ -S ] [ -u ]
       [ -v ] [ -V ] [ -W argument ] [ -x ] [ -X option ] [ -?  ]
       [ --check-hash-based-pycs default | always | never ]
       [ --help ] [ --help-env ] [ --help-xoptions ] [ --help-all ]
       [ -c command | script | - ] [ arguments ]

like, look at all those little flags. it's giving options menu at a fancy restaurant ๐ŸŽ€


๐Ÿฆ‹ DESCRIPTION

okay so. Python is an interpreted, interactive, object-oriented programming language that combines remarkable power with very clear syntax. like, that's the actual sentence and it's so real โ€” she's powerful AND she communicates. main character energy fr fr. ๐Ÿ‘‘

want an intro to programming in Python? go read the Python Tutorial. need the built-ins and the standard types, constants, functions and modules documented? that's the Python Library Reference. and if you want the syntax and semantics of the core language explained in (perhaps too) much detail โ€” their words, not mine, and honestly the shade is valid โ€” that's the Python Reference Manual.

those documents may be located via the INTERNET RESOURCES below, and they may also be installed on your system as well. check both, bestie, they're probably just sitting there. ๐Ÿ“š


Python's basic power can be extended with your own modules written in C or C++. on most systems, such modules may be dynamically loaded. Python is also adaptable as an extension language for existing applications. see the internal documentation for hints. ๐Ÿงš


and if you ever wanna peek at the docs for the Python modules and packages you've got installed? just run the pydoc program. that's it. that's the plug. ๐Ÿ’…

not me ending part 1 here like a cliffhanger bestie, but the page is LONG and there's more coming โ€” stay tuned, we're not done with her yet ๐Ÿซถ

๐Ÿ’… COMMAND LINE OPTIONS ๐Ÿ’…

okay bestie, this is the good part. these are the little letters you sprinkle after python3 to make it do your bidding. let's go one by one, no skips, periodt.

  • -B โ€” don't write .pyc files on import. that's it, that's the whole vibe. also peep PYTHONDONTWRITEBYTECODE if you're into that.
  • -b โ€” throw warnings about str(bytes_instance), str(bytearray_instance), and about comparing bytes/bytearray with str. and if you go -bb (double it up, slay) those become errors instead. ๐Ÿ’€
  • -c command โ€” tell Python which command to execute (the next section spills the tea on this one). heads up: this terminates the option list, so anything after it gets passed as arguments to the command. bye options, hello args.
  • --check-hash-based-pycs mode โ€” this one configures how Python decides whether a hash-based .pyc file is up to date. yes it's niche. yes it's iconic.
  • -d โ€” turn on parser debugging output. expert only, and it depends on how your build was compiled. iykyk.
  • -E โ€” ignore environment variables like PYTHONPATH and PYTHONHOME that mess with how the interpreter behaves. it's giving "i don't see your env vars, bestie".
  • -h, -?, --help โ€” prints the usage for the interpreter executable and dips. classic.
  • --help-env โ€” prints help about Python-specific environment variables, then exits. so handy fr fr.
  • --help-xoptions โ€” prints help about implementation-specific -X options, then exits. โœจ
  • --help-all โ€” prints complete usage information and exits. the whole buffet.
  • -i โ€” when a script is passed as the first argument, or when you use -c, this drops you into interactive mode after running the script or command. note: it does not read the $PYTHONSTARTUP file. super useful for poking at global variables or grabbing a stack trace when a script raises an exception. ๐Ÿ‘‘
  • -I โ€” run Python in isolated mode. this also implies -E, -P and -s. in isolated mode, sys.path contains neither the script's directory nor the user's site-packages directory. all PYTHON* environment variables get ignored too. and further restrictions may be imposed to stop the user from injecting malicious code. safety first, babe. ๐Ÿซถ
  • -m module-name โ€” searches sys.path for the named module and runs the corresponding .py file as a script. this terminates the option list (so everything after gets passed as arguments to the module).
  • -O โ€” removes assert statements and any code conditional on the value of __debug__. it also augments the filename for compiled (bytecode) files by adding .opt-1 before the .pyc extension.
  • -OO โ€” does everything -O does and throws away docstrings. it changes the compiled (bytecode) filename by adding .opt-2 before the .pyc extension. bye docstrings, we hardly knew ye. ๐Ÿ˜ญ
  • -P โ€” don't automatically prepend a potentially unsafe path to sys.path, like the current directory, the script's directory, or an empty string. see also the PYTHONSAFEPATH environment variable.
  • -q โ€” don't print the version and copyright messages. (these are also suppressed in non-interactive mode anyway.)
  • -s โ€” don't add the user site directory to sys.path. not today, user site directory.
  • -S โ€” disable the import of the module site and the site-dependent manipulations of sys.path that come with it. also disables those manipulations if site gets explicitly imported later. so it stays off, fr.
  • -u โ€” force the stdout and stderr streams to be unbuffered. this option has no effect on the stdin stream. ๐Ÿ™ˆ
  • -v โ€” print a message each time a module is initialized, showing the place (filename or built-in module) it's loaded from. give it twice and it prints a message for each file that gets checked while searching for a module. it also gives you info on module cleanup at exit. very chatty, very main character.
  • -V, --version โ€” prints the Python version number of the executable and exits. give it twice and it prints more information about the build.
  • -W argument โ€” warning control, babe. Python's warning machinery by default prints warning messages to sys.stderr.

the simplest settings apply a particular action unconditionally to all warnings emitted by a process (even the ones that are otherwise ignored by default):

-Wdefault  # Warn once per call location
-Werror    # Convert to exceptions
-Walways   # Warn every time
-Wall      # Same as -Walways
-Wmodule   # Warn once per calling module
-Wonce     # Warn once per Python process
-Wignore   # Never warn

the action names can be abbreviated however you want and the interpreter will resolve them to the appropriate action name. for example, -Wi is the same as -Wignore.

the full form of argument is:

action:message:category:module:lineno

empty fields match all values, and trailing empty fields may be omitted. for example, -W ignore::DeprecationWarning ignores all DeprecationWarning warnings.

the action field is as explained above, but it only applies to warnings that match the remaining fields.

the message field must match the whole printed warning message. this match is case-insensitive.

the category field matches the warning category (ex: "DeprecationWarning"). this must be a class name. the match tests whether the actual warning category of the message is a subclass of the specified warning category.

the module field matches the (fully-qualified) module name. this match is case-sensitive.

the lineno field matches the line number, where zero matches all line numbers and is thus equivalent to an omitted line number.

multiple -W options can be given. when a warning matches more than one option, the action for the last matching option is performed. invalid -W options are ignored (though a warning message is printed about invalid options when the first warning is issued).

warnings can also be controlled using the PYTHONWARNINGS environment variable, and from within a Python program using the warnings module. for example, the warnings.filterwarnings() function can be used to apply a regular expression to the warning message.

  • -X option โ€” set an implementation-specific option. the following options are available:
    • -X faulthandler โ€” enable faulthandler.
    • -X showrefcount โ€” 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 debug builds.
    • -X tracemalloc โ€” start tracing Python memory allocations using the tracemalloc module. by default, only the most recent frame is stored in a traceback of a trace. use -X tracemalloc=NFRAME to start tracing with a traceback limit of NFRAME frames.
    • -X importtime โ€” show how long each import takes. it shows the module name, cumulative time (including nested imports) and self time (excluding nested imports). note that its output may be broken in multi-threaded applications. typical usage is python3 -X importtime -c 'import asyncio'.
    • -X dev โ€” enable CPython's "development mode", introducing additional runtime checks that are too expensive to be enabled by default. it will not be more verbose than the default if the code is correct: new warnings are only emitted when an issue is detected. the effect of developer mode is:
      • add default warning filter, as -W default
      • install debug hooks on memory allocators: see the PyMem_SetupDebugHooks() C function
      • enable the faulthandler module to dump the Python traceback on a crash
      • enable asyncio debug mode
      • set the dev_mode attribute of sys.flags to True
      • io.IOBase destructor logs close() exceptions
    • -X utf8 โ€” enable UTF-8 mode for operating system interfaces, overriding the default locale-aware mode. -X utf8=0 explicitly disables UTF-8 mode (even when it would otherwise activate automatically). see PYTHONUTF8 for more details.
    • -X pycache_prefix=PATH โ€” enable writing .pyc files to a parallel tree rooted at the given directory instead of to the code tree.
    • -X warn_default_encoding โ€” enable opt-in EncodingWarning for 'encoding=None'.
    • -X no_debug_ranges โ€” disable 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 you want smaller code objects and pyc files, and it also suppresses the extra visual location indicators when the interpreter displays tracebacks.
    • -X frozen_modules=[on|off] โ€” whether or not frozen modules should be used. the default is "on" (or "off" if you are running a local build).
    • -X int_max_str_digits=number โ€” limit the size of int<->str conversions. this helps avoid denial of service attacks when parsing untrusted data. the default is sys.int_info.default_max_str_digits. 0 disables it.
  • -x โ€” skip the first line of the source. this is intended for a DOS specific hack only. โš ๏ธ warning: the line numbers in error messages will be off by one! ๐Ÿ˜ญ

๐ŸŒธ INTERPRETER INTERFACE ๐ŸŒธ

okay so bestie, the interpreter interface is basically giving UNIX shell energy ๐Ÿ’… โ€” here's how it reads you:

  • When it's called with standard input connected to a tty device, it prompts you for commands and executes them until it reads an EOF. Chatty queen behavior ๐Ÿ’ฌ
  • When it's called with a file name argument or with a file as standard input, it reads and executes a script from that file ๐Ÿ“œ
  • When it's called with -c command, it executes the Python statement(s) given as command. And bestie, command can contain multiple statements separated by newlines. Leading whitespace is significant in Python statements! ๐Ÿ˜ค like genuinely, don't touch the indentation. In non-interactive mode, the entire input is parsed before it is executed.

If available, the script name and additional arguments thereafter are passed to the script in the Python variable sys.argv, which is a list of strings โ€” and yes, you must first import sys to be able to access it, no cap ๐Ÿ™ˆ.

  • If no script name is given, sys.argv[0] is an empty string
  • If -c is used, sys.argv[0] contains the string '-c'
  • Note that options interpreted by the Python interpreter itself are not placed in sys.argv ๐Ÿ‘‘

In interactive mode, the primary prompt is >>> and the second prompt (the one that shows up when a command is not complete) is ... โœจ. You can change the prompts by assignment to sys.ps1 or sys.ps2 โ€” customize your aesthetic, it's your right ๐ŸŽ€.

The interpreter quits when it reads an EOF at a prompt. When an unhandled exception occurs, a stack trace is printed and control returns to the primary prompt ๐Ÿซถ; in non-interactive mode, the interpreter exits after printing the stack trace.

The interrupt signal raises the KeyboardInterrupt exception; other UNIX signals are not caught (except that SIGPIPE is sometimes ignored, in favor of the IOError exception). Error messages are written to stderr. ๐Ÿ’€


๐Ÿ—‚๏ธ FILES AND DIRECTORIES ๐Ÿ—‚๏ธ

These are subject to difference depending on local installation conventions, bestie โ€” ${prefix} and ${exec_prefix} are installation-dependent and should be interpreted as for GNU software; they may be the same. The default for both is /usr/local ๐Ÿ’–

  • ${exec_prefix}/bin/python โ€” the recommended location of the interpreter ๐Ÿ‘‘
  • ${prefix}/lib/python<version> ${exec_prefix}/lib/python<version> โ€” recommended locations of the directories containing the standard modules ๐Ÿ“ฆ
  • ${prefix}/include/python<version> ${exec_prefix}/include/python<version> โ€” recommended locations of the directories containing the include files needed for developing Python extensions and embedding the interpreter ๐Ÿงš

๐ŸŒˆ ENVIRONMENT VARIABLES ๐ŸŒˆ

the way that these little guys control *everything*โ€ฆ obsessed. iykyk ๐Ÿ’œ

  • PYTHONSAFEPATH โ€” if this is set to a non-empty string, don't automatically prepend a potentially unsafe path to sys.path such as the current directory, the script's directory or an empty string. See also the -P option. ๐Ÿ›ก๏ธ
  • PYTHONHOME โ€” change the location of the standard Python libraries. By default, the libraries are searched in ${prefix}/lib/python<version> and ${exec_prefix}/lib/python<version>, where ${prefix} and ${exec_prefix} are installation-dependent directories, both defaulting to /usr/local. When $PYTHONHOME is set to a single directory, its value replaces both ${prefix} and ${exec_prefix}. To specify different values for these, set $PYTHONHOME to ${prefix}:${exec_prefix} ๐Ÿ 
  • PYTHONPATH โ€” augments the default search path for module files. The format is the same as the shell's $PATH: one or more directory pathnames separated by colons. Non-existent directories are silently ignored ๐Ÿ™ˆ. The default search path is installation dependent, but generally begins with ${prefix}/lib/python<version> (see PYTHONHOME above). The default search path is always appended to $PYTHONPATH. If a script argument is given, the directory containing the script is inserted in the path in front of $PYTHONPATH. The search path can be manipulated from within a Python program as the variable sys.path ๐Ÿ›ฃ๏ธ
  • PYTHONPLATLIBDIR โ€” override sys.platlibdir.
  • PYTHONSTARTUP โ€” 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 name space 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 sys.ps1 and sys.ps2 in this file ๐ŸŒ…
  • PYTHONOPTIMIZE โ€” if this is set to a non-empty string it is equivalent to specifying the -O option. If set to an integer, it is equivalent to specifying -O multiple times โšก
  • PYTHONDEBUG โ€” if this is set to a non-empty string it is equivalent to specifying the -d option. If set to an integer, it is equivalent to specifying -d multiple times ๐Ÿ›
  • PYTHONDONTWRITEBYTECODE โ€” if this is set to a non-empty string it is equivalent to specifying the -B option (don't try to write .pyc files) ๐Ÿšซ
  • PYTHONINSPECT โ€” if this is set to a non-empty string it is equivalent to specifying the -i option ๐Ÿ”
  • PYTHONIOENCODING โ€” if this is set before running the interpreter, it overrides the encoding used for stdin/stdout/stderr, in the syntax encodingname:errorhandler. The errorhandler part is optional and has the same meaning as in str.encode. For stderr, the errorhandler part is ignored; the handler will always be backslashreplace โœ๏ธ
  • PYTHONNOUSERSITE โ€” if this is set to a non-empty string it is equivalent to specifying the -s option (don't add the user site directory to sys.path) ๐Ÿ™…
  • PYTHONUNBUFFERED โ€” if this is set to a non-empty string it is equivalent to specifying the -u option ๐Ÿšฐ
  • PYTHONVERBOSE โ€” if this is set to a non-empty string it is equivalent to specifying the -v option. If set to an integer, it is equivalent to specifying -v multiple times ๐Ÿ“ข
  • PYTHONWARNINGS โ€” if this is set to a comma-separated string it is equivalent to specifying the -W option for each separate value โš ๏ธ
  • PYTHONHASHSEED โ€” if this variable is set to "random", a random value is used to seed the hashes of str and bytes objects ๐ŸŽฒ If PYTHONHASHSEED 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. 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. The integer must be a decimal number in the range [0,4294967295]. Specifying the value 0 will disable hash randomization ๐Ÿ”’
  • PYTHONINTMAXSTRDIGITS โ€” limit the maximum digit characters in an int value when converting from a string and when converting an int back to a str. A value of 0 disables the limit. Conversions to or from bases 2, 4, 8, 16, and 32 are never limited ๐Ÿ”ข
  • PYTHONMALLOC โ€” set the Python memory allocators and/or install debug hooks. The available memory allocators are malloc and pymalloc. The available debug hooks are debug, malloc_debug, and pymalloc_debug. When Python is compiled in debug mode, the default is pymalloc_debug and the debug hooks are automatically used. Otherwise, the default is pymalloc ๐Ÿง 
  • PYTHONMALLOCSTATS โ€” if set to a non-empty string, Python will print statistics of the pymalloc memory allocator every time a new pymalloc object arena is created, and on shutdown ๐Ÿ“Š This variable is ignored if the $PYTHONMALLOC environment variable is used to force the malloc(3) allocator of the C library, or if Python is configured without pymalloc support ๐Ÿšซ
  • PYTHONASYNCIODEBUG โ€” if this environment variable is set to a non-empty string, enable the debug mode of the asyncio module ๐ŸŒ€
  • PYTHONTRACEMALLOC โ€” if this environment variable is set to a non-empty string, start tracing Python memory allocations using the tracemalloc module. The value of the variable is the maximum number of frames stored in a traceback of a trace. For example, PYTHONTRACEMALLOC=1 stores only the most recent frame ๐Ÿ•ต๏ธ
  • PYTHONFAULTHANDLER โ€” if this environment variable is set to a non-empty string, faulthandler.enable() is called at startup: install a handler for SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL signals to dump the Python traceback. This is equivalent to the -X faulthandler option ๐Ÿ’ฅ
  • PYTHONEXECUTABLE โ€” if this environment variable is set, sys.argv[0] will be set to its value instead of the value got through the C runtime. Only works on Mac OS X ๐ŸŽ
  • PYTHONUSERBASE โ€” defines the user base directory, which is used to compute the path of the user site-packages directory and installation paths for python -m pip install --user ๐Ÿ—๏ธ
  • PYTHONPROFILEIMPORTTIME โ€” if this environment variable is set to a non-empty string, Python will show how long each import takes. This is exactly equivalent to setting -X importtime on the command line โฑ๏ธ
  • PYTHONBREAKPOINT โ€” if this environment variable is set to 0, it disables the default debugger. It can be set to the callable of your debugger of choice ๐Ÿž

๐Ÿ› ๏ธ Debug-mode variables ๐Ÿ› ๏ธ

Setting these variables only has an effect in a debug build of Python, that is, if Python was configured with the --with-pydebug build option. so likeโ€ฆ no debug build, no vibes ๐Ÿ’…

  • PYTHONDUMPREFS โ€” if this environment variable is set, Python will dump objects and reference counts still alive after shutting down the interpreter ๐Ÿ‘ป

๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

The Python Software Foundation: https://www.python.org/psf/ ๐Ÿ’–


๐ŸŒ INTERNET RESOURCES ๐ŸŒ


๐Ÿ“œ LICENSING ๐Ÿ“œ

Python is distributed under an Open Source license. See the file "LICENSE" in the Python source distribution for information on terms & conditions for accessing and otherwise using Python and for a DISCLAIMER OF ALL WARRANTIES. read it bestie, fr fr ๐Ÿ™


and that's a wrap on the whole page ๐ŸŽ€ the footer says PYTHON(1) and that's the man page name your man command files it under โ€” slay, go forth and run some Python, you've got this ๐Ÿ’…โœจ๐Ÿฆ‹