9 Python Command Line Flags

Commonly used Python command-line options in a single frame.

When invoking a Python script, one can specify various options/flags.

They are used to modify the behavior of the Python interpreter or augment it with additional functionalities.

The following visual neatly summarizes 9 most common command line flags used in Python:

Let’s understand how they work one by one.

1) python -c

This flag lets us run a single Python command directly in the command line.

Say, after installing a library, we want to quickly check whether it’s working or not.

We can do this by specifying the import statement alongside python -c in the command line, as demonstrated below:

So, in a way, one can test such things without loading an IPython kernel.

2) python -i

Usually, when we run any Python script, the interpreter terminates:

  • either when the script has finished execution,

  • or when an error comes up.

This flag runs the script as usual, but it immediately enters the interactive mode after termination.

For instance, consider the following code:

Let’s run it once normally and once with the -i flag.

As depicted above:

  • Running the script normally terminates the program when it encounters an error.

  • But using the -i flag takes us to an interactive mode, where we can access all the variables/functions/objects declared in the code.

The interactive model can be pretty helpful for debugging as it allows us to interact with the objects created during the program.

3-4) python -O and Python -OO

Typically, one may write assert statements in their Python code to enforce certain conditions that must be true for the program to function correctly.

The -O flag lets us run a script while ignoring the assert statements. This means that all assert statements are effectively removed from the bytecode, making the resulting bytecode smaller and potentially improving run-time performance.

For instance, the code below will raise an assertion error:

Running this script with the -O Flag lets us ignore the assert statements, as shown below:

It’s important to note that using the -O flag should be approached with caution, as it eliminates a crucial tool for debugging and error detection.

One may use them when they know their code is thoroughly tested and the assert statements are not needed at that specific time.

Instead of commenting them out manually, the -O flag provides a quick solution.

Similarly, the -OO flag lets us run a script while ignoring the assert statements and docstrings both.

We intentionally ignore the docstrings because, at the end of the day, they are strings, which must be evaluated during run-time.

Ignoring them can be minutely helpful for optimization.

Also, it is sometimes desired to ignore docstrings when we wish to determine the true run-time of our code.

5) python -W

This flag lets us ignore all warnings that a program may throw.

For instance, consider the code below, where we intentionally raise a warning:

Running this script with the -W flag lets us ignore this warning, as shown below:

This is quite useful for turning off warnings temporarily and focusing on development.

6) python -m

This flag lets us run a module as a script.

Some background:

  • A script is a file we would execute — python code.py.

  • A module is a file we would import code from — import code.

So, in a way, this flag lets us run the module as if it were a script.

python -m code

Why would we do that, and what are the advantages?

Understanding this would require another post because python -m is probably one of the most important ones on this list.

So I will cover this particular flag and some background details in the upcoming issue.

The remaining three command line flags are not used actively, so we shall only discuss them briefly.

7) python -v

This flag lets us enter verbose mode when executing a script.

In other words, this prints detailed information about the execution of the script, providing insights into the modules and files being loaded.

One thing to note here is that the -v flag is cumulative, i.e., adding more vs (e.g., -vv or -vvv) increases the verbosity level.

This can be useful for printing extra information during program execution, but it is usually overwhelming for routine script executions.

8) python -x

This flag is used to skip the first line in a script. It is useful for ignoring shebang lines or other comments that may be present at the start of a script.

9) python -E

Finally, the -E flag lets us ignore all Python environment variables.

This is quite useful for ensuring a consistent program behavior by ignoring environment variables that may affect program execution.

That said, instead of using the -E flag, I mostly prefer and recommend creating a new environment instead.

The -E flag can be useful when someone is facing issues while creating a new environment.

👉 Over to you: Which popular Python command line flags have I missed? Let me know :)

Are you overwhelmed with the amount of information in ML/DS?

Every week, I publish no-fluff deep dives on topics that truly matter to your skills for ML/DS roles.

For instance:

Join below to unlock all full articles:

SPONSOR US

Get your product in front of 82,000 data scientists and other tech professionals.

Our newsletter puts your products and services directly in front of an audience that matters — thousands of leaders, senior data scientists, machine learning engineers, data analysts, etc., who have influence over significant tech decisions and big purchases.

To ensure your product reaches this influential audience, reserve your space here or reply to this email to ensure your product reaches this influential audience.

Reply

or to participate.