Virtual environments
Each project needs its own versions of its packages. A virtual environment is a folder with its own site-packages, sharing the interpreter and standard library of a base installation. python -m venv .venv creates one, and activating it puts .venv/bin first on your PATH.
import sys
import sysconfig
print(sys.prefix)
print(sys.base_prefix)
print("in a venv:",
sys.prefix != sys.base_prefix)
print(sysconfig.get_path("purelib"))How Python knows
- At startup Python looks for a
pyvenv.cfgfile next to its executable or one folder up. If it finds one,sys.prefixbecomes the environment's folder, whilesys.base_prefixkeeps pointing at the base installation. pip installwrites into the active environment'ssite-packages, thepurelibpath above.- An environment is disposable. Delete the folder and recreate it from your dependency list rather than copying it.
"I installed it but cannot import it" usually means two Pythons are involved. Compare sys.executable with the Python your pip belongs to.
Your task
Write venv_info(folder) returning None when folder has no pyvenv.cfg file, and otherwise a dict with the file's "home" and "version" values and "system_site_packages", a bool that is True only when include-system-site-packages is true, ignoring letter case. Lines look like key = value.
The explanation opens here once every check passes.