Python 使用 pip 安装软件包时出现错误 403
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46967488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting error 403 while installing package with pip
提问by Keshaw_pathak
getting an error while installing google app engine using pip
使用 pip 安装谷歌应用引擎时出错
Collecting google_appengine
Downloading google-appengine-1.5.1.tar.gz (897kB)
100% |████████████████████████████████| 901kB 1.9MB/s
Complete output from command python setup.py egg_info:
Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.14.tar.gz
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/88/5jq5gz011sl63h_37k_qftv40000gn/T/pip-build-vxx8Ov/google-appengine/setup.py", line 3, in <module>
ez_setup.use_setuptools()
File "/Users/user/path to project/project/venv/lib/python2.7/site-packages/ez_setup.py", line 145, in use_setuptools
return _do_download(version, download_base, to_dir, download_delay)
File "/Users/user/path to project/project/venv/lib/python2.7/site-packages/ez_setup.py", line 124, in _do_download
to_dir, download_delay)
File "/Users/user/path to project/project/venv/lib/python2.7/site-packages/ez_setup.py", line 193, in download_setuptools
src = urlopen(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 437, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 475, in error
return self._call_chain(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: SSL is required
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/88/5jq5gz011sl63h_37k_qftv40000gn/T/pip-build-vxx8Ov/google-appengine/`
回答by ALex_hha
It's because PyPI has disabled non HTTPS access to APIs
这是因为 PyPI 禁用了对 API 的非 HTTPS 访问
https://mail.python.org/pipermail/distutils-sig/2017-October/031712.html
https://mail.python.org/pipermail/distutils-sig/2017-October/031712.html
as workaround you can use
作为您可以使用的解决方法
$ pip install xxxx -i https://pypi.python.org/simple/
回答by Keshaw_pathak
Got the error. The problem was with the repo from where pip was trying to install the package.
得到错误。问题出在 pip 试图安装软件包的仓库中。
回答by Massimo
Unfortunately none of the previous answers work for me.
不幸的是,以前的答案都不适合我。
IMHO it was very stupid pip / distutils chose to break packages on http repos.
恕我直言,pip / distutils 选择破坏 http 存储库上的软件包是非常愚蠢的。
I think a better choice would have been:
我认为更好的选择是:
pip/distutils use https by default
in case of error, like 403, pip has to suggest you "the package repo is on http, do you want to download it?"
pip/distutils 默认使用 https
如果出现错误,例如 403,pip 必须建议您“包 repo 在 http 上,您要下载吗?”
Still in 2020 many Python 2 packages are on http repos; with their decision, the installation of these packages is broken.
仍然在 2020 年,许多 Python 2 包都在 http 存储库上;根据他们的决定,这些软件包的安装被破坏了。
The working solution for me is a very simple patch of one python core modules:
我的工作解决方案是一个非常简单的python核心模块补丁:
--- /usr/local/lib/python2.7/urllib2.py.original
+++ /usr/local/lib/python2.7/urllib2.py
@@ -427,6 +427,9 @@
req = meth(req)
response = self._open(req, data)
+ if protocol == "http" and response.code == 403 :
+ if isinstance(fullurl, basestring) and fullurl.startswith("http://pypi.python.org/packages/source/d/distribute/") :
+ return self.open(fullurl.replace("http://", "https://"), data = data, timeout = timeout)
# post-process response
meth_name = protocol+"_response"
Working: if the failed url is on http, retry on https.
工作:如果失败的 url 在 http 上,请在 https 上重试。
I know it is a little ugly, but it is very clear and also you can revert to the original module in a snap (make a copy of /usr/local/lib/python2.7/urllib2.pybefore to apply this patch).
我知道它有点难看,但它非常清楚,您也可以快速恢复到原始模块(在应用此补丁之前制作/usr/local/lib/python2.7/urllib2.py的副本) .