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. That's it — Python will execute the file and show any output in the same window.
The exact command depends on your operating system and how Python is installed. Windows users often type python, while Mac and Linux users typically type python3 to specify the newer version. If one doesn't work, try the other.
If you get an error saying Python is not found, Python either isn't installed or your computer doesn't know where to find it. You can check by typing python --version or python3 --version. If that works, Python is installed — you just need to navigate to the right folder or use the full path to your file.
Key Takeaways
- The basic command is python filename.py from the terminal or command prompt in the same folder as your file.
- Mac and Linux users should use python3 instead of python to run Python 3 files.
- You can run a file from any folder by typing the full path: python /Users/yourname/Documents/script.py.
- If Python isn't recognized, you may need to install it or add it to your system PATH so your computer knows where to find it.
- Some files require libraries to be installed first — if you see an import error, you'll need to install the missing package using pip.
Running a file from the terminal on Windows
Open Command Prompt by pressing Windows key + R, typing cmd, and pressing Enter. Use the cd command to navigate to your file's folder. For example, if your file is on your Desktop, type cd Desktop. Then type python filename.py and press Enter.
If you see "python is not recognized", try python3 instead. If that also fails, Python may not be installed or may not be in your system PATH. You can check by typing python --version. If nothing appears, you'll need to install Python from python.org or add it to your PATH manually.
To use the full path instead of navigating folders, type python C:\Users\YourName\Documents\filename.py, replacing the path with your actual file location. This works from any folder.
Running a file from the terminal on Mac and Linux
Open Terminal (Mac: press Command + Space, type "Terminal", press Enter; Linux: right-click the desktop or open your applications menu). Use cd to navigate to your file's folder — for example, cd Documents or cd ~/Desktop. Then type python3 filename.py and press Enter.
Mac and Linux systems usually have Python 2 installed by default, which is outdated. That's why you use python3 instead of python. If python3 is not found, Python 3 may not be installed. You can install it using Homebrew on Mac (brew install python3) or your Linux package manager.
To run a file from anywhere without navigating folders, type the full path: python3 /Users/YourName/Documents/filename.py on Mac or python3 /home/username/Documents/filename.py on Linux.
Running a file by double-clicking it
On Windows, you can double-click a .py file to run it if Python is properly installed and associated with the file type. However, the window will close immediately after the script finishes, so you won't see the output. This method works best for scripts that save results to a file rather than display them on screen.
On Mac and Linux, double-clicking a .py file usually opens it in a text editor instead of running it. You'll need to use the terminal method instead. If you want to run a script by double-clicking on Mac, you can create a shell script or use an application launcher, but the terminal is simpler.
For scripts that need to show output, the terminal method is more reliable on all systems because you can see what happened and any error messages that appear.
Handling missing libraries and import errors
If your script uses external libraries — code written by other people that your script imports — you'll see an error like "ModuleNotFoundError: No module named 'requests'". This means the library isn't installed on your computer yet.
Install missing libraries using pip, Python's package manager. Open your terminal or command prompt and type pip install libraryname (or pip3 install libraryname on Mac and Linux). For example, to install the requests library, type pip install requests. Once installed, run your Python file again.
If pip is not recognized, Python may not be installed correctly, or pip may not be in your PATH. You can also try python -m pip install libraryname or python3 -m pip install libraryname, which works even if pip isn't directly accessible.
Checking which Python version you're using
Different versions of Python can behave differently, and some libraries only work with specific versions. To see which version is running when you type python, open your terminal or command prompt and type python --version. For python3, type python3 --version.
Most modern code requires Python 3.6 or newer. If you're running Python 2, it's outdated and no longer supported. You should install Python 3 instead. On Windows, download it from python.org. On Mac, use Homebrew: brew install python3. On Linux, use your package manager: sudo apt install python3 (Ubuntu/Debian) or sudo yum install python3 (Red Hat/CentOS).
Troubleshooting common errors
If you see "FileNotFoundError" or "No such file or directory", your file isn't in the folder you're currently in. Check the file name for typos, make sure you're in the right folder (type ls on Mac/Linux or dir on Windows to list files), or use the full path to your file instead.
If your script runs but produces no output, it may be working correctly — some scripts only save results to files or databases without printing anything. Add print() statements to your code to see what's happening, or check whether the script created any output files.
If you see "SyntaxError", your Python code has a mistake. The error message will tell you which line has the problem. Open the file in a text editor, find that line, and look for missing colons, mismatched parentheses, or incorrect indentation. Python is strict about spacing at the beginning of lines.
Frequently Asked Questions
What's the difference between python and python3?
Python 2 is outdated and no longer supported. Python 3 is the current version. On systems with both installed, python usually runs Python 2 and python3 runs Python 3. Always use python3 unless you have a specific reason to use the older version.
Can I run a Python file without opening the terminal?
On Windows, you can double-click a .py file if Python is installed, but the window closes immediately so you won't see output. On Mac and Linux, double-clicking opens the file in a text editor instead. The terminal method is more reliable and lets you see results and error messages.
What does "Python is not recognized" mean?
Your computer can't find Python in the locations it searches. Either Python isn't installed, or it's installed but not added to your system PATH. Install Python from python.org (Windows) or use Homebrew/your package manager (Mac/Linux), and make sure to check the box that says "Add Python to PATH" during installation on Windows.
How do I run a Python file from a different folder?
Use the full path to your file instead of just the file name. On Windows: python C:\Users\YourName\Documents\filename.py. On Mac/Linux: python3 /Users/YourName/Documents/filename.py. Replace the path with your actual file location.
Why does my script say it can't find a module?
Your script is trying to import a library that isn't installed. Use pip install libraryname (or pip3 on Mac/Linux) to install it, then run your script again. Check the error message for the exact library name.