11

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?

5
  • Have you tried running administrator command prompt? Commented Oct 30, 2019 at 15:06
  • @EcSync Yes, the exact same thing happens whether or not the command prompt is elevated. Commented Oct 30, 2019 at 15:17
  • Possible duplicate of Windows 10 and pip upgrading - Access denied Commented Oct 30, 2019 at 15:31
  • stackoverflow.com/… Commented Oct 30, 2019 at 15:31
  • 1
    @phd That's nice and all but that earlier Q&A completely failed to come up in multiple searches, and it has also attracted incorrect answers (administrative privileges absolutely do not help in the scenario I'm talking about). I wrote this one to fix the google juice on this problem, basically. Commented Oct 30, 2019 at 15:35

4 Answers 4

29

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. As suggested, running python -m pip install --upgrade pip does work. It would be a REALLY good idea for somebody to add a check. It is so easy to have the venv not having pip at all. Uninstall succeeds, install doesn't.
2

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

Comments

0

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! :)

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.