When running Python scripts on a Windows computer, you might get the following error:
Could not install packages due to an EnvironmentError:
[WinError 5] Access is denied
This error occurs because Python is unable to perform operations on a certain folder.
For example, you might want to install a package using pip
, but the folder where pip
stores the package is inaccessible. The reason might be that the folder requires admin permissions.
Let’s see an example that causes this error and how to fix it.
How to fix this error
Suppose you try to install a package using pip
, but Windows gives the following error:
Could not install packages due to an EnvironmentError:
[WinError 5] Access is denied:
Consider using the --user option or check the permissions.
This error means that the command prompt that you used to run pip install
command doesn’t have sufficient permissions to install the package.
One solution that could work is to install the package using the --user
option as follows:
pip install --user <package_name>
# Example:
pip install --user numpy
The --user
option instructs Python to install the package in the user’s home directory instead of the system directory.
If that doesn’t work, then try to use python -m
command to access pip
and run the install command:
python -m pip install --user <package_name>
# Example:
python -m pip install --user numpy
If you’re using Jupyter Notebook, try the following command:
!python -m pip install <package_name>
# or:
!python -m pip install --user <package_name>
If none of the solutions above work, then you need to run the script as administrator.
Open the Start menu and search for “command”, then select the Run as administrator menu as shown below:
Run the Python script using the command prompt and see if it works.
If that doesn’t work, then you might have a corrupt Python version that’s not working as intended.
You need to uninstall Python from the Control Panel, then install the latest version from Python’s official website.
This fresh install should allow your Python scripts to run properly and stop this error.