This browser does not support JavaScript

Run a Python File in Terminal: A Step-by-Step Guide for Beginners

Post Time: 2025-10-20 Update Time: 2025-10-20

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.

What You’ll Get from This Guide

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..

Quick Start (Copy & Run)

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

Quick Definitions (for Absolute Beginners)

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.

The Minimal 3 Step (Do This First)

Run a Python File in Terminal

These are the only steps you need to run almost any Python script.

Step 1. Open a terminal

Windows: open Command Prompt or PowerShell.

macOS: open Terminal.

Linux: open Terminal (or press Ctrl+Alt+T).

Step 2. Navigate to the folder containing your script

cd path/to/folder

# verify file exists

ls   # macOS/Linux

dir  # Windows

Step 3. Run the script

# 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.

If Nothing Works: 3 Step Checks

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).

Quick REPL Mini-block (Interactive Testing)

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.

Install & PATH: What to Do If python/python3 is Missing

Minimal install hint

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.

Add Python to PATH (Windows)

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.

Optional / Next Steps (After Minimal 3 Steps)

These are not required to run a file but are useful to know as you progress.

Run Python interactively (REPL)

Use REPL to try code line-by-line, inspect modules, and debug expressions.

Use virtual environments (venv) — recommended for projects

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.

Make scripts executable (Unix-like) — shebang + chmod

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.

Run modules & packages with -m

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.

Accept command-line arguments (argparse)

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.

Redirect output / run in background (Unix-like)

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.

Run without an open terminal (one-click)

  • Windows: create a .bat wrapper that calls Python and add pause to keep the window open.
  • Unix-like: create a desktop launcher that opens a terminal and runs your script.

Editor vs Terminal: Why Something Works in One & Not the Other

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.

Troubleshooting

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)"

FAQs

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.

Advanced Tips & Next Steps

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.

Final Thoughts

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:

  • Run the three quick checks (python --version, which/where python, python -c "print('ok')") to diagnose most setup issues.
  • Use a virtual environment for each project to avoid dependency conflicts.
  • Inspect code you find online before running it—especially scripts that touch files or run shell commands.

< Previous

Mastering curl get: From Basics to Pro

Next >

How to Scrape eBay Safely and Effectively: 2025 Step-by-Step Guide with GoProxy
Start Your 7-Day Free Trial Now!
GoProxy Cancel anytime
GoProxy No credit card required