This is the one lesson with nothing to run on the page — the commands below belong in a terminal on your own computer. Everything else in the course works without it, but sooner or later you will want a Python that can reach the network, read your real files, and install other people’s libraries.
Getting Python
Check whether you already have it. macOS and most Linux distributions ship a Python, though sometimes an old one:
python3 --version
If that prints Python 3.11 or newer, you are set. Otherwise:
- Windows — install from the Microsoft Store, or python.org. Tick “Add Python to PATH” in the installer.
- macOS —
brew install pythonif you use Homebrew, otherwise the python.org installer. - Linux —
sudo apt install python3 python3-venv python3-pipon Debian/Ubuntu.
python or python3?
On Windows the command is usually python. On macOS and Linux, python3 is
the safe one — plain python may be missing or point at an ancient Python 2.
Running a file
Put your code in a file ending in .py and run it:
python3 hello.py
Running python3 with no file gives you the REPL — an interactive prompt
where each line runs as you press Enter. It is the closest thing to the
playgrounds on this site, and it is where experienced Python programmers test
small ideas. exit() or Ctrl-D leaves it.
Virtual environments
A virtual environment is a private folder of packages belonging to one project.
Without one, every pip install goes into your system Python, and two projects
that need different versions of the same library cannot both work.
Make one per project, in the project folder:
python3 -m venv .venv
Then activate it:
source .venv/bin/activate
On Windows PowerShell, that line is .venv\Scripts\Activate.ps1 instead. Your
prompt gains a (.venv) prefix, and python now means this project’s
Python. deactivate leaves it.
Do not commit .venv
The folder is large, machine-specific, and rebuildable in seconds. Add .venv/
to your .gitignore and record your dependencies instead.
Installing packages
With the environment active, pip installs from the Python Package Index:
pip install requests
Record what a project needs so someone else — or you, on another machine — can reproduce it:
pip freeze > requirements.txt
And install from that record:
pip install -r requirements.txt
Three packages worth knowing about early:
| Package | What it does |
|---|---|
requests | HTTP the pleasant way |
pytest | The testing tool almost everyone uses (Day 7) |
ruff | Fast linter and formatter — catches mistakes before you run |
An editor
Any text editor works, but a real Python editor will show you errors as you type. VS Code with the Python extension is the common choice and free; PyCharm is the heavyweight alternative.
Point your editor at the interpreter inside .venv so it sees the same
packages your program will.
A sensible project layout
myproject/
├── .venv/ # virtual environment, not committed
├── .gitignore
├── requirements.txt
├── README.md
├── src/
│ └── myproject/
│ ├── __init__.py
│ └── main.py
└── tests/
└── test_main.py
You do not need all of this for a fifty-line script — but when a script grows into a program, this is the shape it grows into. Day 7 builds a small version.
What you learned
python3 --versionchecks what you have;python3 file.pyruns a program.python3alone opens the REPL for quick experiments.python3 -m venv .venvplussource .venv/bin/activateisolates a project.pip installadds packages;pip freeze > requirements.txtrecords them.- Keep
.venv/out of version control.