In the course of maintaining a CLI utility, I want to add an update
action that will grab the latest version of that package from PyPI and upgrade the existing installation.
$ cli -V
1.0.23
$ cli update
// many lines of pip spam
$ cli -V
1.0.24 // or etc
This is working perfectly on all machines that have Python installed system-wide (in C:\Python36
or similar), but machines that have Python installed as a user (in C:\users\username\AppData\Local\Programs\Python\Python36
) receive this error as the old version is uninstalled:
Could not install packages due to an EnvironmentError: [WinError 5] Access is denied: 'C:\\Users\\username\\AppData\\Local\\Temp\\pip-uninstall-f5a7rk2y\\cli.exe'
Consider using the `--user` option or check the permissions.
I had assumed that this is due to the fact that the cli.exe
called out in the error text is currently running when pip tries to remove it, however the path here is not to %LOCALAPPDATA%\Programs\Python\Python36\Scripts
where that exe lives, but instead to %TEMP%
. How is it allowed to move the file there, but not remove it once it's there?
including --user
in the install args as recommended by the error message does not (contrary to the indication of an earlier edit of this question) resolve the issue, but moving the cli
executable elsewhere does.
I'm hoping for an answer that:
- Explains the underlying issue of failing to delete the executable from the TEMP directory, and...
- Provides a solution to the issue, either to bypass the permissions error, or to query to see if this package is installed as a user so the code can add
--user
to the args.
While the question is fairly general, a MCVE is below:
def update(piphost):
args = ['pip', 'install',
'--index-url', piphost,
'-U', 'cli']
subprocess.check_call(args)
update('https://mypypiserver:8001')