curl follow redirects Guide: Master From Basics to Advanced
Enable and customize redirect handling in curl, preserve methods, debug chains, and integrate GoProxy for reliable scraping.
Oct 21, 2025
Beginner-friendly guide to run Python scripts: quick-start, REPL, venv activation, shebangs, -m, args, and exact troubleshooting fixes.
New to Python and wondering how to run a Python file in the terminal? This guide covers fast, copy-able commands to run Python scripts, plus clear definitions, the minimal 3-step workflow, optional next steps (venv, shebang, -m, args), and precise troubleshooting.
A one-block Quick Start so you can run a script now.
The minimal 3-step workflow every beginner needs.
Short definitions for absolute newbies.
Optional/next-step topics (venv, shebang, -m, args).
A troubleshooting table with exact error messages and one-line fixes..
Here is the fastest route to seeing output.
# Quick sanity check
python -c "import sys; print(sys.executable, sys.version)"
# macOS / Linux / WSL
cd ~/Desktop/project
python3 script.py
# Windows (PowerShell or Command Prompt)
cd C:\Users\You\Desktop\project
python .\script.py
# or Windows launcher
py script.py
# force Python 3 with the launcher:
py -3 script.py
# Create & use a virtual environment (optional but recommended)
python3 -m venv venv
# macOS / Linux
source venv/bin/activate
# PowerShell
.\venv\Scripts\Activate.ps1
# Command Prompt
venv\Scripts\activate.bat
python script.py
Python: a programming language you run with a program called the interpreter.
Interpreter: the python program that reads and executes .py files.
Script / .py file: a text file with Python code (example: hello.py).
Module / package: a reusable Python file or group of files you can import.
REPL: the interactive prompt (type python) where you can try Python commands live.
Virtual environment (venv): an isolated Python environment that keeps project dependencies separate.
Shebang: the #! line (e.g., #!/usr/bin/env python3) that tells Unix how to run a script.
PATH: an OS setting that tells the terminal where to find programs like python.
These are the only steps you need to run almost any Python script.
Windows: open Command Prompt or PowerShell.
macOS: open Terminal.
Linux: open Terminal (or press Ctrl+Alt+T).
cd path/to/folder
# verify file exists
ls # macOS/Linux
dir # Windows
# Unix-like (macOS/Linux/WSL)
python3 script.py
# Windows (PowerShell / cmd)
python .\script.py
# or
py script.py
# to force Python 3 with the launcher
py -3 script.py
If those three steps work, you’re done. If not, jump to the Troubleshooting table below.
Run these to isolate the problem quickly.
1. python --version (or python3 --version, or py --version) — is Python installed?
2. which python (macOS/Linux) or where python (Windows) — which binary will run?
3. In the target folder: python -c "print('ok')" — does Python run a simple one-liner?
If step 1 or 2 fails, you probably need to install Python or add it to PATH.
If step 3 prints ok but your script fails, the problem is inside the script (see the troubleshooting table).
Start the Python REPL for quick experiments:
python3
>>> print("hello from REPL")
>>> 2 + 2
4
>>> exit()
Use REPL to test small snippets before running a full script.
Linux: install via package manager (example for Debian/Ubuntu: sudo apt install python3).
macOS: use the system installer or a package manager.
Windows: run the official installer and check Add Python to PATH during setup.
1. Open "Edit environment variables" from the Start menu → click "Environment Variables..."
2. Under "User variables" (or "System variables") select Path → Edit → New.
3. Paste the folder that contains python.exe (example: C:\Users\You\AppData\Local\Programs\Python\Python3xx\).
4. Click OK, close terminals, open a new terminal, then run: python --version
Restarting the terminal is required for PATH changes to take effect.
These are not required to run a file but are useful to know as you progress.
Use REPL to try code line-by-line, inspect modules, and debug expressions.
python3 -m venv venv
# macOS / Linux
source venv/bin/activate
# PowerShell
.\venv\Scripts\Activate.ps1
# Command Prompt
venv\Scripts\activate.bat
# install deps inside venv
python -m pip install -r requirements.txt
If PowerShell blocks activation (Activate.ps1 cannot be loaded), use the Command Prompt activation (activate.bat) instead — safer for beginners.
Add at the top of script.py:
#!/usr/bin/env python3
Then:
chmod +x script.py
./script.py
Windows ignores shebang; use python script.py instead.
python -m http.server 8000
python -m package.module
-m runs a module as a script and arranges sys.path so imports work for packages.
Mini example:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("name")
parser.add_argument("--count", type=int, default=1)
args = parser.parse_args()
for _ in range(args.count):
print(f"Hello, {args.name}!")
Run with python greet.py Alice --count 3.
python script.py > output.txt # overwrite
python script.py >> output.txt # append
nohup python3 script.py > out.log 2>&1 &
For production use, prefer a process manager.
If your script runs in your editor but fails in a normal terminal, compare interpreters and packages in both environments:
which python # macOS/Linux
where python # Windows
python -c "import sys; print(sys.executable, sys.version)"
python -m pip list
If outputs differ, configure your editor to use the same interpreter (e.g., the project venv) or use the editor’s integrated terminal.
Error / symptom | Likely cause | Quick fix |
'python' is not recognized as an internal or external command | Python not on PATH (Windows) | Add Python folder to PATH or use py script.py; restart terminal. |
command not found: python | Python not installed / not in PATH (Unix-like) | Install Python or run /usr/bin/python3 script.py. |
ModuleNotFoundError: No module named 'xyz' | Wrong working directory or missing dependency | cd to project root; python -m pip install -r requirements.txt. |
bash: ./script.py: Permission denied | File not executable | chmod +x script.py or run python3 script.py. |
Activate.ps1 cannot be loaded because running scripts is disabled | PowerShell blocks activation | Use Command Prompt: venv\Scripts\activate.bat. |
Terminal window closes immediately after double-click | GUI run hides output | Run the script from a terminal or create a launcher that keeps the terminal open. |
SyntaxError / IndentationError | Code bug or wrong Python version | Fix the code or run with the correct interpreter (python3). |
No such file or directory | Wrong path or typo | Verify filename with ls/dir and correct the path. |
Quick debug command:
python -c "import sys; print(sys.executable, sys.version)"
1. When should I use python vs python3 vs py?
Use python3 on Unix-like systems to explicitly call Python 3; on Windows py is a launcher (py -3 forces Python 3).
2. Why use python -m?
It runs a module as a script and fixes package import paths so relative imports work.
3. Why does double-clicking a script often hide output?
The GUI runner may not open a terminal window — run scripts from a terminal to see errors and output.
4. Is it safe to run code from the internet?
Inspect code first, test in a disposable folder or venv, and never run untrusted code with admin/root privileges.
Debug interactively: python -m pdb script.py.
Schedule jobs: cron (Unix) or Task Scheduler (Windows).
Use system/process managers for services.
Consider containers for reproducible environments.
Improve CLI UX: use argparse or third-party libraries for robust command-line tools.
Learning to run Python from the terminal unlocks reproducible workflows, automation, and server-side development. Start with the minimal three-step flow—open a terminal, cd into your script, and run python script.py. Once that works, adopt optional best practices like virtual environments and python -m as your projects grow.
A few last tips:
< Previous
Next >