When setting up a Python virtual environment on Windows, running .\venv\Scripts\activate in PowerShell often throws a script execution policy error (such as PSSecurityException or cannot be loaded because running scripts is disabled on this system).
Here is a quick walkthrough to resolve this issue safely without disabling your system's global security policies.
The Cause
Windows PowerShell restricts script execution by default under the Restricted policy to prevent malicious scripts from executing. Because virtual environment activation scripts (Activate.ps1) are local scripts, PowerShell blocks them.
Step-by-Step Fix
1. Open PowerShell
Launch PowerShell (or your integrated terminal in VS Code).
2. Update Execution Policy for Current User
Run the following command to allow locally generated scripts to execute for your Windows profile only:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Why -Scope CurrentUser?
Scope limiting ensures you only grant permission to your current user session, eliminating the need to alter global system policies or risk security system-wide.
Verification Workflow
Once the policy is set, you can create and activate your environment seamlessly:
# 1. Create your virtual environment
python -m venv venv
# 2. Activate the virtual environment
.\venv\Scripts\activate
# Output: Your prompt will now show the environment prefix:
# (.venv) PS C:\your-project-path>
Summary
Error: PowerShell blocks Activate.ps1.
Fix: Set -ExecutionPolicy RemoteSigned scoped to -Scope CurrentUser.
Security: Keeps remote unsigned scripts blocked while allowing local development tools to function smoothly.
Top comments (0)