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:
- Click the search icon (magnifying glass) in the bottom-left corner of the screen.
- Type “cmd” to display the command-line.
- Click the Command Prompt icon.
- Inside the terminal, type: “python” to start the Python interpreter.
- 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:
- 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:
python "my script.py"
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 Linux terminal
To run the code from the Linux terminal is similar.
To open the Python interpreter, type “python” and run code.
To exit 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.