如何在Windows上更新Python PIP?
使用pip install --upgrade pip
命令更新pip时出现问题,因为Windows的文件系统有点问题,不允许你删除一个正在使用的文件。
我尝试把环境设置到我想更新的虚拟环境,然后用另一个pip来运行,但这样做也失败了,出现了以下错误:
(jm) E:\python\jm>c:\Python26\Scripts\pip install --upgrade pip
Downloading/unpacking pip
Running setup.py egg_info for package pip
warning: no previously-included files matching '*.txt' found under directory 'docs\_build'
no previously-included directories found matching 'docs\_build\_sources'
Installing collected packages: pip
Found existing installation: pip 0.7.1
Uninstalling pip:
Successfully uninstalled pip
Running setup.py install for pip
warning: no previously-included files matching '*.txt' found under directory 'docs\_build'
no previously-included directories found matching 'docs\_build\_sources'
Installing pip-script.py script to c:\Python26\Scripts
Installing pip.exe script to c:\Python26\Scripts
Installing pip.exe.manifest script to c:\Python26\Scripts
Installing pip-2.6-script.py script to c:\Python26\Scripts
Installing pip-2.6.exe script to c:\Python26\Scripts
Installing pip-2.6.exe.manifest script to c:\Python26\Scripts
Exception:
Traceback (most recent call last):
File "c:\Python26\lib\site-packages\pip-0.7.1-py2.6.egg\pip\basecommand.py", line 120, in main
File "c:\Python26\lib\site-packages\pip-0.7.1-py2.6.egg\pip\commands\install.py", line 165, in run
File "c:\Python26\lib\site-packages\pip-0.7.1-py2.6.egg\pip\req.py", line 1251, in install
File "c:\Python26\lib\site-packages\pip-0.7.1-py2.6.egg\pip\req.py", line 466, in commit_uninstall
File "c:\Python26\lib\site-packages\pip-0.7.1-py2.6.egg\pip\req.py", line 1549, in commit
File "c:\Python26\lib\shutil.py", line 216, in rmtree
rmtree(fullname, ignore_errors, onerror)
File "c:\Python26\lib\shutil.py", line 216, in rmtree
rmtree(fullname, ignore_errors, onerror)
File "c:\Python26\lib\shutil.py", line 221, in rmtree
onerror(os.remove, fullname, sys.exc_info())
File "c:\Python26\lib\shutil.py", line 219, in rmtree
os.remove(fullname)
WindowsError: [Error 5] Access is denied: 'c:\\users\\mark\\appdata\\local\\temp\\pip-gvsove-uninstall\\python26\\scripts\\pip.exe'
Storing complete log in C:\Users\mark\AppData\Roaming\pip\pip.log
(jm) E:\python\jm>dir c:\Users\mark\AppData\Local\temp\pip-gvsove-uninstall\python26\scripts
Volume in drive C has no label.
Volume Serial Number is 74E4-FE9F
Directory of c:\Users\mark\AppData\Local\temp\pip-gvsove-uninstall\python26\scripts
12/07/2010 11:32 AM <DIR> .
12/07/2010 11:32 AM <DIR> ..
05/14/2010 05:54 PM 7,168 pip.exe
1 File(s) 7,168 bytes
2 Dir(s) 22,824,603,648 bytes free
(jm) E:\python\jm>del c:\Users\mark\AppData\Local\temp\pip-gvsove-uninstall\python26\scripts\pip.exe
我希望其他人能找到解决这个问题的方法。在Linux上就没有这个问题……
6 个回答
[更新 2015-11-15] 这篇文章已经过时了。现在的最佳做法是 根据 pip 的说明 使用以下方法:
python -m pip install -U pip setuptools
不需要使用 easy_install
这个工具,它来自 setuptools。更新 pip 的推荐方法是根据 pip-installer 页面,使用 get-pip.py
。
要安装或升级 pip,安全地下载
get-pip.py
。
你可以在 Bash shell 中使用,比如 msysgit 的 Git Bash。
user@machine ~$ curl -L https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py | python
使用 -L
选项来跟随重定向。最近 GitHub 改变了原始内容的链接,所以 旧的原始 get-pip.py
链接 已经被 重定向,而在 pip 网站上 链接也被更改。如果成功,你应该会看到以下内容:
$ curl -L https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py | python
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1309k 100 1309k 0 0 364k 0 0:00:03 0:00:03 --:--:-- 366k
Downloading/unpacking pip from https://pypi.python.org/packages/py2.py3/p/pip/pip-1.5.6-py2.py3-none-any.whl#md5=4d4fb4b69df6731c7aeaadd6300bc1f2
Installing collected packages: pip
Found existing installation: pip 1.5.4
Uninstalling pip:
Successfully uninstalled pip
Successfully installed pip
Cleaning up...
你可以根据 Windows 命令行进行修改,但首先如果你没有 libcurl,可以从 Curl 下载一个 Windows 版本。你需要滚动到页面底部,下载 Günter Knauf 当前的官方 Win32 版本(目前是 7.29),然后解压缩。不要担心是 32 位还是 64 位,只要确保下载的是包含所有功能(ssl、zlib 等)的版本即可。现在,进入你下载 curl 的文件夹,可能是 c:\Users\<你>\Downloads
,打开解压后的文件夹,可能是 curl-7.29.0-rtmp-ssh2-ssl-sspi-zlib-idn-static-bin-w32
,你应该能找到 curl.exe
。太好了!现在在 Windows 命令行中,在提示符后输入以下内容(提示符会显示你的用户名,而不是 myusername
),将 path\to\Curl\
替换为你下载的 curl.exe
文件的路径。
C:\Users\myusername> path\to\Curl\curl.exe -kL https://bootstrap.pypa.io/get-pip.py | python
你需要使用 -k
选项,以允许 curl 在没有证书的情况下访问安全的 GitHub pip 网站。此外,感谢 Piotr Dobrogost 告诉我关于 Windows 管道 |
的信息,这样可以省去保存文件的步骤。
如果你已经有最新版本的 pip,你会看到以下消息:
$ curl https://bootstrap.pypa.io/get-pip.py | python
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1309k 100 1309k 0 0 369k 0 0:00:03 0:00:03 --:--:-- 371k
Requirement already up-to-date: pip in c:\python27\lib\site-packages
Cleaning up...
在虚拟环境中也能正常工作。
用python来执行pip,就像运行一个脚本一样。
python -m pip install -U pip
这个命令是用来更新一个叫做“pip”的工具的。pip是Python编程语言中用来安装和管理软件包的工具。简单来说,运行这个命令就可以让你的pip工具变得更好,拥有最新的功能和修复。
:-)