Paul's Python CoursePPC

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.

About 45 minutes, in 8 steps, free and with no account needed.

Start the project

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 a greeting(name) function that the tests check
What it prints
$ python hello.py Paul
Morning, Paul.

What you learn

The lesson, step by step

  1. 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. cd moves into a folder, cd .. moves up one, and ls lists what's here (dir on Windows). python hello.py runs the file hello.py with 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 called sys.argv.

    The usual slip is running a file from the wrong folder. If the terminal can't find hello.py, cd into the folder it's in first.

  2. 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), and pip install puts packages into it alone.

    $ python3 -m venv .venv
    $ source .venv/bin/activate
    (.venv) $ pip install pytest
    

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

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

  4. Tests that check for you

    A test is a small function that calls your code and checks the answer. assert says 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 prints Passed. Change Morning to Hello on line 2 and run it: the assert fails.

    The usual slip is changing a test to fit a wrong answer. When a test fails, fix the code, not the test.

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

    1. Python, version 3.10 or newer: the program that runs your programs.
    2. An editor, where you write the code. VS Code is free, and it shows Python's mistakes as you type.
    3. A terminal, open in the project's folder, with the virtual environment made and switched on.
    4. 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) $ pytest
    

    requirements.txt lists the packages a project needs, so pip install -r requirements.txt installs them all in one go.

  6. 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, whose greeting function 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 that hello.py runs from the terminal.

    Open the project folder in VS Code (File, then Open Folder) and write greeting there. Run pytest after 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.

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