Sub topic 4
Activating and Deactivating on Linux, macOS, and Windows
This chapter assumes you can run commands in a terminal, you have Python 3 on your command line, and you have already created an environment with python -m venv .venv. It explains what activation does, how to run it on each platform, and how to work without it.
What activation does
Activation does not change Python. It changes your shell session so that the environment's tools are found first. A virtual environment keeps its executables in bin (Linux and macOS) or Scripts (Windows). Activation does three things:
- It puts that directory at the front of the
PATHvariable. The shell searchesPATHfrom left to right, sopythonandpipnow resolve to the environment's copies. - It sets
VIRTUAL_ENVto the environment's root directory. Some tools read this variable to find the active environment. - It prefixes your prompt with the environment name, such as
(.venv), unless prompt changes are disabled.
Activation also defines a deactivate function in the shell. That function undoes the changes above.
Because activation only edits the current shell, it has no effect on other terminal windows. It also ends when you close the terminal.
Activating on Linux and macOS
On Linux and macOS with bash or zsh, you run the activation script with source. The script must be sourced, not executed. A child process cannot change its parent shell's PATH, so ./activate would do nothing useful.
python3 -m venv .venv
source .venv/bin/activate
python -c "import sys; print(sys.prefix); print(sys.base_prefix)"
deactivateThe first printed path points inside .venv. The second points to the base installation that created it. When the two differ, you are running inside a virtual environment. This is the most reliable check from inside Python.
The bin directory contains one activation script per shell family:
| Shell | Command |
|---|---|
| bash, zsh | `source .venv/bin/activate` |
| fish | `source .venv/bin/activate.fish` |
| csh, tcsh | `source .venv/bin/activate.csh` |
| PowerShell Core | `.venv/bin/Activate.ps1` |
In bash and zsh, the command . .venv/bin/activate is equivalent, because . is another name for source. Use source in scripts for clarity.
Activating on Windows
Windows has two shells in common use, and each needs a different script from the Scripts directory. The py launcher is installed with Python on Windows and finds a Python 3 interpreter for you.
In PowerShell:
py -m venv .venv
.venv\Scripts\Activate.ps1
deactivateIn the classic Command Prompt (cmd.exe):
.venv\Scripts\activate.batCommand Prompt also uses deactivate, which activate.bat defines as a batch file in the same directory.
| Shell | Command |
|---|---|
| PowerShell | `.venv\Scripts\Activate.ps1` |
| Command Prompt | `.venv\Scripts\activate.bat` |
| Git Bash | `source .venv/Scripts/activate` |
Git Bash runs a bash-style shell on Windows, so it uses the bash script. Note that the directory is Scripts, not bin, even in Git Bash.
The PowerShell execution policy error
A new Windows machine often refuses to run Activate.ps1. PowerShell's execution policy blocks unsigned scripts, and the error says that running scripts is disabled on this system. You can allow scripts for your own user account only:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserConfirming which interpreter you are using
After activating, confirm that the shell resolves to the environment. A prompt prefix is a hint, not proof, because it can be disabled or can be left over from a copied shell state.
On Linux and macOS:
which python
python -c "import sys; print(sys.executable)"On Windows PowerShell:
Get-Command python
python -c "import sys; print(sys.executable)"Both should print a path inside your .venv directory. If which python shows /usr/bin/python3 or similar, activation did not take effect in this shell.
Use python -m pip rather than bare pip. This runs pip as a module of whichever python the shell resolved, so the packages go to the interpreter you just checked. A bare pip on PATH can belong to a different interpreter.
Deactivating
Run deactivate in the same shell session. It restores the previous PATH, removes VIRTUAL_ENV, and restores the prompt. The command is the same on every platform and shell.
source .venv/bin/activate
python -m pip --version
deactivate
python -m pip --versionThe two pip --version outputs differ. The first reports a path inside .venv. The second reports whichever Python the shell finds without the environment, and it may fail if that Python has no pip.
Deactivating does not delete anything. The environment and its installed packages stay on disk. You can activate it again at any time.
Switching between environments
You do not need to deactivate before activating another environment. The new activation script calls deactivate first when one is active, in the shells that the standard scripts support. Still, deactivating first is a cleaner habit, because it makes the sequence of changes to PATH obvious when you debug.
Using an environment without activating
Activation is a convenience. Every environment has its own interpreter, and you can call it by path. Pip run this way installs into that environment, whether or not any environment is active.
.venv/bin/python -m pip install requests
.venv/bin/python script.pyOn Windows the paths use Scripts:
.venv\Scripts\python.exe -m pip install requests
.venv\Scripts\python.exe script.pyThis is the better choice in several situations:
- Scripts and scheduled jobs, where no interactive shell has run an activation script.
- Continuous integration steps, where each command may start in a fresh shell, so activation would not carry over.
- Editor and tool configuration, where you give the tool the interpreter path directly.
- Cases where you are unsure of the shell state and want an unambiguous command.
A script's shebang line can also point at the environment's interpreter, such as #!/path/to/project/.venv/bin/python, so the script runs with the right packages when launched directly.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `source: .venv/bin/activate: No such file or directory` | Wrong directory, or the environment was never created | Run from the project root and check that `.venv` exists |
| `activate` does nothing after `./activate` or `bash activate` | The script ran in a child process | Use `source` |
| PowerShell says scripts are disabled | Execution policy | Set `RemoteSigned` for `CurrentUser` |
| `.venv\Scripts\activate` not found on Linux | Windows path used | Use `.venv/bin/activate` |
| Prompt shows `(.venv)` but `which python` shows the system Python | A shell command or startup file reset `PATH` after activation | Check your shell startup files and re-activate |
| New terminal tab is not activated | Activation applies to one shell session | Activate in each new session |
A moved or renamed environment folder is another frequent problem. The activation scripts and launchers record the absolute path where the environment was created. If you move the directory, VIRTUAL_ENV and the script shebangs point to the old location. Do not move environments. Delete and recreate them, which a later chapter covers.
Prompt and customization
To choose the prompt label at creation time, use --prompt:
python3 -m venv --prompt myproject .venv
source .venv/bin/activateThe prompt now shows (myproject) while the directory remains .venv. To stop activation from changing your prompt at all, set VIRTUAL_ENV_DISABLE_PROMPT to any non-empty value before activating. Some shell themes, such as those that show the environment in their own status line, use this setting.
Exercise
You activate an environment in one terminal tab, then open a second tab and run python -m pip install requests. Where does requests go, and how would you install it into the environment from the second tab without activating?
Show answer
In the second tab no environment is active, so python resolves to the system or user interpreter and requests installs there (or fails if that installation is protected). Activation only affects the shell session where the script was sourced. To install into the environment without activating, call its interpreter by path: .venv/bin/python -m pip install requests on Linux and macOS, or .venv\Scripts\python.exe -m pip install requests on Windows.
Exercise
Why does running bash .venv/bin/activate fail to activate the environment, even though the script runs without errors?
Show answer
bash starts a child process. The script sets PATH and VIRTUAL_ENV inside that child, and the child exits when the script ends, so the parent shell is unchanged. Sourcing the script with source .venv/bin/activate runs it in the current shell, so the changes persist.