How to run a Python file in terminal: a clear, evergreen workflow
To run a Python file in terminal, first ensure Python is installed and on your system PATH, then navigate to the script’s directory and execute it with the interpreter. This evergreen explainer covers verification, execution patterns, common errors, and debugging steps so you can run Python scripts reliably on any major platform.
Verify your Python installation and PATH
Before you run scripts, confirm Python is installed and accessible from the terminal. Use version checks to validate availability and inspect the executable path to avoid surprises later.
Check Python and pip versions
- python --version or python3 --version
- pip --version
Confirm paths with which/where
- which python or where python
- which python3 or where python3
| Action | Command | Expected useful output |
|---|---|---|
| Check Python | python3 --version | Python 3.x.y |
| Check pip | pip --version | pip x.z.z from <path> |
| Find python location | which python3 | /usr/bin/python3 or C:\Python3x\python.exe |
Navigate to your script’s directory
The terminal’s current working directory determines where Python looks for files and relative imports. Use standard directory navigation commands to move to the folder containing your script before execution.
Common shell navigation commands
- cd path/to/folder
- cd .. to go up one level
- cd ~ or cd to go to your home directory
- pwd (macOS/Linux) or cd (Windows) to show full path
Run a Python file with the interpreter
The simplest way to run Python file in terminal is to call the interpreter with the script name. Use python or python3 depending on your platform and installation, and always type the filename exactly as it appears.
Execution syntax
- python script.py
- python3 script.py
Use the executable-bang (shebang) approach
If your script includes a shebang and has executable permissions, you can run it directly. This pattern is common for Unix-like systems but also works on Windows when file associations are set correctly.
Shebang and execution checklist
- Add #!/usr/bin/env python3 as the first line
- Set executable permission (chmod +x script.py on macOS/Linux)
- Run ./script.py from the directory containing the file
Handle arguments and environment considerations
Scripts often rely on correct working directories, virtual environments, and system paths. Understanding how Python resolves modules and how environment variables affect imports helps prevent common runtime errors.
Practical tips
- Use absolute imports when scripts live in nested packages.
- Activate virtual environments before running if dependencies are involved.
- Quote paths and arguments that contain spaces to avoid parsing issues.
Common errors and fixes
When execution fails, these frequent issues and remedies can help you quickly restore flow. Matching error patterns to causes reduces troubleshooting time and supports reproducible runs.
| Error symptom | Likely cause | Quick fix |
|---|---|---|
| ‘python’ is not recognized | Python not in PATH | Reinstall Python with Add to PATH or use the full interpreter path |
| ModuleNotFoundError | Missing dependencies or wrong cwd | pip install -r requirements.txt and check import paths |
| SyntaxError on older Python | Version mismatch | Run with the correct python3 version or update the script |
| Permission denied (Unix) | Run chmod +x script.py |