How to Run Python Code from the Command Line

Beatme CodeBY-NC-ND
7:00 read·910 words· published

This article is about running Python code from the command line. As for how to run Python scripts as files, you can check out the article How to Run Python Scripts Files, Modules, Packages from the Command Line.

Run Python Code from the Command Line

Type python or python3 (or some alias for Python) on the command line with the -c parameter, then give the Python code you want to run, and press Enter to execute it.

Windows
python -c <command>
UNIX/Linux/macOS
python3 -c <command>
command argument value

The Python code represented by the value of the command argument can be single-line or multi-line, where multi-line effects need to be realized by line breaks. Line breaks may be expressed differently in different command-line applications, for example, a line break in PowerShell may be expressed as `n.

Special characters in command need to be escaped, e.g., double quotes within double quotes, single quotes within single quotes, depending on the specific command-line application. Of course, special characters contained in Python strings can still be escaped with \, and if \ itself is a command-line special character, it should be decided on a case-by-case basis whether to escape \ first.

In addition, a single line of code can be combined with multiple statements via ;, and the indentation at the beginning of each line in multi-line code has the same meaning as the indentation in a py file.

Escape Special Characters

To learn about escaping in the command line, you can check out the Programming Guide’s section How to Escape Special Characters in the Command Line.

In PowerShell, we simply use code to output a message.

# PowerShell
python -c "print('Execute code from the command line!')"
Execute code from the command line!

Still in PowerShell, the following code combines the assignment statements for variables a and b into a single line via ;, while the if statement is divided into multiple lines via `n, and the whitespace in front of the print function can’t be omitted, or else you’ll get a syntax error.

# PowerShell
python -c "a = int(input('A = '));b = int(input('B = '))`nif a > b:`n print('A is greater than B')`nelse: print('A is less than B')"
A = 1
B = 3
A is less than B

If you want to use double quotes within double quotes in PowerShell, then you need to escape them using `", as shown in the first line of code below. As for the other line of code, although the two single quotes in the middle do not need to be escaped for PowerShell, they are in Python’s single-quote string, so they are escaped using the Python escape character \.

# PowerShell
python -c "print('Jerry said, `"Escape the double quotes in PowerShell double quotes`"')"
Jerry said, "Escape the double quotes in PowerShell double quotes"
python -c "print('Jerry said, \'Escape the single quotes in Python single quotes\'')"
Jerry said, 'Escape the single quotes in Python single quotes'

On UNIX/Linux/macOS systems, the escape character of a shell program is generally \, and after writing \\t, \\t will be escaped by the command line as \t, and \t will be escaped by Python as a tab. After writing \\\\, \\\\ will be escaped by the command line as \\, and \\ will be escaped by Python as \.

# UNIX/Linux/macOS
python3 -c "print('Jerry said, \\tEscape Python escape characters\\\\')"
Jerry said, Escape Python escape characters\

Run Python Code in Interactive Mode

What is Python interactive mode?

You can think of Python interactive mode as a command-line implementation that you can use as if you were writing a Python module to demonstrate, teach, or validate an idea.

Once in Python’s interactive mode, you can simply type Python code and press Enter to execute it, and if there are results available to be displayed, those results will be shown in the interactive mode. For example, the return value of a variable or function.

When the entered code is not complete enough to be executed properly, the interactive mode will ask for the remaining code to be added, and you can press Enter again to execute all previous input without entering any characters. If you want to write a blank line instead of ending the input, you need to write at least one whitespace character (for example, a space) before pressing Enter.

Next, we use the input function to ask the user to enter a name, and an if statement to determine if the input is empty.

name = input('Please enter your name:')
Please enter your name:…Enter
if not name:
... print('No name?!')
...…Enter
No name?!

How do I enter Python interactive mode?

You can enter Python interactive mode by typing python on the command line for Windows operating systems, or by typing python3 for UNIX/Linux/macOS systems. Of course, you can use other aliases that point to Python besides python and python3.

How do I exit Python interactive mode?

For Windows, pressing Ctrl+Z and Enter successively in Python interactive mode will exit interactive mode, and for UNIX/Linux/macOS, simply press Ctrl+D.

If the site module is imported at Python startup, typing exit() or quit() in interactive mode will also exit interactive mode. In fact, importing the site module is the default unless you have overridden the Python Module Search Path using the _pth file and have not written a valid (not commented) import site in _pth.