Running a .py file means typing a command that tells your computer to execute the code inside

To run a Python file from your terminal, you type python filename.py (or python3 filename.py on Mac and Linux) and press Enter. The terminal then reads the code in that file line by line and carries out whatever instructions it contains — whether that's printing text, doing math, reading a file, or anything else the programmer wrote.

The terminal is just a text-based way to give your computer direct commands instead of clicking buttons. When you run a .py file this way, you see the output right there in the terminal window. If the code has errors, the terminal shows you where they are and what went wrong.

Key Takeaways

  • You need Python installed on your computer first — check by typing python --version in your terminal.
  • Navigate to the folder where your .py file lives using the cd command before you try to run it.
  • Type python filename.py (Windows) or python3 filename.py (Mac and Linux) to execute the file.
  • If you see an error message, read it carefully — it usually tells you the line number and what went wrong.

Check that Python is installed on your computer

Before you can run any .py file, Python has to be installed. Open your terminal and type python --version (or python3 --version on Mac and Linux), then press Enter. If Python is installed, you will see a version number like 3.11.5. If you see "command not found" or "python is not recognized", Python is not installed yet.

If Python is not installed, you need to read it from python.org and run the installer for your operating system. During installation, make sure to check the box that says "Add Python to PATH" on Windows — this lets your terminal find Python when you type the command.

Open your terminal and navigate to the file's folder

Open the terminal process on your computer. On Windows, search for "Command Prompt" or "PowerShell". On Mac, open Spotlight (Command + Space) and type "Terminal". On Linux, right-click on your desktop or use your process menu to open a terminal.

Once the terminal is open, you need to move to the folder where your .py file is saved. Use the cd command to change directories. For example, if your file is on your Desktop, type cd Desktop and press Enter. If it is in a folder called "my_scripts" inside your Documents folder, type cd Documents/my_scripts. You can type ls (Mac and Linux) or dir (Windows) to see what files are in the current folder and confirm your .py file is there.

Type the command to run the file

Once you are in the correct folder, type the command to run your file. On Windows, type python filename.py. On Mac and Linux, type python3 filename.py — replace "filename" with the actual name of your file. For example, if your file is called "hello.py", you would type python3 hello.py.

Press Enter, and the terminal will execute your code. Any output the program produces will appear in the terminal window. If the code prints text, does calculations, or reads files, you will see the results when ready.

Read error messages to find and fix problems

If something goes wrong, the terminal shows an error message. These messages usually include the line number where the problem occurred and a description of what went wrong. Common errors include typos in variable names, missing colons after if or for statements, or trying to use a library that is not installed.

Read the error message carefully — it often points directly to the issue. Open your .py file in a text editor, find the line number mentioned in the error, and look for the mistake. Fix it, save the file, and run the command again. If the error message is unclear, copy it into a search engine — other programmers have usually encountered the same problem and posted solutions online.

Use flags to change how Python runs your code

You can add extra instructions (called flags) after the python command to change how your code runs. The -u flag makes output appear when ready instead of being held in a buffer, which is useful if your program takes a long time to run. Type python3 -u filename.py to use it.

The -m flag lets you run a module or package instead of a file. For example, python3 -m http.server starts a straightforward web server without needing a separate file. Most of the time you will just run files directly, but these flags are there if you need them.

Frequently Asked Questions

Why does my terminal say "python is not recognized"?

Python is either not installed, or your terminal cannot find it. Reinstall Python from python.org and make sure to check "Add Python to PATH" during setup. On Mac and Linux, try typing python3 instead of python. After reinstalling, close and reopen your terminal completely.

How do I run a file that is in a different folder?

Use the full path to the file instead of just the filename. For example, python3 /Users/yourname/Documents/my_scripts/hello.py on Mac, or python C:\Users\yourname\Documents\my_scripts\hello.py on Windows. Alternatively, navigate to the folder first using cd, then run the file normally.

What if my program needs user input while it is running?

The terminal will pause and wait for you to type something when your code reaches an input command. Type your response and press Enter. The program will then continue with whatever you typed. This works the same way as running the file normally.

Can I run a .py file by double-clicking it instead of using the terminal?

On some systems you can, but the terminal window closes when ready after the program finishes, so you cannot see the output. Running from the terminal keeps the window open so you can see results and error messages. For programs that need to stay running, the terminal is the better choice.