How to Use Command Line with Python

Python allows you to run a script in multiple different ways. One of them is a Command Line. In this lesson, I’ll show you how you can test your python script directly from the command line and python terminal.

Command line on Windows

To find a command line on Windows, you have to click the magnifying glass, or text box, depending on your configuration, and type cmd. This is the shortcode for a command line on Window.

First, check whether the system path is set up. It should be if you chose it during installation.

To do it just type python. If it is recognized, it will display the message informing you about the version of python you are currently using. When you see three greater signs (>>>), this means that you are inside the Python interpreter.

You can exit the command using the Ctrl + Z keyboard shortcut or typing exit() in the console.

If the command is not recognized, it going to return a different message.

'python' is not recognized as an internal or external command,
 operable program or batch file.

Setting the system path

If Python is installed on your system, but Windows doesn’t recognize the python command, you have to set up the path to your Python directory, where the python.exe file is located.

To get there, click the windows search icon again and type: environment variables.

In the System Properties window, there is the Environment Variables button.

In the System variables area, there is the Path variable. Click Edit and the New.

Running Python scripts

After we have everything set up correctly, we can execute our first script from the command line.

Type python and press Enter to open the interpreter console and write the first function.

The message is automatically displayed on the console.

You can also run more complicated code, consisting of multiple lines. Let’s create a simple function that prints “Hello World!”.

First, you have to create the first line. This is going to be a function name. After you end it with a colon, press Enter to move to the next line.

The continuation line is indicated by the three dots ().

When you type, remember that indentation is necessary, otherwise, you will see an error, the same as you would see inside a Python editor.

If you finish defining the function, you’ll have to press Enter twice to leave the continuation line; first, enter for a new line and the second one for ending continuation.

Now, you can see three greater signs (>>>) again. Type the function name and it’s going to be executed. This is exactly what happened in our example.

Stopping Python scripts while running

If you want to stop the execution of the code inside the terminal, you can use the Ctrl + C keyboard shortcut to terminate the program.

Let’s take a look, how it works using the sleep function.

Instead of waiting for a function to finish, press Ctrl + C.

The message KeyboardInterrupt will appear, informing us that the operation was terminated before the function could end normally.

Line breaks in Python interpreter

When you have a long line of code inside an editor you can use a backslash and continue to write the same line of code on the second line in the editor. It’s the same with the command line.

You start the line of code, put backslash, and continue in the next line inside the terminal.

Redirecting output to a file

So far, you have displayed the result inside a console. But the same result can be redirected and saved as a file. This can be achieved using a single “greater” operator (>).

As an example, we are going to use the Fibonacci sequence.

If you run the script inside the terminal, you will get this result.

If you redirect the stream to a file, there will be no message, and the result will be saved as a file.

If you open the output.txt file, there is data that was displayed on a terminal before.

The file will be created each time you run the command. If it’s already present, it will be overwritten.

If you want to append data to a file, instead of creating the new one each time, you can use the double “greater” operator. (>>).

This command will also create the output.txt file as the last one, but this time it will append the result to the end if the file is already present.

If you run the operation twice, you will get the following result.

Longer commands with a line break

When you enter the Python terminal, you can continue writing the same line of code in the next line in the terminal using the backslash.

You can also continue the same line, inside the command line, before you enter the Python interpreter. This time we are not going to use the backslash, but the caret character.

This is how it works.

After you place caret and hit Enter, there is the “More?” text. You continue writing code there. It works the same as if it was written as a single line.

Command line arguments (argparse)

Command line arguments are parameters that are typed just after the python keyword. It can be a single parameter or multiple parameters.

Take a look at this script (script.py):

Now, run the following command:

It will return 4 as the number of arguments. The file name is the first parameter.