The fastest way to run a Python file from the terminal is to use the one-line command `python3 your_script_name.py.
Here’s how it works: The `python3` executable finds the Python interpreter (typically version 3.x), executes your script file, and outputs the results. You’ll need this file open in a text editor or saved on your filesystem, and you must be in the correct directory or use an absolute path.
What You'll Need
- Python 3 installed on your system (check with `python3 --version` in the terminal).
- A Python file (e.g., `
script.py`) saved on your computer. - A terminal window (Linux/macOS: open it via
Terminal; Windows: openCommand PromptorPowerShell). - Permissions to read the file (no read-only restrictions unless the file is locked).
Step-by-Step
-
Open your terminal.
On macOS/Linux: Press
Ctrl+Alt+Tor search for "Terminal."
On Windows: PressWin+R, typecmd, and hit Enter (for Command Prompt) orpowershell(for PowerShell). -
Navigate to the folder where your Python file is saved. Use one of these commands:
cd path/to/directory(replace with your actual path, e.g.,cd Documents/my_project).- If you’re unsure where it is, run
pwd(macOS/Linux) orGet-Location(Windows) to see your current directory.
cd ~/projects/scripts -
Run the Python file. Type the following command and press
Enter:python3 your_script_name.pyReplace:your_script_name.pywith your actual filename (e.g.,calculation.py).- If you’re unsure which Python version is installed, try
python your_script_name.pyfirst (Windows may default to Python 2).
sudo apt install python3on Ubuntu). -
Verify the script ran. Look for output in the terminal. If no output appears, check:
- The script has no syntax errors (fix them first).
- Your terminal has write permissions for the file’s directory.
If That Did Not Work
Start here:- Restart your terminal and try again.
- Check Python’s location with `which python3` (macOS/Linux) or `where python3` (Windows).
- Use an absolute path to the script:
python3 /full/path/to/your_script.py. - Run the file directly with a full filename:
python3 -m path.to.moduleif it’s in a package.
Common Mistakes
-
Wrong Python version: Use `python3` explicitly. Windows may default to Python 2.x, which won’t work (upgrade if needed).
Fix: Install Python 3 via [python.org](https://www.python.org/downloads/) and ensure it’s added to PATH during installation. -
File not found: The script may be in a different directory, or the filename contains spaces/special chars.
Fix: Use absolute paths (e.g.,/home/user/projects/scripts.py) or escape spaces with quotes:python3 "script with spaces.py". -
Permission denied: The file or directory lacks read/write access.
Fix: Run `chmod +x script.py` (Linux/macOS) or grant permissions via your editor (e.g., VS Code’s "Run in Terminal"). -
Syntax errors: Typos (e.g., missing colons, incorrect indentation) will block execution.
Fix: Use a text editor to inspect the script. Python’s built-in error messages show line numbers.
Tips
- Run scripts without arguments: Use `--file` or `argv[0]` in Python code to handle file paths dynamically.
-
Debug interactively: Launch Python in REPL mode first:
python3 -i your_script.py(interactive mode drops you into a prompt). -
Use shebang for shell execution: Add this at the top of your script to run it directly:
#!/usr/bin/env python3Then make it executable with `chmod +x script.py` and run it via:./script.py -
Version conflicts: Use `pyenv` to manage Python versions locally:
pyenv local 3.9.7before running scripts. -
Log output to a file: Redirect output with:
python3 script.py > output.log 2>&1(captures both stdout and stderr).
Troubleshooting
-
Terminal won’t recognize `python3`. Verify Python is installed:
python3 --versionIf missing, install it via your OS’s package manager (e.g.,sudo apt install python3) or [python.org](https://www.python.org/downloads/). -
Script runs but does nothing. Check:
- The script has executable permissions (run `ls -l script.py` to confirm).
- No hidden errors (run in REPL mode as noted above).
- Permission errors when running from a network drive. Use absolute paths or mount the drive locally.
-
Windows: "Python is not recognized." Add Python to PATH during installation or use the full path:
C:\Python39\python.exe script.py -
Slow execution. Check for resource-heavy operations (e.g., large file reads). Profile with:
python3 -m cProfile script.py
Frequently Asked Questions
What if Python isn’t installed?
Install Python 3 via your OS’s package manager (e.g., `sudo apt install python3` on Ubuntu) or download it from [python.org](https://www.python.org/downloads/). Ensure it’s added to PATH during installation.
How do I run a script with arguments?
Modify your script to accept arguments via the `sys.argv` list. Then run it with:
python3 script.py arg1 arg2
Example script:
import sys
print("Arguments:", sys.argv[1:])
Can I run Python scripts from anywhere?
Yes, if you add Python to your system PATH (during installation) or use absolute paths (e.g., /home/user/bin/script.py).
Why does my script fail silently?
Silent failures often mean Python crashed due to a hard error (e.g., memory issues). Check the script’s output or enable verbose mode:
python3 -v script.py
How do I run a script in a virtual environment?
Activate the environment first:
source venv/bin/activate (macOS/Linux) or
venv\Scripts\activate (Windows).
Then run the script as normal.
What if my script requires specific dependencies?
Use pip to install them:
pip install -r requirements.txt
If the script has dependencies not in your global pip, create a virtual environment.
Need more help? Check your script’s error messages or consult [Python’s documentation](https://docs.python.org/3/tutorial/).