Last updated: Apr 9, 2024
Reading timeยท5 min
The error "Could not install packages due to an EnvironmentError: [WinError 5] Access is denied" occurs when we don't have the necessary permissions to install a package.
To solve the error, run the command with the --user
option, e.g.
pip install tensorflow-gpu --user
.
ERROR: Could not install packages due to an EnvironmentError: [WinError 5] Access is denied: 'C:\\Users\\bobbyhadz\\AppData\\Roaming\\Python\\Python310\\site-packages\\Python\\Python310\\site-packages' Consider using the `--user` option or check the permissions.
--user
optionOne way to solve the error is to run the pip install
command with the --user
option.
pip install <package-name> --user pip3 install <package-name> --user python -m pip install <package-name> --user python3 -m pip install <package-name> --user
Make sure to replace the <package-name>
placeholder with the actual name of
the package, e.g. pip install tensorflow-gpu --user
.
# ๐๏ธ Install the correct pacakge with the --user option pip install tensorflow-gpu --user pip3 install tensorflow-gpu --user python -m pip install tensorflow-gpu --user python3 -m pip install tensorflow-gpu --user # ๐๏ธ On Windows py -m pip install tensorflow-gpu --user
The --user
option installs the package in the user's home directory.
However, the --user
option wouldn't work if you have a virtual environment
active.
pip
before installing the packageAnother thing that often solves the error is upgrading your pip
version before
installing the specific package.
python -m pip install --user --upgrade pip python -m pip install tensorflow-gpu python3 -m pip install --user --upgrade pip python3 -m pip install tensorflow-gpu # ๐๏ธ on Windows py -m pip install --user --upgrade pip py -m pip install tensorflow-gpu
Make sure to stop all of your Python applications before running the installation command.
tensorflow
package while you have an application running tensorflow
in the background, the error is raised.You have to stop your script and rerun the installation command.
If that didn't help, run CMD as an administrator and install the package.
To run CMD as an administrator:
pip install
command.pip install <package-name> pip3 install <package-name> python -m pip install <package-name> python3 -m pip install <package-name>
pip install
command.If opening CMD as an administrator didn't help, try to open PowerShell as an administrator and run the command.
To run PowerShell as an administrator:
pip install
command.Windows throws [WinError 5] Access is denied when the file is locked by another process.
If the error persists, change the user's access permissions.
The error is often caused because the user doesn't have access to modify the directory where the package should be installed.
To solve the error, allow the user full access to the Python directory.
To change the access permissions for the user:
C:\Program Files (x86)\Python310
.You can find where Python is installed with either of the following 2 commands.
where python python -c "import os, sys; print(os.path.dirname(sys.executable))"
pip install <package-name>
command again.Once you give "Full access" permissions to the user, you should be able to
pip install
packages without getting any errors.
If you have a virtual environment active, your Python location will be scoped to the specific virtual environment.
You can run the where python
command with your virtual environment active to
find the directory.
pip
and setuptools
If that didn't resolve the error, try upgrading your version of pip
.
# ๐๏ธ If you have pip already installed pip install --upgrade pip # ๐๏ธ If your pip is aliased as pip3 (Python 3) pip3 install --upgrade pip # ๐๏ธ If you don't have pip in your PATH environment variable python -m pip install --upgrade pip # ๐๏ธ If you don't have pip in your PATH environment variable python3 -m pip install --upgrade pip # ๐๏ธ If you have easy_install easy_install --upgrade pip
If you get the error "ModuleNotFoundError: No module named 'pip' in Python", check out my other article:
If the commands from the code sample didn't work for you, click on the "Install pip in Python" link.
After you upgrade pip, upgrade setuptools as well.
pip install --upgrade setuptools pip3 install --upgrade setuptools python -m pip install --upgrade setuptools python3 -m pip install --upgrade setuptools
The is often solved by upgrading your pip
version and re-running the
pip install
command.
If the error persists, try creating a virtual environment.
pip install
command with the virtual environment active.# ๐๏ธ Use the correct version of Python when creating VENV python3 -m venv venv # ๐๏ธ Activate on Unix or MacOS source venv/bin/activate # ๐๏ธ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐๏ธ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # ๐๏ธ Install the specific package in the virtual environment pip install numpy
Make sure to use the correct command to activate your virtual environment depending on your operating system and your shell.
Your virtual environment will use the version of Python that was used to create it.
Make sure to not create your virtual environment as root, e.g. with sudo
because then you'd only permit root users to install packages.
If you created your virtual environment using sudo
, try changing its
permissions or recreate it without sudo
.
sudo chmod -R 777 venv
The command above assumes that your virtual environment is in a folder called
venv
.
777
means granting all users full access to the contents of the directory.
If you use a virtual environment, another thing that might help is to:
venv
folder.pyvenv.cfg
file.include-system-site-packages
property to true
.pip install
command.If nothing else helped, try to restart your PC and rerun the installation command.
This might help because the package you are trying to install/upgrade might be locked by another process.
Closing the process might enable you to run the installation command.
The solve the "Could not install packages due to an EnvironmentError: [WinError 5] Access is denied" error:
pip install
command with the --user
option.