python Command in Linux



The python command in Linux runs the Python scripts or starts the Python interactive shell. Python is a high-level, interpreted programming language known for its simplicity and readability. It supports multiple paradigms, including procedural, object-oriented, and functional programming. It is widely used in Linux for scripting, automation, system administration, and software development.

Table of Contents

Here is a comprehensive guide to the options available with the python command −

Syntax of python Command

The syntax of using the python command in Linux is as follows −

python [options] [script] [arguments]

The above syntax is for systems where Python 2 is installed by default.

Or −

python3 [options] [script] [arguments]

The above syntax is for systems where Python 3 is installed by default. Since Python 2 is deprecated, most modern Linux distributions use Python 3.

In the above syntax, the [options] field is used to specify flags like -c, -m, or -V. The [script] field is used to specify the Python script to execute. The [arguments] are additional arguments passed to the script.

python Command Options

The options used with the Linux python command are listed below −

Flag Option Description
-B Do not write .pyc files on import.
-b Warn about str(bytes_instance), str(bytearray_instance), and invalid comparisons (-bb makes them errors).
-c command Execute a command string and exit.
--check-hash-based-pycs mode Configure hash-based .pyc file validation.
-d Enable parser debugging output (for experts).
-E Ignore Python environment variables (PYTHONPATH, PYTHONHOME).
-h, -? --help Show Python usage and exit.
--help-env Show help for Python environment variables.
--help-xoptions Show help for -X options.
--help-all Show full usage information.
-i Enter interactive mode after script execution.
-I Run in isolated mode, ignoring environment variables and user site-packages.
-m module-name Run a module as a script.
-O Remove assert statements and optimize .pyc filenames (.opt-1).
-OO Like -O, but also remove docstrings (.opt-2).
-P Prevent adding unsafe paths (example: current directory) to sys.path.
-q Suppress version and copyright messages.
-s Prevent adding the user site directory to sys.path.
-S Disable automatic import of site module.
-u Unbuffer stdout and stderr.
-v Print module import details (-vv for more).
-V --version Show Python version (-VV for more details).
-W argument Control warnings (-Wignore, -Werror, and others).
-X option Set implementation-specific options (Example: -X faulthandler).
-x Skip the first line of a script (DOS-specific).

Examples of python Command in Linux

This section explores how to use the python command in Linux with examples −

Note − Since Python 3 is the default in modern Linux, the python3 command is used in examples.

  • Running a Python Script File
  • Running Python Code
  • Enabling Interactive Mode
  • Running a Module
  • Ignoring All Environment Variables
  • Enabling Debug Mode
  • Enabling Isolated Mode
  • Running a Python Script without Creating a Compiled File
  • Controlling Warnings
  • Displaying the Imported Modules
  • Displaying Help for Python Environment Variables
  • Displaying Usage Help

Running a Python Script File

To run a Python script file, use the python command in the following way −

python3 script.py
python Command in Linux1

Running Python Code

To run the Python code strings directly from the command line, use the -c option −

python3 -c 'print("Hello, Tutorialspoint!")'
python Command in Linux2

Enabling Interactive Mode

To enable the interactive mode, use the -i option with the python command −

python3 -i
python Command in Linux3

A list of various Python operations in the interactive mode is given below:

Running a Python script −

print("Hello, Tutorialspoint!")
python Command in Linux4

Performing a calculation −

8+4
python Command in Linux5

Defining variables and performing an operation −

a=5 b=8 a+b
python Command in Linux6

Creating and running a Python function −

def greet(name): return "Welcome! "+name
python Command in Linux7

To exit the interactive mode, type exit() and press Enter or simply press the CTRL+D buttons.

python Command in Linux8

Running a Module

Python provides various built-in modules that can be run using the -m option. For example, to run the built-in debugger, use the python command in the following manner −

python3 -m pdb script.py
python Command in Linux9

Similarly, to inspect a module, use −

python3 -m pydoc math
python Command in Linux10

Ignoring All Environment Variables

To ignore all the environment variables before executing a script, use the -E option. It changes the way Python script is executed.

python3 -E script.py

This ensures the script runs with Python's default settings, ignoring any modifications from the environment.

Enabling Debug Mode

To run a Python script in debug mode, use the -d option. It enables parser debugging output.

python3 -d script.py

It is only useful for experts and requires a Python build compiled with debugging support.

Enabling Isolated Mode

The isolated mode restricts access to user-specific configurations and environment variables to run scripts in a secure environment.

To run a Python script in the isolated mode, use the -I option with the python command.

python3 -I script.py

Running a Python Script without Creating a Compiled File

A .pyc file is a compiled Python bytecode file that is automatically created when a Python script is run or imported. To run the Python script without creating this file, use the -B option −

python3 -B script.py

Controlling Warnings

To control warnings while running the Python script, use the -W option. For example, to ignore warnings, use the following command −

python3 -W ignore script.py

Other options are listed below −

default Prints each warning once per source line (default behavior).
all Prints a warning every time it occurs, even repeatedly on the same line.
module Prints each warning only the first time it occurs in each module.
once Prints each warning only the first time it occurs in the program.
error Raises an exception instead of printing a warning.

Displaying the Imported Modules

To display the imported modules in the script, use the -v option −

python3 -v script.py
python Command in Linux11

To increase the verbosity level, use the -vv

python3 -vv script.py

Displaying Help for Python Environment Variables

To display the help for the Python environment variables, use the --help-env option −

python3 --help-env
python Command in Linux12

Displaying Usage Help

To display the usage help of the python command, use the -h, -?, or --help option −

python3 -h

To display full usage help, use the --help-all option −

python3 --help-all

Conclusion

The python command in Linux is used to run Python scripts or start the interactive Python shell. It supports various options to modify execution behavior, such as running modules, enabling debugging, controlling warnings, and ignoring environment variables. Modern Linux systems default to python3, as Python 2 is deprecated.

This tutorial covered the syntax, available options, and practical usage examples of the python command, providing insights into its flexibility for scripting, automation, and development.

Advertisements