Python PIP Install 抛出 TypeError: 不支持操作数类型 for-= : ‘ Retry’和‘ int’

在我的 Ubuntu 16.04系统和 python 2.7.11 + 上,对任何模块使用 pip install都会抛出以下错误:

TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

Pip 怎么了? 如果有必要,我怎么重新安装它?

更新: 完整的回溯如下

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'
120412 次浏览

Ubuntu 提供了一个前寒武纪时期的 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

正如您所看到的,我包含了有关 Python 2.x 和3. x 的信息

如果您位于代理之后,则必须在开始安装之前执行一些额外的配置步骤。你必须将代理地址设定为环境变量的 http _ proxy。使用 bash 可以通过命令完成

export http_proxy="http://user:pass@my.site:port/"

还可以提供

--proxy=[user:pass@]url:port

参数到 pip。 [user:pass@]部分是可选的。

我也有这个问题。最初,设置了一个代理并使其工作正常。然后我连接到一个不通过代理的网络。取消代理 pip 后再次获得工作。

unset http_proxy; unset http_prox;  unset HTTP_PROXY; unset HTTPS_PROXY

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.

但是即使删除了代理设置,我仍然得到相同的错误。

So, when I did type

env | grep proxy

我发现了这样的东西:

http_proxy=http://127.0.0.1:1234/

这就是为什么我仍然得到相同的错误,即使我认为我已经删除了代理设置。

要取消此代理的设置,请键入

unset http_proxy

对所有其他条目(例如 https _ proxy)采用相同的方法。

端口443没有打开,如果在 AWS else 上为出站连接打开端口443,只允许自定义 tcp 端口443..。

I was facing similar issue while trying to install awscli tool on ec2 instance. 我更改了安全组以允许端口443进出访问,这为我解决了问题。

当我试图用命令 virtualenv myVirtualEnv创建 virtualenv时,出现了这个错误。我只是在命令之前添加了一个 sudo; 它解决了所有问题。

奇怪的是,如果我从环境中删除代理,并将其添加到命令行中,它将为我工作。例如,升级 pip本身:

env http_proxy= https_proxy= pip install pip --upgrade --proxy 'http://proxy-url:80'

My issue was having the proxy in the environment. It seems that pip only honors the one in argument.

这里发生的情况是,当请求/urllib3的供应版本导入到两个不同的地方(相同的代码,但名称不同)时,会发生冲突。如果然后出现了网络错误,它不会重新尝试获取轮子,但是会因为上面的错误而失败。有关此错误的更深入信息,请参见 给你

有关具有系统 pip 的解决方案,请参见 above

如果你在 python -m venv构建的 Virtual alenv 中遇到这个问题(即使你单独安装了 pip,它仍然会从 /usr/share/python-wheels拷贝车轮) ,最简单的“修复”方法似乎是:

  1. create the virtualenv: /usr/bin/python3.6 -m venv ...
  2. requests安装到环境中(这可能会引发上述错误) : <venv>/bin/pip install requests
  3. 删除将被 pip: rm <venv>/share/python-wheels/{requests,chardet,urllib3}-*.whl使用的 requests的复制版本

现在,<venv>/bin/pip使用已安装的版本 requests,它提供了 urllib3。

Just upgrade pip worked for me:

pip install --upgrade pip

这是我发现的这个问题的有效解决方案。

sudo apt-get clean
cd /var/lib/apt
sudo mv lists lists.old
sudo mkdir -p lists/partial
sudo apt-get clean
sudo apt-get update

解决方案:
1. sudo apt remove python-pip
2. pip3 install pip(或按 Get-pip.py安装管道)

原因:
This error occurred on pip 8.0.1 which installed by apt-get. And happened only when your network is unstable.

如果您有一个使用 apt 安装的 pip,它会通过其他方式隐藏您安装的 pip,因此您应该首先删除 apt。

I disconnected the network and tested 8.0.1, 9.0.3, 10.x the 3 versions installed with pip3 or get-pip.py, no error occurred.   So, I think only the apt version of pip 8.0.1 has that bug, the others is ok.

For myself, it turns out that wlan0 was down, which resulted in me being unable to connect out. So, ensuring that wlan0 was up, allowed pip / pip3 to work without issue.

当我在 Adafruit 用 /Adafruit-pitft.sh安装一个覆盆子/PI TFT 时,我也遇到了同样的问题。

我不喜欢编码风格,这种风格的错误来自某个地方,可以用某种方式进行解释——从前面的答案中就可以看出来。

备注: retry.py的类型错误异常显然是一个错误,这是由于不适当地将类 答复的一个实例分配和计算到一个 Int,其默认值为 10-在代码的某个地方..。 应该通过添加 inplace 操作符或修复错误的赋值来修复。

因此,首先尝试对误差本身进行分析和修补。在我的案例中,实际的错误与 Pip调用的 Retry py相同。

安装脚本 Adafruit-pitft.sh/尝试应用 Urllib3,而 Urllib3本身尝试通过 Pip安装嵌套依赖项,所以出现了同样的错误。

Adafruit-pitft.sh # or 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: 不支持操作数类型 for-= : ‘ Retry’和‘ int’

For the current distribution(based on debian-9.6.0/stretch):

文件“/usr/share/python-Wheels/urllib3-1.19.1-py2. py3-none-any. whl/urllib3/util/retry.py”,第315行,增量

总计-= 1

TypeError: 不支持操作数类型 for-= : ‘ Retry’和‘ int’

下面的-Dirty * :)-patch 启用一个探测错误跟踪:

# 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

临时补丁的声音输出是(显示两次... ?) :

在连接被’ConnectTimeoutError (< requests.packages.urllib3.connect. VerifiedHTTPSConnection 对象/中断后重试(Retry (total = 1,connect = Nothing,read = Nothing,redirect = Nothing))

连接中断后重试(重试(总数 = 0,连接 = 无,读取 = 无,重定向 = 无)) ’ConnectTimeoutError (< requests.packages.urllib3.connect. VerifiedHTTPSConnection 对象/

无法找到满足要求 evdev 的版本(来自版本:)

没有找到与 evdev 匹配的发行版

警告: 皮普安装软件失败!

因此,在我的案例中,实际上有两个因素导致了错误,这在其他环境中可能会有所不同:

  1. Missing Evdev => try to install
  2. 未能连接包含 Evdev的 repo/dist 以便下载。 = > 最终放弃它

我的安装环境是从一个内部 debian + raspbian 镜像离线的,因此 不想设置代理..。

所以我继续手动安装缺失的部件 Evdev:

  1. 从 PyPI (或 github.com)下载 evdev:

    Https://pypi.org/project/evdev/

    Https://files.pythonhosted.org/packages/7e/53/374b82dd2ccec240b7388c65075391147524255466651a14340615aabb5f/evdev-1.1.2.tar.gz

  2. 解压并手动安装为 用户-对于所有本地帐户,所以检测到安装:

    Sudo Su -

    Tar xf evdev-1.1.2.tar. gz

    Cd evdev-1.1.2

    Python setup.py 安装

  3. 再次调用安装脚本:

    Adafruit-pitft.sh # or pitft.sh

    ... 回答对话..。

    就是这样。

如果您通过直接 PyPI访问在线进行操作:

  1. check your routing + firewall for access to pypi.org

  2. 如果需要,设置一个代理(http _ xy/https _ xy)

And it works..

希望这对其他案子也有帮助。

Arno-Can Uestuensoez

----------------------------------------------

另见: 问题 -35334: https://bugs.python.org/issue35334

----------------------------------------------

另见: 问题 -1486: https://github.com/urllib3/urllib3/issues/1486

档案: https://github.com/urllib3/urllib3/blob/master/src/urllib3/util/retry.py

我尝试了上述答案:

apt-get remove python-pip python3-pip
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
python3 get-pip.py

当我尝试的时候

python get-pip.py
python3 get-pip.py

我收到了这条信息

 Could not install packages due to an EnvironmentError:
[Errno 13] Permission denied: /usr/bin/pip3 Consider using the --user
option or check the permissions.

我做了以下几个步骤,效果很好

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

更新 setuptools 对我来说效果很好。

sudo pip install --upgrade setuptools

在我的示例中,我在 sudo 模式下打开 Pycharm,并在 Pycharm 终端中运行 pip install nltk,该终端显示了此错误。用 sudo pip 安装运行解决了错误。

检查网络问题,以绕过例外情况代码

在我的例子中,我使用了一个自定义索引,该索引没有路由,因此会触发异常情况代码。异常情况 bug 仍然存在,并且仍然掩盖了真正的问题,但是我能够通过测试与其他工具(如 nc -vzw1 myindex.example.org 443)的连接并在网络启动时重试来解决这个问题。

fixed it temporary:

pip install requests -i http://a.b.com/pypi/simple --trusted-host a.b.com

永久性的:

Linux 操作系统: 将它们添加到 ~/.pip/pip.conf中(如果不存在则创建它)

[global]
index-url = http://a.b.com/pypi/simple
[install]
trusted-host = a.b.com

Ps: http://a.b.com/pypi/simple你的代理 http _ address