online posts 16
python 101 for network engineers

task 1.2 · chapter 1 of 8: running python · 25 min

A venv of your own

objective

Build a venv in scratch/, install one pinned package into it, write its requirements.txt, and prove that the Python outside the venv doesn’t have the package.

you edit, in the labscratch/requirements.txt

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
Wherescratch/.venv (Git ignores it) and scratch/requirements.txt
The packagenetaddr 1.3.0, a library for IP addresses and MAC addresses
Python to make it frompython3, outside the lab’s venv
new to this? step by stepbeginner
  1. deactivate, then python3 -m venv scratch/.venv.
  2. source scratch/.venv/bin/activate: the prompt now starts with (.venv).
  3. pip install netaddr==1.3.0, then pip freeze > scratch/requirements.txt.
  4. deactivate, and python3 -c "import netaddr": it should fail.
  5. make check T=1.2, then open a new terminal to get back into the lab’s venv.

more in meet the lab

1 explain

A folder that acts as a Python

What a venv is

A venv (virtual environment) is a folder with a Python of its own in it: bin/python, bin/pip, and its own site-packages folder where installed packages go. It borrows the interpreter from the Python that made it, and nothing else. Whatever you install into a venv stays in that venv.

One project, one venv. That way two projects can need different versions of the same package and never meet.

in one sentence python3 -m venv FOLDER makes a venv; what you pip install inside it stays inside it.

Activating

source FOLDER/bin/activate puts the venv’s bin first on PATH, so python and pip mean the venv’s. The prompt shows its name in parentheses. deactivate undoes it. You can skip activating altogether and call FOLDER/bin/python by its path: it’s the same thing.

Pinning

requirements.txt lists a project’s packages, one per line. name==version pins one exact version, so the venv can be rebuilt identically later, by anyone, with pip install -r requirements.txt. pip freeze prints everything installed in exactly that format.

in one sentence A pinned requirements.txt is how a venv becomes repeatable.

2 show

Build, install, freeze

$ python3 -m venv scratch/.venv
$ source scratch/.venv/bin/activate
$ which python
/workspaces/python-101/scratch/.venv/bin/python
$ pip install netaddr==1.3.0
Collecting netaddr==1.3.0
...
Successfully installed netaddr-1.3.0
$ pip freeze > scratch/requirements.txt
$ cat scratch/requirements.txt
netaddr==1.3.0

And outside it:

$ deactivate
$ python3 -c "import netaddr"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'netaddr'

That error is the result you want here.

3 follow along

Make the venv

  1. Leave the lab’s venv, so the new one is made from the codespace’s Python, not from another venv:

    run in the terminal
    deactivate
    python3 -m venv scratch/.venv
    

    It takes a few seconds and prints nothing. Look in the Explorer: scratch/.venv/ now has bin/, lib/ and pyvenv.cfg. It’s greyed out because .gitignore lists it.

  2. Activate it, and check:

    run in the terminal
    source scratch/.venv/bin/activate
    which python
    

    The prompt starts with (.venv), and which python points into scratch/.venv/bin/.

  3. See what’s in it so far:

    run in the terminal
    pip list
    

    Only pip and setuptools. None of the lab’s packages: this venv is new and empty.

in one sentence A venv starts empty, whatever the Python that made it has.

4 challenge

A package only it has

With scratch/.venv active:

  • install netaddr, pinned to version 1.3.0;
  • write scratch/requirements.txt, with every package in this venv pinned;
  • then leave the venv and show that python3 outside it can’t import netaddr.
hint

A pin goes in the install command too: pip install name==version.

hint

pip freeze prints the pinned list; > (from Linux CLI 101’s lesson on redirection) sends it into a file instead of the screen.

hint

If the last step doesn’t fail, netaddr was installed outside the venv: you ran pip after deactivate. python3 -m pip uninstall netaddr removes it from there.

show the solutionscratch/requirements.txt

or pull it into your lab: make solve T=1.2, then git diff --staged shows what changed

pip install netaddr==1.3.0
pip freeze > scratch/requirements.txt
deactivate
python3 -c "import netaddr"         # ModuleNotFoundError: the point
netaddr==1.3.0

The check does what you did by hand: it asks pip inside scratch/.venv for each pinned package and its version, then asks the Python outside the venv for the same package, and expects it not to be there. Open a new terminal when you’re done, to get back into the lab’s venv.

5 check: run it, then prove it

$ make check T=1.2

when it's right, you'll see

task 1.2  checks/test_1_2.py
checks/test_1_2.py::test_venv PASSED                                     [ 33%]
checks/test_1_2.py::test_requirements_pinned PASSED                      [ 66%]
checks/test_1_2.py::test_only_in_the_venv PASSED                         [100%]
============================== 3 passed in 0.45s ===============================
✓ task 1.2 done