Creating, activating, and deactivating isolated Python environments with venv and virtualenv, and why isolation is essential for security tooling.
A virtual environment is a self-contained directory holding its own Python interpreter link and site-packages. Installing into it leaves the system interpreter untouched. For a pentester or analyst this isolation is not optional: it lets you pin exact tool versions per engagement, avoid dependency conflicts between projects, and keep the OS interpreter clean so the box stays stable. The built-in venv module covers almost all needs; virtualenv is a faster third-party alternative with extra features.
# Create, activate, deactivate (built-in venv)
python3 -m venv .venv
source .venv/bin/activate # bash/zsh; .venv\Scripts\activate on Windows
deactivateWhen a venv is active, python and pip resolve to the copies inside .venv/bin, and your shell prompt is prefixed with (.venv). Deactivating restores the previous interpreter.
| Command | Effect |
|---|---|
python3 -m venv .venv |
Create an environment in .venv/ |
source .venv/bin/activate |
Put the venv's bin first on PATH |
deactivate |
Leave the environment |
python -m venv --upgrade-deps .venv |
Create with up-to-date pip/setuptools |
rm -rf .venv |
Delete the environment (it is disposable) |
venv (standard library) vs virtualenv (PyPI):
venvships with Python 3, is enough for most projects.virtualenvis faster to create, supports older interpreters, and can seed newer pip. Install withpip install virtualenv, thenvirtualenv .venv.
# Isolation proof: package exists in venv, not in system interpreter
source .venv/bin/activate
pip install scapy
python -c "import scapy; print('in venv:', scapy.__version__)"
deactivate
python3 -c "import scapy" # ModuleNotFoundError — system stays cleanin venv: 2.5.0
Traceback (most recent call last):
...
ModuleNotFoundError: No module named 'scapy'
- Per-engagement environments — a
acme-2026/.venvwith pinned tools produces identical results on a retest months later. - Avoiding system pollution — installing
scapy,impacket,pwntoolsinto a venv keeps the OS interpreter (and Kali's own tooling) unbroken. - Reproducible payloads — freeze the venv to a
requirements.txtso a teammate or CI rebuilds the exact toolchain.
- One venv per project/engagement; name it
.venvand add it to.gitignore. - Recreate rather than repair a broken venv — they are disposable.
- Pin dependencies with
pip freeze > requirements.txt(see [[Pip-Package-Manager]]). - Activate the venv before installing anything; verify the prompt shows
(.venv).
- Forgetting to activate, then
pip install-ing into the system interpreter (PEP 668 now blocks this on Debian/Kali). - Committing the
.venv/directory to git — it is large, machine-specific, and unnecessary; commitrequirements.txtinstead. - Moving/renaming the project folder and expecting the venv to still work — the interpreter paths inside are absolute; recreate it.
Goal: prove environment isolation.
python3 -m venv .venv && source .venv/bin/activate.pip install requestsinside the venv.python -c "import requests; print(requests.__version__)"— succeeds.deactivate, thenpython3 -c "import requests"against the system interpreter and observe theModuleNotFoundError.pip freeze > requirements.txt, delete.venv, recreate it, and reinstall withpip install -r requirements.txt.
- [[Pip-Package-Manager]]
- [[Pyenv]]
- [[Project-Structure-Best-Practices]]
- [[Installing-Python-on-Linux]]
- [[Python-Environment-Setup/Readme|Python Environment Setup]] — module index
- [[Readme|Python for Security Professionals]] — course home