Python Virtual Environment: Activation & Deactivation

This guide provides quick commands to activate and deactivate Python virtual environments across different platforms.

🖥️ Windows (CMD)

python -m venv venv
venv\Scripts\activate.bat
REM To deactivate:
deactivate
    

🖥️ Windows (PowerShell)

python -m venv venv
.\venv\Scripts\Activate.ps1
# To deactivate:
deactivate
    

🐧 Linux / macOS

python3 -m venv venv
source venv/bin/activate
# To deactivate:
deactivate
    

🗑️ Deleting a Virtual Environment

Removing a virtual environment is straightforward—just delete its directory. Here's how to do it on each OS:

Deactivate first, close programs using the environment, and confirm that venv contains only the disposable environment, not your project files.

Windows (PowerShell)

From the environment's parent directory, confirm the resolved path and preview deletion. Only then run the final command:

Resolve-Path -LiteralPath .\venv
Remove-Item -LiteralPath .\venv -Recurse -WhatIf
Remove-Item -LiteralPath .\venv -Recurse

Or manually delete the folder using File Explorer.

macOS / Linux

rm -rf venv

Make sure you're in the parent directory of the virtual environment before running this command.

⚠️ Note: This action is irreversible. Ensure you no longer need the environment before deleting it.

✅ Tip: Activation makes the environment's Python available by name, but is optional. If PowerShell blocks activation, use the environment's interpreter directly instead of changing security policy.

📦 After activation, install packages with python -m pip install <package-name>. Without activation, use .\venv\Scripts\python.exe -m pip install <package-name> on Windows or ./venv/bin/python -m pip install <package-name> on macOS/Linux.

Reference: Python's official venv documentation.