Python PIP 安装抛出 TypeError: 不支持的操作数类型 -=: 'Retry' 和 'int'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37495375/
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
Python PIP Install throws TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
提问by devautor
Using pip install
for any module apparently on my Ubuntu 16.04 system with python 2.7.11+ throws this error:
使用pip install
了显然与蟒蛇2.7.11+我的Ubuntu 16.04系统中的任何模块抛出这个错误:
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
What is wrong with pip? How could I reinstall it, if necessary?
pip 有什么问题?如果需要,我该如何重新安装它?
Update: Full traceback is below
更新:完整回溯如下
sunny@sunny:~$ pip install requests
Collecting requests
Exception:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 209, in main
status = self.run(options, args)
File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 328, in run
wb.build(autobuilding=True)
File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 748, in build
self.requirement_set.prepare_files(self.finder)
File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 360, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 512, in _prepare_file
finder, self.upgrade, require_hashes)
File "/usr/lib/python2.7/dist-packages/pip/req/req_install.py", line 273, in populate_link
self.link = finder.find_requirement(self, upgrade)
File "/usr/lib/python2.7/dist-packages/pip/index.py", line 442, in find_requirement
all_candidates = self.find_all_candidates(req.name)
File "/usr/lib/python2.7/dist-packages/pip/index.py", line 400, in find_all_candidates
for page in self._get_pages(url_locations, project_name):
File "/usr/lib/python2.7/dist-packages/pip/index.py", line 545, in _get_pages
page = self._get_page(location)
File "/usr/lib/python2.7/dist-packages/pip/index.py", line 648, in _get_page
return HTMLPage.get_page(link, session=self.session)
File "/usr/lib/python2.7/dist-packages/pip/index.py", line 757, in get_page
"Cache-Control": "max-age=600",
File "/usr/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl/requests/sessions.py", line 480, in get
return self.request('GET', url, **kwargs)
File "/usr/lib/python2.7/dist-packages/pip/download.py", line 378, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File "/usr/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl/requests/sessions.py", line 468, in request
resp = self.send(prep, **send_kwargs)
File "/usr/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl/requests/sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/adapter.py", line 46, in send
resp = super(CacheControlAdapter, self).send(request, **kw)
File "/usr/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl/requests/adapters.py", line 376, in send
timeout=timeout
File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 610, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/util/retry.py", line 228, in increment
total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
回答by sorin
Ubuntu comes with a version of PIP from precambrian and that's how you have to upgrade it if you do not want to spend hours and hours debugging pip related issues.
Ubuntu 附带了一个来自 precambrian 的 PIP 版本,如果您不想花费大量时间调试 pip 相关问题,那么您必须升级它。
apt-get remove python-pip python3-pip
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
python3 get-pip.py
As you observed I included information for both Python 2.x and 3.x
正如您所观察到的,我包含了 Python 2.x 和 3.x 的信息
回答by Livruen Nati
If you are behind a proxy, you must do some extra configuration steps before starting the installation. You must set the environment variable http_proxy to the proxy address. Using bash this is accomplished with the command
如果您使用代理,则必须在开始安装之前执行一些额外的配置步骤。您必须将环境变量 http_proxy 设置为代理地址。使用 bash 这是通过命令完成的
export http_proxy="http://user:[email protected]:port/"
You can also provide the
您还可以提供
--proxy=[user:pass@]url:port
parameter to pip. The [user:pass@]
portion is optional.
pip 的参数。该[user:pass@]
部分是可选的。
回答by Matheswaran Kanagarajan
Updating setuptools has worked out fine for me.
更新 setuptools 对我来说效果很好。
sudo pip install --upgrade setuptools
回答by thisisashwani
First of all, this problem exists because of network issues, and uninstalling and re-installing everything won't be of much help. Probably you are behind proxy, and in that case you need to set proxy.
首先,这个问题是因为网络问题而存在的,卸载重新安装一切都没有多大帮助。可能你落后于代理,在这种情况下你需要设置代理。
But in my case, I was facing the problem because I wasn't behind proxy. Generally, I work behind proxy, but when working from home, I set the proxy to None in Network settings.
但就我而言,我面临这个问题,因为我没有落后于代理。通常,我在代理后面工作,但在家工作时,我在网络设置中将代理设置为无。
But I was still getting the same errors even after removing the proxy settings.
但是即使删除了代理设置,我仍然遇到相同的错误。
So, when I did type
所以,当我输入
env | grep proxy
I found something like this :
我发现了这样的东西:
http_proxy=http://127.0.0.1:1234/
And this was the reason I was still getting the very same error, even when I thought I had removed the proxy settings.
这就是我仍然收到同样错误的原因,即使我认为我已经删除了代理设置。
To unset this proxy, type
要取消设置此代理,请键入
unset http_proxy
Follow the same approach for all the other entries, such as https_proxy.
对所有其他条目(例如 https_proxy)采用相同的方法。
回答by Jan Katins
What happens here is that the the vendored versions of request/urllib3 clash when imported in two different places (same code, but different names). If you then have a network error, it doesn't retry to get the wheel, but fails with the above error. See herefor a deeper dive into this error.
这里发生的情况是,当在两个不同的地方(相同的代码,但不同的名称)导入时,request/urllib3 的供应商版本会发生冲突。如果您随后遇到网络错误,它不会重试获取轮子,但会因上述错误而失败。请参阅此处更深入地了解此错误。
For the solution with system pip, see above.
系统pip的解决方法见上。
If you have this problem in a virtualenv built by python -m venv
(which still copies the wheels from /usr/share/python-wheels
, even if you have pip installed separately), the easiest way to "fix" it seems to be:
如果您在构建的 virtualenv 中遇到此问题python -m venv
(/usr/share/python-wheels
即使您单独安装了 pip,它仍然从 复制轮子),“修复”它的最简单方法似乎是:
- create the virtualenv:
/usr/bin/python3.6 -m venv ...
- install
requests
into the environment (this might raise the above error):<venv>/bin/pip install requests
- remove the copied versions of
requests
which would be used by pip:rm <venv>/share/python-wheels/{requests,chardet,urllib3}-*.whl
- 创建虚拟环境:
/usr/bin/python3.6 -m venv ...
- 安装
requests
到环境中(这可能会引发上述错误):<venv>/bin/pip install requests
- 删除
requests
pip 将使用的复制版本:rm <venv>/share/python-wheels/{requests,chardet,urllib3}-*.whl
Now a <venv>/bin/pip
uses the installed version of requests
which has urllib3 vendored.
现在 a<venv>/bin/pip
使用已安装的requests
urllib3 版本。
回答by Deepak
port 443 is not open, just allow custom tcp port 443 if on AWS else open the port 443 for the outbound connections ...
端口 443 未打开,如果在 AWS 上,则只允许自定义 tcp 端口 443,否则为出站连接打开端口 443 ...
回答by cleicar
Just upgrade pip worked for me:
只需升级 pip 对我有用:
pip install --upgrade pip
pip install --upgrade pip
回答by acue
I have the same problem when installing a RaspberryPI TFT from Adafruit with pitft.sh/ adafruit-pitft.sh.
使用pitft.sh/ adafruit-pitft.sh从 Adafruit 安装 RaspberryPI TFT 时,我遇到了同样的问题。
I am not happy about coding-styles with errors from somewhere to be interpreted somehow - as could be seen by the previous answers.
我对来自某个地方的错误以某种方式解释的编码风格不满意 - 正如之前的答案所见。
Remark: The type error exception of retry.pyis obviously a bug, caused by an unappropriate assignement and calculation of an instance of the class Replyto an intwith the default value of 10- somewhere in the code... Should be fixed either by adding an inplace-operator, or fixing the erroneous assignment.
备注:的类型错误异常retry.py显然是一个错误,所造成的类的实例的unappropriate assignement和计算回复到INT与默认值10-在代码的某个地方......应该是固定的通过添加就地运算符,或修复错误的分配。
So tried to analyse and patch the error itself first. The actual error in my case case is the same - retry.pycalled by pip.
所以尝试首先分析和修补错误本身。在我的案例中的实际错误是相同的 - retry.py由pip调用。
The installation script adafruit-pitft.sh/ pitft.shtries to apply urllib3which itself tries to install nested dependencies by pip, so the same error.
安装脚本adafruit-pitft.sh/ pitft.sh尝试应用urllib3,它本身尝试通过pip安装嵌套依赖项,因此出现相同的错误。
adafruit-pitft.sh # or pitft.sh
...
_stacktrace=sys.exc_info()[2]) File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3 none-any.whl/urllib3/util/retry.py", line 228, in increment
total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
adafruit-pitft.sh # 或 pitft.sh
...
_stacktrace=sys.exc_info()[2]) 文件“/usr/share/python-wheels/urllib3-1.13.1-py2.py3 none-any.whl/urllib3/util/retry.py”,第 228 行,在增量
总计 -= 1
TypeError: 不支持的操作数类型 -=: 'Retry' 和 'int'
For the current distribution(based on debian-9.6.0/stretch):
对于当前发行版(基于 debian-9.6.0/stretch):
File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/util/retry.py", line 315, in increment
total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
文件“/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/util/retry.py”,第315行,增量
总计 -= 1
TypeError: 不支持的操作数类型 -=: 'Retry' 和 'int'
The following - dirty *:) - patch enables a sounding error trace:
以下 - 脏 *:) - 补丁启用了探测错误跟踪:
# File: retry.py - in *def increment(self, ..* about line 315
# original: total = self.total
# patch: quick-and-dirty-fix
# START:
if isinstance(self.total, Retry):
self.total = self.total.total
if type(self.total) is not int:
self.total = 2 # default is 10
# END:
# continue with original:
total = self.total
if total is not None:
total -= 1
connect = self.connect
read = self.read
redirect = self.redirect
cause = 'unknown'
status = None
redirect_location = None
if error and self._is_connection_error(error):
# Connect retry?
if connect is False:
raise six.reraise(type(error), error, _stacktrace)
elif connect is not None:
connect -= 1
The sounding output with the temporary patch is(displayed twice...?):
临时补丁的声音输出是(显示两次......?):
Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at/
Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at/
Could not find a version that satisfies the requirement evdev (from versions: )
No matching distribution found for evdev
WARNING : Pip failed to install software!
连接被 'ConnectTimeoutError(<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at/
连接被 'ConnectTimeoutError(<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at/
找不到满足 evdev 要求的版本(来自版本:)
找不到与 evdev 匹配的发行版
警告:Pip 安装软件失败!
So in my case actually two things cause the error, this may vary in other environments:
所以在我的情况下,实际上有两件事导致了错误,这在其他环境中可能会有所不同:
- Missing evdev=> try to install
- Failed to connect a repo/dist containing evdevin order to download. => finally give it up
- 缺少evdev=> 尝试安装
- 无法连接包含evdev的 repo/dist以进行下载。=> 终于放弃了
My installation environment is offline from an internal debian+raspbian mirror, thus do not want to set the proxy...
我的安装环境是从内部 debian+raspbian 镜像离线的,因此不想设置代理...
So I proceeded by manual installation of the missing component evdev:
所以我继续手动安装缺少的组件evdev:
download evdev from PyPI(or e.g. from github.com):
Unpack and install manually as rootuser - for all local accounts, so detected as installed:
sudo su -
tar xf evdev-1.1.2.tar.gz
cd evdev-1.1.2
python setup.py install
Call install script again:
adafruit-pitft.sh # or pitft.sh
...Answer dialogues...
...that's it.
从 PyPI(或例如从 github.com)下载 evdev:
以root用户身份手动解压并安装- 对于所有本地帐户,因此检测为已安装:
须藤 -
tar xf evdev-1.1.2.tar.gz
cd evdev-1.1.2
python setup.py 安装
再次调用安装脚本:
adafruit-pitft.sh # 或 pitft.sh
...回答对话...
...就是这样。
If you proceed online by direct PyPIaccess:
如果您通过直接PyPI访问在线进行:
check your routing + firewall for access to pypi.org
set a proxy if required (http_proxy/https_proxy)
检查您的路由 + 防火墙以访问pypi.org
如果需要,设置代理(http_proxy/https_proxy)
And it works..
它有效..
Hope this helps in other cases too.
希望这在其他情况下也有帮助。
Arno-Can Uestuensoez
Arno-Can Uestuensoez
----------------------------------------------
----------------------------------------------
See also: issue - 35334: https://bugs.python.org/issue35334
另请参阅:问题 - 35334:https://bugs.python.org/issue35334
----------------------------------------------
----------------------------------------------
See now also: issue - 1486: https://github.com/urllib3/urllib3/issues/1486
现在另见:问题 - 1486:https: //github.com/urllib3/urllib3/issues/1486
for file: https://github.com/urllib3/urllib3/blob/master/src/urllib3/util/retry.py
对于文件:https: //github.com/urllib3/urllib3/blob/master/src/urllib3/util/retry.py
回答by i_thamary
I tried the solution answered above:
我尝试了上面回答的解决方案:
apt-get remove python-pip python3-pip
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
python3 get-pip.py
When I tried
当我尝试
python get-pip.py
python3 get-pip.py
I got this message
我收到了这条消息
Could not install packages due to an EnvironmentError:
[Errno 13] Permission denied: /usr/bin/pip3 Consider using the --user
option or check the permissions.
I did the following and it works
我做了以下工作并且它有效
python3 -m venv env
source ./env/bin/activate
Sudo apt-get update
apt-get remove python-pip python3-pip
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
python3 get-pip.py
pip3 install pip
sudo easy_install pip
pip install --upgrade pip
回答by Aayush Arora
In my case, i had opened Pycharm in sudo mode, and was running pip install nltk in pycharm terminal which showed this error. running with sudo pip install solves the error.
就我而言,我以 sudo 模式打开了 Pycharm,并在 pycharm 终端中运行 pip install nltk 显示此错误。使用 sudo pip install 运行解决了错误。