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 message instead, the sections below will help you figure out why.
Key Takeaways
- You need Python installed on your computer first — check by typing python --version in your terminal or command prompt.
- Navigate to the folder containing your .py file using cd commands, then run python filename.py or python3 filename.py.
- On Windows, you may need to use py filename.py instead if the python command does not work.
- If your file needs other Python packages to run, you must install them first using pip install package-name.
- Some Python files are meant to be run from the command line, while others are meant to be imported into other files — check what the file is supposed to do.
Check whether Python is installed
Before you can run any Python file, Python itself must be on your computer. Open your terminal (Mac and Linux) or command prompt (Windows) and type python --version. If you see a version number like 3.11.5, Python is installed. If you see "command not found" or "is not recognized", Python is not installed or not in your system path.
On Mac and Linux, try python3 --version instead — many systems have both Python 2 and Python 3, and Python 3 is what you want. On Windows, if neither python nor python3 works, download Python from python.org and run the installer. During installation, check the box that says "Add Python to PATH" — this lets you run Python from any folder.
Navigate to your file's folder
Your terminal or command prompt starts in a default location (usually your home folder). You need to move to the folder where your .py file is saved. Use the cd command to change directories.
On any system, type cd path/to/your/folder. For example, if your file is on your Desktop, type cd Desktop. If it's in a subfolder called "projects", type cd Desktop/projects. On Windows, you can also type the full path like cd C:\Users\YourName\Desktop. Type ls (Mac and Linux) or dir (Windows) to list the files in the current folder and confirm your .py file is there.
Run the file with the correct command
Once you're in the right folder, type the command to run your file. On Mac and Linux, use python3 filename.py. On Windows, try python filename.py first; if that does not work, try py filename.py. Replace "filename" with the actual name of your file.
If the file runs successfully, you will see its output in the terminal — either printed text, a result, or nothing at all, depending on what the file does. If you see an error message, read the next section.
Understand common error messages
"No such file or directory" means the terminal cannot find your file. Check that you are in the correct folder (use ls or dir to list files) and that you typed the filename correctly, including the .py extension.
"ModuleNotFoundError" means your file needs a Python package that is not installed. The error message will tell you which package — for example, "No module named 'requests'". Install it by typing pip install requests (or pip3 install requests on Mac and Linux). Then run your file again.
"SyntaxError" means there is a mistake in the code itself — a missing colon, mismatched parentheses, or similar. You will need to open the file in a text editor and fix the error. The error message usually tells you which line the problem is on.
"Permission denied" on Mac or Linux means the file is not marked as executable. Type chmod +x filename.py to fix this, then run the file again.
Install packages your file needs
Many Python files depend on external packages — libraries of code that do not come with Python by default. If your file imports a package that is not installed, you will get a ModuleNotFoundError. The error message tells you the package name.
Install packages using pip, which is Python's package manager. Type pip install package-name (or pip3 install package-name on Mac and Linux). For example, pip install numpy installs the numpy package. You only need to install each package once — after that, any Python file on your computer can use it.
If your file came with a requirements.txt file, that file lists all the packages it needs. Install them all at once by typing pip install -r requirements.txt.
Run Python files from a text editor or IDE
Instead of using the terminal, you can run Python files directly from a text editor or integrated development environment (IDE). Popular choices include Visual Studio Code, PyCharm, and IDLE (which comes with Python). Each has a "Run" button or menu option that executes the file without typing commands.
This approach is often easier for beginners because the editor handles the terminal work for you. However, understanding how to run files from the command line is useful when you work on servers, use other people's code, or need to automate tasks.
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 and what you should use. On many systems, python refers to Python 2 and python3 refers to Python 3. If python filename.py does not work, try python3 filename.py.
Can I run a Python file by double-clicking it?
On Windows, yes — if Python is installed and added to PATH, you can double-click a .py file to run it. On Mac and Linux, double-clicking usually opens the file in a text editor instead. The terminal method works on all systems and gives you more control.
Why does my file run but produce no output?
The file may have run successfully but simply does not print anything. Check the file's code to see what it is supposed to do. If it is supposed to print something and does not, there may be a logic error in the code itself.
How do I stop a Python file that is running?
Press Ctrl+C in the terminal. This sends an interrupt signal and stops the program. If the program is stuck in an infinite loop or waiting for input, this is the way to stop it.
Can I run a Python file from a different folder without navigating to it?
Yes. Instead of using cd to navigate, type the full path to the file: python /path/to/filename.py on Mac and Linux, or python C:\path\to\filename.py on Windows. This runs the file without changing your current folder.