A sidequest, for Neil
Python at Home
Set Python up on your own computer: a terminal, an editor, a virtual environment and pytest, proved by a small program and its tests.
No account needed. Your work is kept in this browser until you sign up.
The brief
- NeilEverything so far has run here in the Workshop, but real programs run on real computers.
- NeilYours, for a start. If you set it up properly once, it stays set up.
- NeilMy first laptop had Python installed four times, and three of them are still in there somewhere.
- PaulMine had five, and we don't talk about that.
Set Python up on your own computer, and prove it with a small program and its tests:
- Python 3.10 or newer, run from a terminal
- an editor (VS Code is a good one, and free)
- a virtual environment for the project, with pytest installed in it
hello.py, with agreeting(name)function that the tests check
$ python hello.py Paul Morning, Paul.
What you learn
The lesson, step by step
The terminal
A terminal is a window where you type a command, press Enter, and read what comes back. On your own computer it does the job of the Workshop's Run button.
A terminal is always in a folder, and commands look for files there.
cdmoves into a folder,cd ..moves up one, andlslists what's here (diron Windows).python hello.pyruns the filehello.pywith Python.$ cd at-home $ ls README.md hello.py pytest.ini tests $ python hello.py Paul Morning, Paul.Anything after the file name, like
Paul, reaches the program in a list calledsys.argv.The usual slip is running a file from the wrong folder. If the terminal can't find
hello.py,cdinto the folder it's in first.A virtual environment
A virtual environment is a folder inside a project, usually called
.venv, holding that project's own copy of Python and its packages. A package is code someone else wrote that you install, like pytest.With one per project, two projects can need different versions of the same package without breaking each other.
You make it once per project, then switch it on in every new terminal. While it's on, the prompt starts with
(.venv), andpip installputs packages into it alone.$ python3 -m venv .venv $ source .venv/bin/activate (.venv) $ pip install pytestOn Windows, the second line is
.venv\Scripts\Activate.ps1.The usual slip is opening a new terminal and forgetting to switch it on again. Look for
(.venv)before you start.One environment per project
Before you install anything, say it in your own words. It's the reason for the oddest-looking step in the setup.
Tests that check for you
A test is a small function that calls your code and checks the answer.
assertsays what must be true. If it isn't, the program stops with an AssertionError.Tests check your work the same way every time, so you don't have to. The checks in the Workshop are tests like these.
pytest is a package that finds every function whose name starts with
test_, runs them all, and reports which passed. There's no pytest here, so the example calls its test by hand and printsPassed.ChangeMorningtoHelloon line 2 and run it: theassertfails.The usual slip is changing a test to fit a wrong answer. When a test fails, fix the code, not the test.
What goes where
- NeilThere are four things to do in order, like a pre-flight check, and people get stuck by doing them out of order.
Here's the order you'll set things up in on your own computer. The next step has the full guide for Windows, macOS and Linux.
- Python, version 3.10 or newer: the program that runs your programs.
- An editor, where you write the code. VS Code is free, and it shows Python's mistakes as you type.
- A terminal, open in the project's folder, with the virtual environment made and switched on.
- pytest, installed into that environment, to run the project's tests.
$ python3 -m venv .venv $ source .venv/bin/activate (.venv) $ pip install -r requirements.txt (.venv) $ pytestrequirements.txtlists the packages a project needs, sopip install -r requirements.txtinstalls them all in one go.Python on your own computer
- NeilDownload it, set it up and make the tests pass, then hand the results in from your own terminal.
The project has
hello.py, whosegreetingfunction you finish, and six tests. Three check your function. Three check your computer: that Python is recent enough, that the virtual environment is switched on, and thathello.pyruns from the terminal.Open the project folder in VS Code (File, then Open Folder) and write
greetingthere. Runpytestafter each change. When all six pass, hand it in.If a setup test fails, its message says what to do. The usual one is forgetting to switch the virtual environment on in a new terminal.
Ship it
- NeilIt's the same tests, just on your own computer, and that's really all there is to it.
- PaulOne Python, and one environment per project. Neil, go and check yours.
- NeilJust the one Python. I checked twice.
After this, Act 2
This is the last project in Act 1. Act 2 and everything after it need a free account, and whatever you've done in this browser comes with you when you make one.