Open Terminal, cd to the script’s folder, and run python filename.py. Use python3 on macOS/Linux or py on Windows.
You have a Python script ready to run, and the terminal is open. That final step—the command itself—is the one place where new users most often get stuck. This guide shows you how to execute Python in terminal on any operating system, covering the exact commands for Windows, macOS, and Linux, plus how to handle the most common errors that stop a script from launching.
Do You Need To Install Python First?
Before you can run any .py file, Python must be installed on your machine. Open your terminal and type python --version (or python3 --version). If you see a version number, you’re ready to go. If you see an error saying the command is not found, download the latest installer from Python’s official site. Real Python’s guide to running scripts also walks you through the entire setup process, including platform-specific installation steps.
Windows users should download the Windows installer (64-bit) unless running an older 32-bit computer. macOS users grab the macOS 64‑bit universal2 installer. Linux users can compile from source, though most distributions already include Python 3—if not, the apt or yum package manager is the fastest route.
The Generic Command For Running A Python Script
Once Python is installed and sitting in a folder with your .py file, three steps are all you need. This sequence works on every operating system with only the command name changing.
- Open your terminal. On Windows, launch Command Prompt or PowerShell. On macOS, open the Terminal app. On Linux, open your distribution’s terminal emulator.
- Navigate to the folder containing your script. Use
cd path\to\folderon Windows orcd path/to/folderon macOS/Linux. Runls(macOS/Linux) ordir(Windows) to confirm your.pyfile is listed. - Execute the script. Type
python script_name.pyand press Enter. Replacescript_name.pywith your actual file name.
If the script prints output, it will appear directly in the terminal window. If there are no output statements, the terminal simply returns to a new prompt—that means the script ran successfully with no errors.
| Operating System | Primary Command | Alternate Command |
|---|---|---|
| Windows | python script.py |
py script.py |
| macOS | python3 script.py |
python script.py (Python 2 only) |
| Linux (Ubuntu, Fedora, etc.) | python3 script.py |
python script.py (if Python 3 is the default) |
| Multiple Python versions | python3.11 script.py |
python3.12 script.py |
| Virtual environment active | python script.py |
Same, env resolves the interpreter |
Why The Wrong Command Name Is The Most Common Mistake
The single biggest reason a new script fails to run is using the wrong command name for the operating system. On Windows, the py launcher avoids confusion when multiple Python versions are installed. On macOS and many Linux distributions, python still points to Python 2 (which is now deprecated), so python3 is the safer bet. If you type python script.py and see an error, try python3 script.py (macOS/Linux) or py script.py (Windows) before troubleshooting anything else. Visual Studio Code’s documentation also notes that python3 is the explicit command on macOS/Linux, and the standard terminal workflow mirrors that distinction.
What If The Terminal Says “Python Is Not Recognized”?
When the terminal returns an error like 'python' is not recognized or command not found, the problem is either that Python is not installed or that the installer did not add it to your system’s PATH environment variable. On Windows, the quickest fix is to rerun the Python installer and make sure the box labeled Add Python to PATH is checked. On macOS and Linux, run which python3 to see if Python exists elsewhere—if it does, just use python3 instead of python. If the which command returns empty, install Python from the official download page.
If you are absolutely certain Python is installed but the terminal still cannot find it, close and reopen your terminal after the installation. Environment variable changes only take effect in new terminal sessions.
Running Scripts From VS Code’s Integrated Terminal
If you edit code inside Visual Studio Code, you can run your script without ever leaving the editor. Press Ctrl+` (backtick) or go to Terminal > New Terminal to open the built-in shell. VS Code automatically activates any virtual environment configured for the workspace, so python or python3 uses the expected interpreter. You can then run the same cd and python filename.py commands as you would in a stand-alone terminal. Microsoft’s VS Code documentation also highlights the Run Python File in Terminal button in the top‑right corner, which handles both the environment selection and the command execution in one click.
| Error Message | Likely Cause | Fix |
|---|---|---|
'python' is not recognized |
Python not in PATH or not installed | Reinstall with “Add Python to PATH” checked |
No such file or directory |
Terminal is not in the script’s folder | Use cd to correct folder or provide full path |
SyntaxError |
Python 2 code on Python 3 (or vice versa) | Check shebang line or switch python/python3 |
ModuleNotFoundError |
Required package is not installed | Run pip install package_name |
A Dependable Workflow For Every New Python Script
Getting a Python file to run from the terminal comes down to three fixed checks: install Python if missing, run the correct command for your operating system, and make sure your terminal is pointing at the right folder. With those basics locked down, you can launch any script in seconds. For deeper details on managing virtual environments, running Python in the background, or scheduling scripts, the official Python documentation and Real Python’s tutorials are excellent next stops.
References & Sources
- Real Python. “How to Run Your Python Scripts.” Covers the exact command-line steps for all major operating systems, plus common errors.
- Python Software Foundation. Python Downloads. Official source for the latest Python installer.
- Microsoft. “Python in Visual Studio Code – Running Python Code.” Explains the integrated terminal and the Run Python File button.
