LinuxCommandLibrary

python

Execute Python code

TLDR

Start a REPL (interactive shell)

$ python
copy

Execute a specific Python file
$ python [path/to/file.py]
copy

Execute a specific Python file and start a REPL
$ python -i [path/to/file.py]
copy

Execute a Python expression
$ python -c "[expression]"
copy

Run the script of the specified library module
$ python -m [module] [arguments]
copy

Install a package using pip
$ python -m pip install [package]
copy

Interactively debug a Python script
$ python -m pdb [path/to/file.py]
copy

Start the built-in HTTP server on port 8000 in the current directory
$ python -m http.server
copy

SYNOPSIS

python [options] [-c command | -m module | script] [args...]

PARAMETERS

script
    The name of the Python script file to execute. Subsequent arguments are passed to the script.

-c command
    Executes the Python command (a string) as a single statement. Useful for quick one-liners.

-m module
    Runs the specified Python library module as a script. This is equivalent to locating the module file and running it directly.

-i
    Inspect interactively after running script or -c command or -m module. Also forces prompts even if stdin does not appear to be a terminal.

-O
    Optimize generated bytecode (removes assert statements and __debug__ checks).

-OO
    Remove docstrings in addition to optimizations from -O.

-v
    Verbose mode (trace import statements as they are executed).

-q
    Quiet mode (don't print the Python version and copyright banner on interactive startup).

-s
    Don't add the user site directory to sys.path.

-E
    Ignore all PYTHON* environment variables (e.g., PYTHONPATH, PYTHONHOME).

-S
    Disable the import of the site module on startup. Useful for environments where site-specific paths or modules are not desired.

-B
    Don't write .pyc files on import of source modules. Bytecode is cached in memory only.

-W arg
    Warning control. Specifies how to handle warnings. E.g., 'ignore', 'default', 'error', 'always', 'module', 'once'.

-X opt
    Set implementation-specific option. E.g., 'faulthandler', 'utf8', 'dev'.

--version
    Print the Python version number and exit.

--help
    Print a usage help message and exit.

DESCRIPTION

The python command on Linux systems serves as the primary interpreter for the Python programming language. It allows users to execute Python scripts, run Python modules directly, or enter an interactive Python shell for immediate code execution and testing.

When invoked without arguments, it typically launches the interactive interpreter, providing a convenient environment for experimenting with Python code. With arguments, it can execute a specified Python script file (`.py`), run a Python module as a script using the -m option, or execute a single line of Python code using the -c option. The command is fundamental for developing, testing, and deploying Python applications and utilities across various Linux distributions. It is widely used for web development, data analysis, automation, system administration, and scientific computing.

CAVEATS

On many Linux systems, the default python command often symlinks to Python 2 (e.g., /usr/bin/python -> /usr/bin/python2) for backward compatibility, while python3 points to Python 3. It's crucial to specify python3 explicitly for modern applications to avoid compatibility issues. Using virtual environments (created via python3 -m venv or virtualenv) is highly recommended to manage project dependencies and avoid conflicts with system-wide Python installations.

ENVIRONMENT VARIABLES

The behavior of the python command can be influenced by several environment variables:
PYTHONPATH: A colon-separated list of directories added to the default module search path.
PYTHONSTARTUP: Path to an initialization file executed when the interactive interpreter starts.
PYTHONCASEOK: (Windows only) Ignores case in 'import' statements.
PYTHONIOENCODING: Forces the encoding for stdin/stdout/stderr.

SHEBANG LINE

For making Python scripts executable directly, the 'shebang' line is crucial. It specifies the interpreter to use for the script. For example:
#!/usr/bin/env python3
This tells the kernel to execute the script using the python3 interpreter found in the system's PATH. Ensure the script has executable permissions (chmod +x script.py).

HISTORY

Python was created by Guido van Rossum in the late 1980s, with its first release in 1991. The python command became the standard way to invoke the interpreter on Unix-like systems. A significant evolution occurred with the release of Python 3.0 in 2008, which introduced backward-incompatible changes. This led to a period where both Python 2 and Python 3 interpreters co-existed, often accessible via `python` (for version 2) and `python3` commands, respectively. As of 2020, Python 2 is officially deprecated, and Python 3 is the current and actively developed version, making `python3` the preferred command for new projects.

SEE ALSO

python3(1), pip(1), venv(1), chmod(1), env(1)

Copied to clipboard