PythonMastery
beginner 20 min read · lesson 1 of 14 in Python How-To

The Terminal for Python Developers

1 · The lesson

read

Everything on this site runs in your browser. The moment you write Python on your own computer, install a package, make a virtual environment or use Git, you meet the terminal: a window where you type commands instead of clicking. It looks unfriendly for about a day. After that it's faster than any menu, and every guide you'll ever read assumes you can use it.

You need about ten commands. This lesson teaches them, and the three error messages that send beginners to a search engine.

The outputs below are real. The Windows ones come from PowerShell; the macOS and Linux ones from bash, which is what those systems run (macOS uses zsh by default, which behaves the same for everything here). Your username, folder names, dates and version numbers will be different from ours; everything else should match.


1. Open One

  • Windows: press the Windows key, type Terminal, press Enter. On older Windows 10, type PowerShell instead.
  • macOS: press Cmd + Space, type Terminal, press Enter.
  • Linux: Ctrl + Alt + T on Ubuntu and most others, or find Terminal in your apps.

You'll see a prompt: a short line that ends in $ (macOS, Linux) or > (Windows), with a blinking cursor after it. The prompt usually shows which folder you're in:

text
ada@laptop:~$
PS C:\Users\ada>

In guides, including this one, commands are often written after a $. Don't type the $; it stands for the prompt. Type what comes after it, then press Enter.


2. Where Am I?

A terminal is always "in" one folder, called the working directory. Two commands tell you where you are and what's there. pwd prints the folder; ls lists what's in it.

macOS / Linux:

bash
pwd
text
/home/ada
bash
ls
text
Documents
Downloads

Windows PowerShell understands the same two words and answers in more detail:

powershell
pwd
text
Path
----
C:\Users\ada

~ (tilde) is shorthand for your home folder, /home/ada or /Users/ada on Mac, C:\Users\ada on Windows. You'll see it in prompts and in commands.

The old Windows Command Prompt (cmd, black window, prompt like C:\Users\ada> without PS) uses cd with no folder name to print where you are, and dir instead of ls. Use PowerShell or Windows Terminal instead: the commands in this lesson then work as written.


3. Moving Around and Making Folders

mkdir makes a folder; cd (change directory) moves into it.

bash
mkdir projects
cd projects
pwd
text
/home/ada/projects

The rest of cd:

bash
cd ..            # up one level, to the folder that contains this one
cd ~             # straight home, from anywhere
cd projects/web  # down two levels at once (Windows accepts / here too)

Two habits save a lot of typing and mistakes:

  • Tab completes names. Type cd proj and press Tab; the terminal fills in projects. If nothing happens, either nothing matches or several things do (press Tab twice to see them).
  • Quote names with spaces. cd My Projects is read as two separate words. Use cd "My Projects", or just avoid spaces in folder names you create for code.

4. Run a Python Script

Create a file called hello.py in your editor (VS Code, IDLE, anything) and save it inside the projects folder:

python
name = input("What is your name? ")
print(f"Hello, {name}!")

Then, in the terminal, from that same folder:

bash
ls
text
hello.py
bash
python hello.py
text
What is your name? Ada
Hello, Ada!

That's the whole loop: write in the editor, save, run in the terminal, read the output, repeat.

The name of the command differs by system:

SystemCommandWhy
Windowspython (or py)py is the Python launcher that installs with Python for Windows
macOSpython3Apple ships no python command; python3 comes with the Xcode Command Line Tools or a python.org install
Linux (Ubuntu, Debian)python3Ubuntu has no plain python command unless you install the python-is-python3 package

Inside a virtual environment, python works everywhere, which is one more reason to use one. Check what you've got with:

bash
python --version
text
Python 3.13.12

5. Stop a Program: Ctrl + C

Some programs don't end by themselves: a server, a game loop, or a while True you wrote by accident. Here's one that ticks every second, forever. Save it as ticker.py and run it in your terminal (it never ends, so it's not one to run on this page):

python · run on your machine
import time

count = 0
while True:
    count += 1
    print("tick", count)
    time.sleep(1)

Run it, wait a few ticks, then press Ctrl + C (on a Mac too: Control, not Command):

text
tick 1
tick 2
tick 3
tick 4
Traceback (most recent call last):
  File "/home/ada/projects/ticker.py", line 7, in <module>
    time.sleep(1)
    ~~~~~~~~~~^^^
KeyboardInterrupt

The traceback looks like an error, and technically it is one: Ctrl + C raises KeyboardInterrupt inside your program, and nothing caught it. That's exactly what you wanted. Your program stopped and you have your prompt back.


6. The Three Errors Everyone Meets

"can't open file … No such file or directory"

text
C:\Users\ada\AppData\Local\Programs\Python\Python313\python.exe: can't open file 'C:\\Users\\ada\\projects\\missing.py': [Errno 2] No such file or directory

Python looked for the file in the folder the terminal is in, and it isn't there. Either you're in the wrong folder or the name is slightly different. Run pwd and ls, and compare. The most common cause: the script is in Downloads and the terminal is in your home folder. cd Downloads (or wherever it is) and try again.

"is not recognized" (Windows) or "command not found" (macOS, Linux)

text
The term 'pythn' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

The terminal couldn't find a program with that name. Either it's a typo (as here), or the program is installed somewhere the terminal doesn't look. The places it looks are a list called PATH. If python itself is "not recognized" right after you installed it, Python isn't on your PATH; the dedicated error page walks through the fix.

To see which program a name runs, ask:

powershell
where.exe python        # Windows
text
C:\Users\ada\AppData\Local\Programs\Python\Python313\python.exe
C:\Users\ada\AppData\Local\Microsoft\WindowsApps\python.exe
bash
which python3           # macOS / Linux

Two results is normal, and the first one wins. The WindowsApps one on Windows isn't a real Python: it's a shortcut that opens the Microsoft Store. If it's the only line you see, Python isn't installed properly yet.

SyntaxError on a command that looks right

text
>>> python hello.py
  File "<stdin>", line 1
    python hello.py
           ^^^^^
SyntaxError: invalid syntax

See the >>>? You're not in the terminal any more; you're inside Python's own interactive prompt, which happens if you type python on its own. Python is reading your command as Python code. Type exit() (or press Ctrl + D on macOS/Linux, Ctrl + Z then Enter on Windows) to get back to the terminal, then run python hello.py.


7. Installing Packages from the Terminal

pip installs packages, and it runs in the terminal, not inside Python. The most reliable form names the Python it should install for:

bash
python -m pip install requests

Plain pip install requests usually works too, but on a machine with two Pythons (see where.exe above) pip can belong to a different one than python, and then import requests fails even though the install "succeeded". python -m pip removes the doubt. Do it inside a virtual environment and the question never comes up.


8. Small Habits That Make It Pleasant

  • Up arrow brings back the last command; press it again for earlier ones. Edit and press Enter to run it again.
  • Tab completes file and folder names (section 3). Use it constantly; it also catches typos, because a misspelt name won't complete.
  • Copy and paste aren't always Ctrl + C / Ctrl + V, because Ctrl + C means "stop". In Windows Terminal, Ctrl + C copies only when text is selected and Ctrl + V pastes. On Linux use Ctrl + Shift + C and Ctrl + Shift + V. On a Mac, Cmd + C and Cmd + V work as usual.
  • clear (or Ctrl + L) wipes the screen when it's full; nothing is undone.
  • Open a terminal in a folder directly: in VS Code, Terminal → New Terminal opens one already in your project folder. On Windows, right-click a folder → Open in Terminal.

Common Mistakes

1. Typing the $ from a guide

$ python hello.py makes the terminal look for a program called $. The $ stands for the prompt; copy what comes after it.

2. Running the script from the wrong folder

The single most common cause of "can't open file". pwd, then ls, then cd to where the file really is.

3. Typing terminal commands at the >>> prompt

python hello.py, pip install, cd all belong to the terminal. At >>> you're inside Python. exit() first.

4. Using python on macOS or Ubuntu

There's no such command there by default; it's python3. Or activate a virtual environment, where python exists everywhere.

5. Closing the terminal while a program is running

Closing the window stops whatever was running in it. For a script that should keep going, keep the window open (minimise it), or schedule the script properly; the automation lesson shows how.

6. Spaces in folder names

cd My Projects fails; cd "My Projects" works. For folders that hold code, my-projects saves you the quotes forever.


🎯 Your Turn — Know Where You Are, From Inside Python

The terminal's working directory matters to your programs too: open("data.csv") looks for the file in that folder, which is why scripts work from one place and fail from another.

Write a script whereami.py that:

1. Prints the folder it's running in.
2. Prints the folder the script file itself is saved in.
3. Lists every .py file in the working directory, sorted.

Run it from its own folder, then cd .. and run it again as python projects/whereami.py. Which lines change, and which stay the same?

python
from pathlib import Path

# 1. the folder the terminal is in
# 2. the folder this script lives in
# 3. the .py files in the working directory
Hint 1 — The two folders Path.cwd() is where the terminal is. Path(__file__).resolve().parent is where the script is saved. They're the same only when you run the script from its own folder.
Hint 2 — Listing files Path.cwd().glob("*.py") finds the files; wrap it in sorted() and print each one's .name.
Show full solution
python
from pathlib import Path

here = Path.cwd()
print("Terminal is in: ", here)

# __file__ only exists when Python runs a file (not in an interactive prompt)
script_dir = Path(__file__).resolve().parent if "__file__" in globals() else here
print("Script lives in:", script_dir)

print("Python files here:")
for path in sorted(here.glob("*.py")):
    print("  ", path.name)

Run from projects, both folders are .../projects and you see your scripts. Run from the folder above as python projects/whereami.py, the first line changes (the terminal moved), the second doesn't (the file didn't), and the list shows the .py files of the folder you're in, not the folder the script is in.

That difference is the whole reason scripts that read files should build their paths from script_dir, not trust the working directory. The file I/O lesson uses exactly this pattern.


What You Learned

  • A terminal runs typed commands; the prompt shows it's waiting. Don't type the $ from guides.
  • pwd shows where you are, ls what's there, cd moves (.. up, ~ home), mkdir makes a folder. PowerShell accepts all four.
  • python hello.py runs a script from the folder you're in. On macOS and Ubuntu it's python3; in a virtual environment it's python everywhere.
  • Ctrl + C stops a running program; the KeyboardInterrupt traceback is expected.
  • "can't open file" means wrong folder; "not recognized" / "command not found" means a typo or PATH; a SyntaxError on a command means you're at the >>> prompt.
  • python -m pip install installs for the Python you actually run.
  • Up arrow, Tab and clear do most of the work.

Going deeper: when you want more than this (pipes, redirection, scripting the shell itself), MIT's free Missing Semester course starts with an excellent lecture on the shell. Next on this site: Virtual Environments, which is where the terminal starts paying for itself.

Found this useful? Share it with a friend who’s learning Python.