Run Python From Terminal

After you install Python on your computer, you can run scripts in a few different ways. One of these ways is terminal. In this lesson, I’ll show you how to do it.

Running Python code from the Windows terminal

To run Python from a terminal on Windows:

  1. Click the search icon (magnifying glass) in the bottom-left corner of the screen.
  2. Type “cmd” to display the command line.
  3. Click the Command Prompt icon.
  1. Inside the terminal, type: “python” to start the Python interpreter.
  1. You can type Python code, and the Python interpreter will automatically interpret it for you.

type exit() to quit the interpreter.

Running Python script from Windows terminal

If your file is inside the current directory, you can run this command:

If your file is not located inside the same directory, you have two options:

First option

  1. Inside the terminal, navigate to the directory where the file is located and run the code.

If the has spaces in the name, you have to use double quotes:

Second option

With the second option, you don’t have to leave the current directory. Just add the full path before the word “python”.

Running Python code from a Linux terminal

Running the code from the Linux terminal is similar.

To open the Python interpreter, type “python” and run the code.

To exit the interpreter, type exit() and press Enter, or use the Ctrl + D keyboard shortcut.

To run the script, navigate to the directory where the file is located and run the command:

Another option is to add the full path to the file, instead of the file name only.