Running a Python file means telling your computer to read and execute the code inside it
A .py file is a text file containing Python code. To run it, you need Python installed on your computer, then you open a terminal or command prompt and type a command that points to the file. The computer reads the code line by line and does what it says — whether that's printing text, doing math, reading data, or controlling other programs.
The exact steps depend on whether you use Windows, Mac, or Linux, and whether you want to run the file from a terminal or from a code editor. All three approaches work; they just look different on screen.
Key Takeaways
- Python must be installed on your computer before you can run any .py file; you can read it free from python.org.
- The simplest way to run a file is to open a terminal, navigate to the folder where the file lives, and type python filename.py or python3 filename.py.
- On Windows, you can also right-click a .py file and choose "Open with Python" if Python is set up as the default program.
- If you get an error saying "python is not recognized", Python either is not installed or your computer does not know where to find it.
Check that Python is installed on your computer
Before you run any file, verify that Python exists on your machine. Open a terminal or command prompt and type python --version or python3 --version. If Python is installed, you will see a version number like Python 3.11.5. If you see "command not found" or "is not recognized", Python is not installed or not set up in your system path.
If Python is not installed, go to python.org, read the latest stable version for your operating system, and run the installer. On Windows, make sure to check the box that says "Add Python to PATH" during installation — this lets your computer find Python from any terminal window.
Navigate to the folder where your .py file is located
Open a terminal (Mac or Linux) or Command Prompt (Windows). You start in your home directory, but your Python file might be on your Desktop, in Documents, or in a project folder somewhere else. Use the cd command to move to the right folder.
For example, if your file is on your Desktop, type cd Desktop. If it is in a folder called my_project inside Documents, type cd Documents/my_project. On Windows, the path uses backslashes: cd Documents\my_project. Once you are in the right folder, type ls (Mac or Linux) or dir (Windows) to list the files and confirm your .py file is there.
Run the file using the python command
Once you are in the correct folder, type python filename.py and press Enter. Replace filename with the actual name of your file. For example, if your file is called hello.py, type python hello.py.
On some computers, especially Mac or Linux, you may need to type python3 filename.py instead. This tells the system to use Python version 3 specifically. If one command does not work, try the other. The file will run, and any output will appear in your terminal window.
What to do if you get an error
"python is not recognized" means Python is not installed or not in your system path. Reinstall Python and make sure to check "Add Python to PATH" on Windows. On Mac or Linux, you may need to use python3 instead of python.
"No such file or directory" means your terminal is not in the folder where the file lives. Double-check the file name (including capital letters and the .py extension) and use cd to navigate to the correct folder. Type ls or dir to see what files are actually there.
Errors inside your code (like "NameError" or "SyntaxError") mean the Python file itself has a problem. These are not installation issues — they are mistakes in the code. Read the error message; it tells you which line has the problem and what went wrong.
Running a file from a code editor instead of the terminal
If you use a code editor like Visual Studio Code, PyCharm, or Thonny, you can run your file without opening a terminal at all. Open the file in the editor, look for a "Run" button (often a green triangle or play icon), and click it. The editor handles the terminal work behind the scenes and shows you the output in a panel at the bottom.
This approach is often easier for beginners because you do not have to remember terminal commands. The trade-off is that you are learning less about how your computer actually works. Many people start with an editor and move to the terminal later as they get more comfortable.
Running a Python file on Windows by double-clicking
On Windows, you can set up Python files so that double-clicking them runs the code directly. Right-click a .py file, choose "Open with", then select Python. If Python does not appear in the list, click "More apps" and look for "Python" or browse to find the Python executable file on your computer.
Once you set this up, double-clicking any .py file will run it. The output appears in a terminal window that closes when the program finishes, so you may not see the results if the program runs very fast. To keep the window open, add input("Press Enter to close") as the last line of your Python file.
Frequently Asked Questions
What is the difference between python and python3?
Python 2 is an older version that is no longer supported. Python 3 is the current version. On most modern computers, python3 runs Python 3, while python might run either version depending on how the system is set up. If one command does not work, try the other.
Can I run a Python file from a different folder without using cd?
Yes. Instead of navigating to the folder, type the full path to the file: python /Users/yourname/Desktop/filename.py on Mac or Linux, or python C:\Users\yourname\Desktop\filename.py on Windows. This runs the file from wherever your terminal is currently open.
Why does my Python file run but produce no output?
The file ran successfully — it just did not print anything. Python files only show output if the code contains print statements or other commands that produce visible results. If you want to see what happened, add print("File ran successfully") to your code and run it again.
Do I need to install anything besides Python to run a .py file?
Python itself is all you need for basic files. However, if your code uses external libraries (like NumPy or Requests), you must install those separately using a tool called pip. The error message will tell you which library is missing and how to install it.