Skip to content
Visual explainer 2h to learn 4 steps

Installing Python, the REPL & an editor

Run Python from the terminal, the REPL and VS Code.

By Invicly Tech, , in Python Programming from Zero. About 13 minutes to read.

Topic 1 of 34

In short

Python is a program that reads your code and runs it. You can type code into the interactive REPL to try ideas instantly, or save it in a .py file and run the whole script from the terminal or VS Code.

Think of it like this: The REPL is a calculator you chat with; a script is a recipe you hand over to cook start to finish.

Explained simply

Python is two things at once. It is a language, which means a set of rules for writing code. It is also a program, called the interpreter, that reads your code and does what it says. When people say "install Python", they mean installing that interpreter, plus the standard library (thousands of ready-made tools) and pip, the tool that downloads extra packages.

You talk to the interpreter through a terminal: a text window where you type a command, press Enter and get text back. On Windows that is PowerShell or Windows Terminal. On macOS it is the Terminal app. On Linux it is whatever terminal your desktop offers. It looks old-fashioned, but it is the fastest way to run and control programs.

There are two ways to run Python code. The REPL is an interactive prompt (it shows >>>) where every line runs the moment you press Enter. It is perfect for quick experiments. A script is a .py file that you save and run from start to finish. Real programs are always scripts.

An editor like VS Code makes writing scripts much easier. It colors your code, points out mistakes while you type, and runs the file with one click. Under the hood, the Run button simply starts the interpreter with your file, exactly as you would in the terminal. Knowing both ways means you are never stuck when one of them misbehaves.

Press Play on a diagram to walk through it one step at a time.

FlowHow Python runs a script
  1. 1
    Write hello.py

    Plain text file saved in your editor

  2. 2
    python hello.py

    Terminal starts the interpreter with your file

  3. 3
    Parse & compile

    Code is checked for syntax and turned into bytecode

  4. 4
    Interpreter executes

    Bytecode runs top to bottom, one statement at a time

  5. 5
    Output or error

    print() shows text; a bug shows a traceback

CompareREPL vs script
REPL (>>>)
  • Start with python or py
  • Runs each line immediately
  • Echoes expression results
  • Lost when you exit
Script (.py file)
  • Run with python file.py
  • Runs the whole file in order
  • Only print() shows output
  • Saved and reusable
MapWays to run Python code
Python code
  • REPL
    • python3 or py
    • python3 -i app.py
  • Script file
    • python3 app.py
    • ./app.py with a shebang line
  • One-liner
    • python3 -c "print(42)"
  • Module
    • python3 -m pip
    • python3 -m http.server
  • Editor
    • VS Code Run button
    • F5 for the debugger
CompareThe same tasks on each system
Windows
  • Install from python.org
  • Command: py or python
  • Check: py --version
  • Exit REPL: Ctrl+Z, Enter
macOS
  • python.org or brew install python
  • Command: python3
  • Check: python3 --version
  • Exit REPL: Ctrl+D
Linux
  • Usually preinstalled; apt or dnf
  • Command: python3
  • Check: python3 --version
  • Exit REPL: Ctrl+D

In depth

What an install really puts on your computer

The python.org installer copies four main things: the interpreter (python.exe on Windows, python3 elsewhere), the standard library (folders of modules such as json, math and pathlib), pip, and IDLE, a very simple editor. On Windows it also installs the py launcher, a small tool that finds and starts the right Python for you.

On macOS you can use the python.org installer or Homebrew (brew install python). Most Linux systems already have python3, because the operating system's own tools use it. Do not remove or replace that system Python. On Debian and Ubuntu, venv and pip come as separate packages: sudo apt install python3-venv python3-pip.

A new 3.x version comes out every October and each one gets fixes for about five years. Install the newest stable release unless a course or tool asks for another. Python 2 reached its end of life in 2020; never start a project with it.

  • Windows: tick "Add python.exe to PATH" in the installer
  • macOS: brew install python, or the python.org installer
  • Ubuntu/Debian: sudo apt install python3 python3-venv python3-pip

Which Python runs? PATH and the three command names

When you type a command like python3, the shell does not search your whole disk. It reads an environment variable called PATH, which is a list of folders, and checks them in order. The first folder that contains a matching program wins. So if two Pythons are installed, the one whose folder comes first in PATH is the one you get.

The command name differs by system. On macOS and Linux use python3; plain python may be missing or point to something else. On Windows use py (the launcher) or python. The launcher can pick a version for you: py -3.12 runs 3.12, and py -0 lists every version it found.

When in doubt, ask Python itself: python3 -c "import sys; print(sys.executable)" prints the full path of the interpreter that is actually running. This one line solves most "I installed it but it does not work" problems.

FlowHow the shell finds python3
  1. 1
    You type python3

    The shell needs to find a program with that name

  2. 2
    Read PATH

    A list of folders, for example /usr/local/bin then /usr/bin

  3. 3
    Check folders in order

    Look for an executable called python3 in each one

  4. 4
    First match runs

    Later folders are never checked

  5. 5
    Confirm it

    sys.executable shows the path that really ran

Getting more out of the REPL

The REPL (Read, Eval, Print, Loop) reads one line, runs it, prints the result and waits again. It only prints results for expressions, like 2 + 3. In a script you need print() to see anything.

A few tricks make it a real learning tool. The underscore _ holds the last result, so you can keep calculating with it. help(str) shows the documentation for any object, and dir("text") lists everything a value can do. When a line ends with a colon, such as the first line of a loop, the prompt changes to ... and you finish the block with an empty line.

Since Python 3.13 the REPL is friendlier: it uses colors, lets you edit a whole block with the arrow keys, accepts exit and quit without parentheses, and has F1 for help, F2 for history and F3 for paste mode. python3 -i app.py runs a script and then opens a REPL with all of its variables, which is great for poking at results.

  • _ is the last result (only in the REPL)
  • help(x) explains x; dir(x) lists its attributes
  • Exit: exit(), Ctrl+D on macOS/Linux, Ctrl+Z then Enter on Windows

Running scripts: folders, flags and exit codes

python3 hello.py looks for hello.py in the current working directory, the folder your terminal is in right now. Run pwd (macOS, Linux, PowerShell) to see it and cd to move. Most "No such file or directory" errors simply mean you are in the wrong folder.

The interpreter has useful flags. -c runs a short piece of code: python3 -c "print(2 + 2)". -m runs a module as a program, for example python3 -m pip, python3 -m venv, or python3 -m http.server 8000, which serves the current folder on port 8000. -i opens the REPL after the script ends.

Every program ends with an exit code. 0 means success; an uncaught error gives 1. Shells and CI systems use this number to decide if a step passed. Check it with echo $? in bash or $LASTEXITCODE in PowerShell. On macOS and Linux a first line of #!/usr/bin/env python3 plus chmod +x lets you run the file as ./hello.py.

A VS Code setup that actually works

Install VS Code, then the Python extension published by Microsoft. It also brings Pylance (smart completions and error hints) and the Python Debugger. Open your project with File, Open Folder rather than opening single files, so VS Code can find your settings and virtual environment.

The most important setting is the interpreter. Press Ctrl+Shift+P (Cmd+Shift+P on macOS), choose "Python: Select Interpreter" and pick the Python you want. The chosen version shows in the status bar at the bottom. If the Run button and your terminal behave differently, they are almost always using two different interpreters.

The Run button (the triangle at the top right) runs the current file in the built-in terminal. Shift+Enter sends selected lines to a Python REPL. F5 starts the debugger, which lets you pause on a line and look at every variable. That habit will save you hours later.

SequenceWhat the VS Code Run button does
You
VS Code
Terminal
Python
  1. 1Click Run Python File
    You to VS Code: Click Run Python File
  2. 2Open the built-in terminal
    VS Code to Terminal: Open the built-in terminal
  3. 3Start the selected interpreter with the file path
    Terminal to Python: Start the selected interpreter with the file path
  4. 4Print output or a traceback
    Python to Terminal: Print output or a traceback
  5. 5You read the result
    Terminal to You: You read the result

Learn it in 4 short steps

About 30m each. Track them with a timer in your plan.

  1. 1
    Install Python 3 and check the version~30m
  2. 2
    Start the REPL and try expressions~30m
  3. 3
    Set up VS Code with the Python extension~30m
  4. 4
    Run a script from the terminal~30m

Key points

  • Install Python 3 and confirm it with python --version (or py --version on Windows).
  • Use the REPL to test small expressions; exit with exit() or Ctrl+D.
  • Save real programs in .py files and run them from the terminal.
  • VS Code with the Python extension gives highlighting, linting and a Run button.

Key terms

Tap a card to flip it. 0/5 known

Examples

python
# hello.py
name = "Ada"
print("Hello,", name)
print(2 + 3 * 4)

# In the terminal:
# $ python hello.py
# Hello, Ada
# 14

Save as hello.py, then run it from the folder that contains it.

First check on a fresh machine

You just installed Python and want to be sure it works. Step 1: ask for the version. Step 2: ask which file is actually running, so you know which install you got. Step 3: open the REPL and do a quick calculation, using _ to reuse the last result. Step 4: leave the REPL. If step 1 fails with "command not found", the install folder is not on PATH; re-run the installer (on Windows, tick Add python.exe to PATH) or open a new terminal.

bash
python3 --version          # Windows: py --version
# Python 3.x.y
python3 -c "import sys; print(sys.executable)"
# /usr/bin/python3
python3
>>> 60 * 60 * 24
86400
>>> _ * 7
604800
>>> exit()

Your first script and the wrong-folder error

You saved hello.py in a folder called projects, but your terminal is still in your home folder. Step 1: running it fails because Python looks in the current folder. Step 2: move into the right folder with cd. Step 3: run it again. Step 4: check the exit code, 0 means it finished without errors. Reading the error message closely, it tells you exactly which path it tried.

bash
python3 hello.py
# python3: can't open file '/home/ana/hello.py': [Errno 2] No such file or directory
pwd                     # /home/ana
cd projects
python3 hello.py
# Hello, Ada
echo $?                 # 0 (PowerShell: $LASTEXITCODE)

Quick reference

  • python3 --version (macOS/Linux) or py --version (Windows) shows the installed version
  • py -0 lists every Python on Windows; py -3.12 runs that exact version
  • python3 -c "import sys; print(sys.executable)" shows which interpreter really runs
  • In the REPL, _ is the last result; help(x) and dir(x) explain any object
  • Exit the REPL with exit(), Ctrl+D (macOS/Linux) or Ctrl+Z then Enter (Windows)
  • python3 -i app.py runs the script, then opens a REPL with its variables
  • python3 -m http.server 8000 serves the current folder at localhost:8000
  • VS Code: Ctrl+Shift+P, then "Python: Select Interpreter" picks which Python runs

Common mistakes

  • Running python from a different folder than the file, causing 'No such file' errors.
  • Typing terminal commands like python hello.py inside the REPL instead of the shell.
  • Having several Python versions and installing packages into the wrong one.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1You run python3 hello.py and get "[Errno 2] No such file or directory". What is the most likely cause?

Question 2

2In the REPL you type 6 * 7, then _ + 1. What is shown?

Question 3

3Two Pythons are installed and both folders are on PATH. Which one does python3 start?

Question 4

4What does python3 -m http.server 8000 do?

Question 5

5The VS Code Run button and your terminal show different Python versions. What should you check first?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Use the REPL as a calculator: work out how many seconds are in a week, then use _ to turn that into minutes. Leave the REPL cleanly.

  2. Exercise 2medium

    Write info.py that prints the Python version and the full path of the interpreter running it. Run it from the terminal and from the VS Code Run button, and compare the two outputs.

  3. Exercise 3hard

    On macOS or Linux, make a script you can run as ./hello.py. It should print which interpreter runs it and finish with exit code 3. Show the exit code in the shell.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Should I type python or python3?
On macOS and Linux use python3, because python may be missing or point somewhere else. On Windows use py or python. Inside an active virtual environment, python always means that environment's Python.
Which Python version should I install?
The newest stable 3.x release from python.org, unless a course or tool you need asks for a specific one. Python 2 reached its end of life in 2020, so never start with it.
Why does typing python on Windows open the Microsoft Store?
Windows has an app execution alias called python that opens the Store when no Python is installed. Install Python, or turn the alias off in Settings, Apps, Advanced app settings, App execution aliases.
Do I need an IDE like PyCharm to learn Python?
No. Any editor plus a terminal is enough. VS Code with the Python extension is free and popular, and PyCharm is a good alternative; the skills are the same in both.

Go deeper

ShareXLinkedInWhatsApp