The fastest way to run a Python file
To run a Python file, open your terminal or command prompt, navigate to the folder where the file lives, and type python filename.py (or python3 filename.py on Mac and Linux). Press Enter. If Python is installed and the file has no errors, it will run.
That's the core of it. The rest of this guide covers what to do when that doesn't work, how to know if Python is installed, and how to run files from different places on your computer.
Key Takeaways
- Python must be installed on your computer before you can run any Python file; check by typing python --version in your terminal.
- You need to navigate to the folder containing your Python file using cd commands before you can run it.
- On Windows, use python filename.py; on Mac and Linux, use python3 filename.py.
- If you see a "command not found" or "is not recognized" error, Python is either not installed or not in your system's PATH.
Check if Python is installed
Open your terminal (Mac and Linux) or Command Prompt (Windows). Type python --version and press Enter. If Python is installed, you'll see a version number like Python 3.11.5. If you see "command not found" or "is not recognized as an internal or external command", Python is not installed or not set up correctly.
On Mac and Linux, also try python3 --version — many systems have both Python 2 and Python 3, and Python 3 is what you want. On Windows, python --version is usually the right command.
If Python is not installed, download it from python.org. On Windows, make sure to check the box that says "Add Python to PATH" during installation — this lets your computer find Python from anywhere in the terminal.
Navigate to your file's folder
Your terminal starts in your home directory. If your Python file is on your Desktop or in a folder called Projects, you need to move there first using the cd command (which stands for "change directory").
For example, if your file is on your Desktop, type cd Desktop and press Enter. If it's in a folder called Projects inside your home directory, type cd Projects. If it's nested deeper — say, Projects/learning/script.py — type cd Projects/learning.
To see what files are in your current folder, type ls (Mac and Linux) or dir (Windows) and press Enter. You should see your Python file listed there before you try to run it.
Run the file with the correct command
Once you're in the right folder and Python is installed, type the command. On Windows, use python filename.py. On Mac and Linux, use python3 filename.py. Replace "filename" with the actual name of your file.
Press Enter. If the file runs without errors, you'll see the output in your terminal. If there's a mistake in the code, Python will show you an error message with a line number — that tells you where to look in the file to fix it.
If you see "No such file or directory" or "cannot find the file", you're either in the wrong folder or the filename is spelled differently than you typed. Use ls or dir again to check the exact spelling.
Run a file from a different folder without navigating
If you don't want to use cd to move folders, you can tell Python the full path to the file. On Windows, type python C:\Users\YourName\Desktop\filename.py. On Mac and Linux, type python3 /Users/YourName/Desktop/filename.py.
Use forward slashes on Mac and Linux, and backslashes on Windows. If the path has spaces in it — like "My Documents" — wrap the whole path in quotes: python "C:\Users\YourName\My Documents\filename.py".
Understand common error messages
"command not found" or "is not recognized": Python is not installed or not in your PATH. Reinstall Python and make sure to check "Add Python to PATH" on Windows, or verify the installation on Mac and Linux by checking the version again.
"No such file or directory": You're in the wrong folder, or the filename is spelled differently. Use ls or dir to see what files are actually there.
"SyntaxError" or other code errors: Your Python file has a mistake in it. The error message will tell you which line. Open the file in a text editor and fix the problem on that line.
"ModuleNotFoundError": Your file is trying to use a library that isn't installed. You'll need to install it using pip install libraryname before running the file again.
Run Python files by double-clicking (Windows and Mac)
On Windows, you can right-click a .py file, select "Open with", and choose Python. On Mac, you can double-click a .py file if it has a shebang line at the top — the first line should be #!/usr/bin/env python3. However, this method doesn't show you the output clearly, so the terminal method is more reliable for learning.
If you want a Python file to run when you double-click it on Windows, you can create a batch file (.bat) in the same folder that contains python filename.py and then double-click the batch file instead. This is less common and the terminal method is usually simpler.
Frequently Asked Questions
Do I need to install anything besides Python?
No, not to run basic Python files. Python itself is all you need. If your file uses external libraries like NumPy or Requests, you'll need to install those separately using pip install libraryname, but the Python interpreter itself is the only requirement to get your free guide.
What's the difference between python and python3?
Python 2 is old and no longer supported. Python 3 is the current version. On Windows, python usually means Python 3. On Mac and Linux, python might mean Python 2 if it's still on the system, so use python3 to be sure you're running the right one.
Can I run a Python file from inside a text editor?
Yes, most text editors and all IDEs (like Visual Studio Code, PyCharm, or IDLE) have a "Run" button or menu option. This is convenient while you're writing and testing, but understanding how to run files from the terminal is important because it's how Python actually works on servers and in real projects.
Why does my file run but nothing happens?
Your file might not have any output. If it's supposed to print something, make sure it has print() statements in the code. If it's supposed to create a file or change something, check your Desktop or the folder where you ran it from — the results might be there instead of in the terminal.