The fastest way to run a Python file
Open your terminal or command prompt, navigate to the folder where your Python file lives, and type python filename.py (or python3 filename.py on Mac and Linux). Press Enter. If Python is installed and your file has no errors, it will run.
That's the core of it. The rest depends on your operating system, whether you have Python installed, and what your file is supposed to do. If you get an error like "python is not recognized" or "command not found", Python either isn't installed or your system doesn't know where to find it.
Key Takeaways
- You run a Python file by opening a terminal or command prompt, moving to the file's folder, and typing python filename.py.
- Your computer must have Python installed first; you can check by typing python --version in the terminal.
- On Mac and Linux, use python3 instead of python to avoid running an outdated version.
- If your file produces output, it appears in the terminal window; if it creates files or connects to the internet, those actions happen in the background.
- You can also run Python files by double-clicking them in Windows if Python is set up to handle .py files, though using the terminal gives you more control.
Check whether Python is installed
Before you can run a Python file, Python needs to be on your computer. Open your terminal (Mac and Linux) or command prompt (Windows) and type python --version. If a version number appears, Python is installed. If you see "command not found" or "is not recognized", you need to install it first.
On Windows, download Python from python.org, run the installer, and make sure to check the box that says "Add Python to PATH" during setup. This tells Windows where to find Python when you type the command. On Mac, you can use Homebrew (a package manager) by typing brew install python3, or download the installer from python.org. On Linux, Python usually comes pre-installed, but if not, use your system's package manager — for Ubuntu or Debian, type sudo apt install python3.
Navigate to your file's location in the terminal
The terminal starts in your home directory by default. To run a Python file, you need to move to the folder where it's stored. Use the cd command (short for "change directory") followed by the path to your folder.
For example, if your file is on your Desktop, type cd Desktop. If it's in a folder called "Projects" inside your Documents folder, type cd Documents/Projects. On Windows, use backslashes instead: cd Documents\Projects. Once you're in the right folder, type ls (Mac and Linux) or dir (Windows) to list the files and confirm your Python file is there.
Run the file with the correct Python command
Once you're in the file's folder, type python filename.py and press Enter. Replace "filename" with the actual name of your file — for example, python hello.py or python script.py. The file will run immediately.
On Mac and Linux, use python3 instead of python. This matters because some older systems still have Python 2 installed as the default "python" command, and Python 2 is outdated. Using python3 ensures you're running the current version. On Windows, python usually points to Python 3 if you installed it recently, but python3 works too.
Understand what happens when your file runs
When you press Enter, Python reads your file line by line and executes the code. If your file prints text to the screen (using the print() function), you'll see that text appear in the terminal. If your file has errors, Python will stop and show you an error message with a line number telling you where the problem is.
If your file creates new files, downloads data, or connects to the internet, those actions happen silently in the background — you won't see them in the terminal unless the code explicitly prints a message. Once the file finishes running, the terminal returns to the prompt and waits for your next command.
Run a Python file by double-clicking (Windows only)
On Windows, if Python is installed and set up correctly, you can double-click a .py file in File Explorer and it will run. However, the terminal window opens and closes so fast you may not see any output. This method works for files that create other files or perform background tasks, but for files that print results to the screen, using the terminal is better because you can see the output before the window closes.
If double-clicking doesn't work, right-click the file, select "Open with", and choose Python. If Python doesn't appear in the list, your system isn't configured to recognize .py files — in that case, use the terminal method instead.
Troubleshoot common errors
If you see "python is not recognized" or "command not found", Python isn't installed or isn't in your system's PATH. Reinstall Python and make sure to check "Add Python to PATH" on Windows, or verify the installation on Mac and Linux by typing which python3.
If you see "No such file or directory" or "cannot find the file", you're in the wrong folder. Use ls or dir to list the files in your current location and confirm the filename matches exactly — including capitalization and the .py extension. If you see an error inside your Python code (like "NameError" or "SyntaxError"), the file ran but the code itself has a problem. Read the error message and the line number it points to, then fix the code in your text editor and try again.
Frequently Asked Questions
What's the difference between python and python3?
Python 2 is old and no longer supported. Python 3 is the current version. On most modern systems, python points to Python 3, but on some older Macs and Linux machines, python still means Python 2. Using python3 guarantees you're running the current version and avoids confusion.
Can I run a Python file without opening the terminal?
On Windows, you can double-click a .py file if Python is installed. On Mac and Linux, you can make a file executable and run it directly, but this requires extra setup. The terminal method works on all systems and gives you the most control and visibility into what's happening.
Why do I need to navigate to the file's folder?
Python looks for your file in the current directory. If you don't navigate to the right folder, Python won't find it. You can also type the full path to the file instead — for example, python /Users/yourname/Desktop/script.py — but navigating first is simpler for most people.
What if my Python file needs to read another file?
If your Python code opens a file (like a CSV or text file), make sure that file is in the same folder as your Python script, or provide the full path to it in your code. Running the script from the terminal in that folder ensures Python can find the other files.
Can I run a Python file from a different folder without navigating there?
Yes. Instead of using cd to navigate, type the full path: python /path/to/your/filename.py on Mac and Linux, or python C:\path\to\your\filename.py on Windows. This works from any location in the terminal.