Sub topic 4
Activating and deactivating on Linux, macOS, and Windows
This chapter assumes you can create an environment with python -m venv .venv and that you know what PATH is. Here you will activate that environment in each common shell, check that it worked, and leave it again.
What activation does
Activation changes your current shell session in three ways:
- It puts the environment's
bindirectory (Scriptson Windows) at the front ofPATH. A barepythonorpipthen resolves to the environment's copies. - It sets the
VIRTUAL_ENVvariable to the environment's path. - It adds the environment name to your prompt, such as
(.venv), and defines adeactivatecommand.
Activation is a convenience and nothing more. Running .venv/bin/python script.py uses the environment without activating it. Activation only saves you from typing the full path.
Linux and macOS
In bash or zsh, run the script with source. Running it without source starts a child process, and that process cannot change your shell.
source .venv/bin/activate
python -c "import sys; print(sys.prefix)"
deactivateOther shells have their own scripts in the same directory. Use source .venv/bin/activate.fish in fish and source .venv/bin/activate.csh in csh or tcsh.
Windows
PowerShell uses the .ps1 script:
.venv\Scripts\Activate.ps1
python -c "import sys; print(sys.prefix)"
deactivateThe classic Command Prompt (cmd.exe) uses the batch file. Run it without source:
.venv\Scripts\activate.bat
deactivateSummary of commands
| Shell | Activate command |
|---|---|
| bash, zsh | `source .venv/bin/activate` |
| fish | `source .venv/bin/activate.fish` |
| csh, tcsh | `source .venv/bin/activate.csh` |
| PowerShell | `.venv\Scripts\Activate.ps1` |
| cmd.exe | `.venv\Scripts\activate.bat` |
Every shell uses the same command to leave: deactivate.
Verifying that activation worked
Do not rely on the prompt alone. Check which interpreter will run:
which python
python -c "import sys; print(sys.prefix == sys.base_prefix)"On Windows PowerShell, use Get-Command python in place of which python. While the environment is active, which python should print a path inside .venv. The second command should print False, because sys.prefix points to the environment and sys.base_prefix points to the base installation. Outside an environment, both values are the same and it prints True.
Common errors
- Forgetting
sourceon Linux or macOS. The command seems to succeed, but the prompt does not change andpythonis still the system one. - Activating in one terminal and installing in another. Activation affects only the session where you ran it. A new terminal tab starts without it.
- Moving or renaming the environment folder. The activate scripts contain absolute paths. After a move, recreate the environment instead of repairing it.
- Using the wrong script for the shell. Running
activate.batin PowerShell orActivate.ps1incmd.exefails or does nothing useful.
Exercise
You run .venv/bin/activate on Linux and get "Permission denied", or nothing changes. What is the correct command, and why does the original fail to change your shell?
Show answer
Run source .venv/bin/activate. Executing the file directly runs it in a child process, and a child process cannot change the environment variables (PATH, VIRTUAL_ENV) of the parent shell. source runs the script inside your current shell, so the changes persist.
Exercise
Your prompt shows (.venv), but pip install puts packages in the system Python. Name one check that shows which interpreter is in use, and one way to avoid the problem.
Show answer
Run which python (or Get-Command python in PowerShell) and compare the path with the environment, or print sys.prefix from Python. To avoid the problem, always call python -m pip, which ties pip to the interpreter you named, or use the environment's interpreter by its full path.