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
ExecutionPolicyscope (CurrentUservs.LocalMachinevs.Process):- Do not run
Set-ExecutionPolicy Unrestrictedglobally as Administrator whenSet-ExecutionPolicy RemoteSigned -Scope CurrentUserworks without elevation and safely allows locally created.ps1scripts while still requiring signatures on scripts downloaded from the internet. If corporate Group Policy locksCurrentUser, bypass it for a single terminal session viaSet-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process.
- Do not run
- Linux/macOS
Permission deniedorbad interpreterwhen running./.venv/bin/activate:.venv/bin/activateis intentionally not marked executable (chmod +x), because executing it in a child subshell updatesin that subshell and discards it the moment the subshell exits! Always runsource .venv/bin/activate(or POSIX. .venv/bin/activate) so environment variables modify your active shell session.
- Wrong activation script for your shell (
Git Bashon 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, notbin/activate).
- PowerShell:
- On Linux/macOS
fishshell, usesource .venv/bin/activate.fish.
- Windows virtual environments generate three distinct scripts inside
- Broken activation after moving, renaming, or sharing a
.venvacross machines/OSes:- Virtual environments hardcode the absolute
VIRTUAL_ENVpath and symlink/copy the host OS’s Python binary insideactivateandpyvenv.cfg. Never commit.venv/to Git or copy a Windows.venvto Linux/WSL—delete and recreate it (python3 -m venv .venv) fromrequirements.txtorpyproject.toml.
- Virtual environments hardcode the absolute
- Missing
ensurepipon Debian/Ubuntu (The virtual environment was not created successfully):- Minimal Ubuntu/Debian installations strip
venvout of the base Python package, producing a half-built.venvwith nobin/activatefile. Install the OS package (sudo apt update && sudo apt install -y python3-venv), delete the incomplete.venv, and re-runpython3 -m venv .venv.
- Minimal Ubuntu/Debian installations strip
- Bypass shell activation entirely (zero activation errors in CI/CD, cron, IDEs, and scripts):
- Activation only prepends
.venv/bin(or.venv\Scripts) toPATH. Calling.venv/bin/python -m pip install ...or.venv\Scripts\python.exe script.pyuses the virtual environment’s interpreter and packages directly with zero shell-policy orsourceissues.
- Activation only prepends
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. |
You must log in or register to comment.
