zwol
zwol

Reputation: 140866

`pip install --upgrade pip` fails inside a Windows virtualenv with "Access denied"

On Windows, if you try to use pip to upgrade itself, inside a virtualenv, you may get a mysterious "access is denied" error. For instance:

D:\scratch\> C:\Program Files\Python\3.7.4\x64\python.exe -m venv D:\scratch\my-venv
D:\scratch\> D:\scratch\my-venv\Scripts\activate
(my-venv) D:\scratch\> pip install --upgrade pip

Collecting pip
  Downloading pip-19.3.1-py2.py3-none-any.whl (1.4MB)
Installing collected packages: pip
  Found existing installation: pip 19.0.3
    Uninstalling pip-19.0.3:
Could not install packages due to an EnvironmentError: 
  [WinError 5] Access is denied: 'd:\\scratch\\my-venv\\scripts\\pip.exe'
Consider using the `--user` option or check the permissions.

This happens whether or not the command prompt has administrative privileges. We know we have write access to everything inside d:\scratch\my-venv, because we just created it with the initial python -m venv command. The advice to use the --user option is unhelpful, since we want to upgrade the version of pip inside the virtualenv, which --user will not do.

What could be wrong, and what is the correct way to upgrade pip inside a virtualenv on Windows?

Upvotes: 11

Views: 8115

Answers (4)

Andrew
Andrew

Reputation: 497

Same error for me, but in both conditions: my system pip and virtualenv pip. So, when I tried to upgrade my system pip, hopefully it wasn't like totally deleted, I could still use the "pip" command. However I know the upgrade system pip failed. When I tried the command again, it said pip was on the latest version. Maybe this is just a glitch. I believe it is the same for virtual environments(virtualenv, venv). When I upgrade the system pip I get this error:

ERROR: Could not install packages due to an OSError: [WinError 5] Access is denied: 'C:\\Users\\heewo\\AppData\\Local\\Temp\\pip-uninstall-8ob_krif\\pip.exe'
Consider using the `--user` option or check the permissions.

I know this is not normal. But still, different than the virtualenv, "pip" is still installed. I believe this is still ignorable as this is just a known issue and most people here know about this topic. For me, python -m pip install --upgrade pip did not work in this state.
I tried this method, and this did work for me.

virtualenv --pip [VERSION]

and replace VERSION with the latest version available on pip. This prints out that what version you are attempting to upgrade to, when you get the error.
And that should do the trick.

Upvotes: 0

Adrian Larry
Adrian Larry

Reputation: 11

You must have the same version of pip installed in windows as in the virtual env. I think that is the reason for the error Access Denied in Virtual Env.

in promt,

python -m pip install --upgrade pip

located in the Scripts folder of the virtual env, execute the update command upgrade pip in venv

Upvotes: 1

3Ring
3Ring

Reputation: 59

I would have put this in a comment to zwol's answer, but I don't have enough reputation yet.

I just wanted to add to anyone else potentially coming across this from google like I did, that python -m pip install --upgrade pip did fix this issue for me. However if you try pip install --upgrade pip before doing that, something happens in the process before you hit the access denied error that messes up pip. I personally was getting ModuleNotFoundError: No module named 'pip' after trying to upgrade the normal way.

Once I deleted and restarted my virtualenv and had the first command be python -m pip install --upgrade pip It worked just fine.

I hope that helps other newbies out there struggling like me! :)

Upvotes: 0

zwol
zwol

Reputation: 140866

I don't know if this is the only reason this can happen, but notice that the "Access is denied" error points at d:\scratch\my-venv\scripts\pip.exe. pip is trying to replace itself, and Windows doesn't allow you to modify a running EXE file in any way.

A workaround for this specific problem is to use python -m pip install --upgrade pip instead. This way, pip.exe is not running, so Windows will allow it to be replaced. This action doesn't try to overwrite d:\scratch\my-venv\scripts\python.exe, and Windows doesn't care what pip does to all the other files belonging to the pip package.

See https://github.com/pypa/pip/issues/188 and https://github.com/pypa/pip/issues/1299 for further information.

Upvotes: 29

Related Questions