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.
scratch/requirements.txtopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Where | scratch/.venv (Git ignores it) and scratch/requirements.txt |
|---|---|
| The package | netaddr 1.3.0, a library for IP addresses and MAC addresses |
| Python to make it from | python3, outside the lab’s venv |
new to this? step by stepbeginner
deactivate, thenpython3 -m venv scratch/.venv.source scratch/.venv/bin/activate: the prompt now starts with(.venv).pip install netaddr==1.3.0, thenpip freeze > scratch/requirements.txt.deactivate, andpython3 -c "import netaddr": it should fail.make check T=1.2, then open a new terminal to get back into the lab’s venv.
more in meet the lab
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.
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.
Make the venv
Leave the lab’s venv, so the new one is made from the codespace’s Python, not from another venv:
run in the terminaldeactivate python3 -m venv scratch/.venvIt takes a few seconds and prints nothing. Look in the Explorer:
scratch/.venv/now hasbin/,lib/andpyvenv.cfg. It’s greyed out because.gitignorelists it.Activate it, and check:
run in the terminalsource scratch/.venv/bin/activate which pythonThe prompt starts with
(.venv), andwhich pythonpoints intoscratch/.venv/bin/.See what’s in it so far:
run in the terminalpip listOnly
pipandsetuptools. 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.
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
python3outside it can’t importnetaddr.
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.2when 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