How do you fix virtualenv activation errors on Windows and Linux? | A1Lab@MZeeshanM to PythonEnglish • 2 days ago

How do you fix virtualenv activation errors on Windows and Linux?

message-square
0

Resolve virtual environment activation errors on Windows PowerShell (running scripts is disabled on this system / UnauthorizedAccess) by permitting local scripts for your user via Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser (or using cmd.exe’s activate.bat), on Linux/macOS (No such file or directory, Permission denied, or activate: command not found) by sourcing the script with . or source .venv/bin/activate instead of executing ./.venv/bin/activate directly, and whenever a .venv was copied, moved, or created on another OS—or completely skip shell activation by invoking .venv/bin/python (Linux/macOS) or .venv\Scripts\python.exe (Windows) directly.

Basic Example

# --- WINDOWS (PowerShell) ---
# 1. Recommended fix for `PSSecurityException: running scripts is disabled on this system`:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

# 2. Activate the virtual environment in PowerShell:
.\.venv\Scripts\Activate.ps1

# (Alternative in Windows Command Prompt `cmd.exe` — no ExecutionPolicy needed):
# .\.venv\Scripts\activate.bat
# --- LINUX / macOS (Bash / Zsh) ---
# 1. Recommended: Always `source` (or `.`) the activate script in the CURRENT shell
source .venv/bin/activate

# 2. If the `.venv` folder was moved, copied, or missing `pip`/symlinks, recreate it cleanly:
rm -rf .venv && python3 -m venv .venv && source .venv/bin/activate

Key Details & Edge Cases

  • Windows PowerShell ExecutionPolicy scope (CurrentUser vs. LocalMachine vs. Process):
    • Do not run Set-ExecutionPolicy Unrestricted globally as Administrator when Set-ExecutionPolicy RemoteSigned -Scope CurrentUser works without elevation and safely allows locally created .ps1 scripts while still requiring signatures on scripts downloaded from the internet. If corporate Group Policy locks CurrentUser, bypass it for a single terminal session via Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process.
  • Linux/macOS Permission denied or bad interpreter when running ./.venv/bin/activate:
    • .venv/bin/activate is intentionally not marked executable (chmod +x), because executing it in a child subshell updates $PATH in that subshell and discards it the moment the subshell exits! Always run source .venv/bin/activate (or POSIX . .venv/bin/activate) so environment variables modify your active shell session.
  • Wrong activation script for your shell (Git Bash on Windows, fish, csh):
    • Windows virtual environments generate three distinct scripts inside .venv\Scripts\:
      • PowerShell: .\.venv\Scripts\Activate.ps1
      • Command Prompt (cmd.exe): .\.venv\Scripts\activate.bat
      • Git Bash / MSYS2 / Cygwin on Windows: source .venv/Scripts/activate (POSIX forward slashes + source, not bin/activate).
    • On Linux/macOS fish shell, use source .venv/bin/activate.fish.
  • Broken activation after moving, renaming, or sharing a .venv across machines/OSes:
    • Virtual environments hardcode the absolute VIRTUAL_ENV path and symlink/copy the host OS’s Python binary inside activate and pyvenv.cfg. Never commit .venv/ to Git or copy a Windows .venv to Linux/WSL—delete and recreate it (python3 -m venv .venv) from requirements.txt or pyproject.toml.
  • Missing ensurepip on Debian/Ubuntu (The virtual environment was not created successfully):
    • Minimal Ubuntu/Debian installations strip venv out of the base Python package, producing a half-built .venv with no bin/activate file. Install the OS package (sudo apt update && sudo apt install -y python3-venv), delete the incomplete .venv, and re-run python3 -m venv .venv.
  • Bypass shell activation entirely (zero activation errors in CI/CD, cron, IDEs, and scripts):
    • Activation only prepends .venv/bin (or .venv\Scripts) to PATH. Calling .venv/bin/python -m pip install ... or .venv\Scripts\python.exe script.py uses the virtual environment’s interpreter and packages directly with zero shell-policy or source issues.

Comparison of Virtualenv Activation Errors & Fixes Across Platforms

Platform / Shell Typical Error Message Root Cause Exact Fix
Windows PowerShell Activate.ps1 cannot be loaded because running scripts is disabled on this system Default Restricted PowerShell ExecutionPolicy blocks .ps1 files Run Set-ExecutionPolicy RemoteSigned -Scope CurrentUser, then .\.venv\Scripts\Activate.ps1.
Windows Git Bash bash: .venv/bin/activate: No such file or directory Windows venvs store scripts in Scripts/, not bin/ Run source .venv/Scripts/activate (with forward slashes).
Linux / macOS (Bash/Zsh) bash: ./.venv/bin/activate: Permission denied (or env doesn’t stay active) Executed directly in a subshell instead of sourcing into current shell Run source .venv/bin/activate or . .venv/bin/activate.
Debian / Ubuntu Linux No such file or directory (activate missing after python3 -m venv) System python3-venv / ensurepip package is not installed Run sudo apt install python3-venv, remove broken .venv, and recreate.
Moved / Cross-OS .venv bad interpreter or Fatal Python error: init_fs_encoding .venv was copied/moved to a new directory or between Windows & WSL/Linux Delete .venv (rm -rf .venv) and rebuild fresh with python3 -m venv .venv.