ERROR: Could not install packages due to an OSError: [WinError 5] Access denied
ID: pip/access-denied-windows
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 24 | active | — | — | — |
Root Cause
pip cannot write to the Python installation directory on Windows because the current user lacks write permissions. This typically occurs when Python is installed system-wide (e.g., in C:\Program Files\Python311) and pip is run from a non-elevated command prompt, or when antivirus software locks files during installation. It also occurs when trying to upgrade pip itself while pip's executable is in use by the running process.
genericWorkarounds
-
92% success Use a virtual environment to isolate packages in a user-writable directory
Create and activate a virtual environment: # Create venv in project directory (user-writable): python -m venv .venv # Activate on Windows CMD: .venv\Scripts\activate.bat # Activate on Windows PowerShell: .venv\Scripts\Activate.ps1 # If PowerShell execution policy blocks activation: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser .venv\Scripts\Activate.ps1 # Now pip installs to the venv (no admin needed): pip install <package-name> # Verify install location: pip show <package-name> # Location should be under .venv\
Sources: https://docs.python.org/3/library/venv.html https://pip.pypa.io/en/stable/user_guide/
-
85% success Use --user flag to install packages in the user site-packages directory
Install to the user-level directory instead of the system directory: pip install --user <package-name> # This installs to: # C:\Users\<username>\AppData\Roaming\Python\Python311\site-packages\ # Ensure user scripts directory is in PATH: # Add to PATH: C:\Users\<username>\AppData\Roaming\Python\Python311\Scripts # To upgrade pip itself with --user: python -m pip install --user --upgrade pip # Note: --user flag does not work inside virtual environments # (it's already user-scoped there)
Sources: https://pip.pypa.io/en/stable/user_guide/#user-installs
-
80% success Close other Python processes and retry when upgrading pip itself
When the error occurs specifically during 'pip install --upgrade pip': # The running pip process locks its own executable. # Use python -m pip to avoid the locked pip.exe: python -m pip install --upgrade pip # If that still fails, close ALL Python processes first: # 1. Close all Python IDEs, Jupyter notebooks, Python terminals # 2. Check Task Manager for python.exe processes # 3. Then upgrade: python -m pip install --upgrade pip # Alternative: upgrade pip in a fresh command prompt: # Open new CMD/PowerShell (not the one currently running pip) python -m pip install --upgrade pip --force-reinstall
Sources: https://pip.pypa.io/en/stable/installation/#upgrading-pip
Dead Ends
Common approaches that don't work:
-
Always running pip as Administrator
60% fail
Running pip with elevated privileges installs packages into the system-wide Python directory, which creates security risks and can break the system Python installation. It also means all pip operations require admin privileges going forward, packages installed as admin may conflict with user-level packages, and it prevents using virtual environments properly. Microsoft's own Python documentation recommends against this.
-
Disabling Windows antivirus/Defender to prevent file locking
70% fail
While antivirus software can lock Python files during scanning, disabling it entirely creates significant security risks. The real issue is that pip is trying to write to a protected directory. Disabling antivirus is a dangerous workaround that does not address the permission model and leaves the system vulnerable. If antivirus is the actual cause, adding an exclusion for the virtual environment directory is the proper approach.