Sidebar
Programming
Python Environments Cheat Sheet
Sidebar
Programming · Python Environments Cheat Sheet
Creating, activating, and safely tearing down a Python virtual environment — using the built-in venv module, no extra tools required.
Why Bother With One at All?
IsolationEach project gets its own private set of installed packagesNo version clashesProject A can use Django 4 while Project B uses Django 5A clean system PythonNothing you install for a project can break other tools on your machineReproducibilityA pinned package list lets anyone rebuild the exact same setupCreating One
python -m venv myenvCreates a folder named myenv holding its own interpreterpython3 -m venv myenvUse this instead on macOS/Linux if python isn't aliasedWhere to put itInside the project folder itself — commonly named venv or .venvActivating
myenv\Scripts\activate.batWindows, Command Promptmyenv\Scripts\Activate.ps1Windows, PowerShell (see Troubleshooting if this fails)source myenv/bin/activatemacOS / Linux, bash or zsh(myenv)The prompt prefix that confirms activation actually workedDeactivating
deactivateThe exact same command on every platform — no arguments neededwhere python / which pythonConfirms which interpreter is actually active right nowInstalling & Freezing Packages
pip install requestsInstalls only into the currently active environmentpip freeze > requirements.txtSnapshots exact installed versionspip install -r requirements.txtRecreates that exact same package set elsewherepython -m pip install --upgrade pipA new venv often ships with an outdated bundled pipSafely Deleting a Venv
deactivateStep 1 — leave it before deleting itrmdir /s /q myenvWindows, Command PromptRemove-Item -Recurse -Force myenvWindows, PowerShellrm -rf myenvmacOS / LinuxIt's just a folderDeleting it never touches your actual project source filesTroubleshooting
"running scripts is disabled"PowerShell only — see the fix belowPackages installing "globally"You forgot to activate — check for the (myenv) prefixWrong Python version inside itDelete it and recreate with the correct interpreter, e.g. py -3.11 -m venv myenvOld/broken pip errorspython -m pip install --upgrade pip while activatedGood Habits
.gitignoreAdd your venv folder name — never commit it to version controlrequirements.txtCommit this instead, so the environment itself is always reproducibleOne venv per projectDon't reuse a single environment across unrelated projectsCreated it in the wrong folder, or with the wrong name? Delete it — don't try to move or rename it
A venv's own activation scripts and its pyvenv.cfg file have the absolute path to where it was created baked directly into them. Move the folder or rename it and those paths silently stop pointing anywhere real — activation can break in ways that are confusing to debug. Python's own official documentation is explicit about this: if you need a venv somewhere else, delete the misplaced one and run python -m venv again in the right location under the right name. It only takes a few seconds to recreate, and deleting it is completely safe — a venv holds nothing but installed packages and a copy of the interpreter, never your actual project code.
Fixing the PowerShell "running scripts is disabled" error
Run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser once, in PowerShell. This only relaxes the policy for locally-created scripts under your own user account — it doesn't touch the whole system, and it isn't Python-specific. Using Command Prompt instead of PowerShell sidesteps the issue entirely, since cmd.exe doesn't block script execution this way.