解决树莓派下载django的TypeError:..... Retry and int 问题

王尼玛

诸位, 不好意思哈, 请允许我把完整的标题写完(标题不能超过64个有效字符):

记一次解决树莓派下载Django库 ——报错为TypeError: unsupported operand types for -=: 'Retry' and 'int'的问题(可能该问题Linux系统普遍存在)

进日, 玩起了树莓派3B+, 对我这种新手来说, 不搭服务器怎么可以, So, 选择Python的Django库, 尝试各种方法, 失败N次
报错为:

pi@raspberrypi:~ $ sudo pip install django
Collecting django
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 353, in                                                                                                              run
    wb.build(autobuilding=True)
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 380, in prepa                                                                                                             re_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 554, in _prep                                                                                                             are_file
    require_hashes
  File "/usr/lib/python2.7/dist-packages/pip/req/req_install.py", line 278, in p                                                                                                             opulate_link
    self.link = finder.find_requirement(self, upgrade)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 465, in find_requir                                                                                                             ement
    all_candidates = self.find_all_candidates(req.name)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 423, in find_all_ca                                                                                                             ndidates
    for page in self._get_pages(url_locations, project_name):
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 568, in _get_pages
    page = self._get_page(location)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 683, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 792, in get_page
    "Cache-Control": "max-age=600",
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/s                                                                                                             essions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/pip/download.py", line 386, in request
    return super(PipSession, self).request(method, url, *args, **kwargs)
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/s                                                                                                             essions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/s                                                                                                             essions.py", line 630, in send
    history = [resp for resp in gen] if allow_redirects else []
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/s                                                                                                             essions.py", line 190, in resolve_redirects
    **adapter_kwargs
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/s                                                                                                             essions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachec                                                                                                             ontrol/adapter.py", line 47, in send
    resp = super(CacheControlAdapter, self).send(request, **kw)
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/a                                                                                                             dapters.py", line 423, in send
    timeout=timeout
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/con                                                                                                             nectionpool.py", line 643, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/uti                                                                                                             l/retry.py", line 315, in increment
    total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

当然尝试pip3亦是如此

解决过程

  • First

菜鸟给的安装方法很直接的安装方法, 失败

  • Second

老方法从Python Extension Packages 下载whl 文件, 要么直接通过树莓派的网络下载, 要么通过你的电脑下载, 通过U盘拷贝到树莓派, 再安装, 当然未果, 而且报错略有不同

pi@raspberrypi:~/Desktop $ pip install --upgrade pip
Collecting pip
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 353, in                                                                                                              run
    wb.build(autobuilding=True)
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 380, in prepa                                                                                                             re_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 554, in _prep                                                                                                             are_file
    require_hashes
  File "/usr/lib/python2.7/dist-packages/pip/req/req_install.py", line 278, in p                                                                                                             opulate_link
    self.link = finder.find_requirement(self, upgrade)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 465, in find_requir                                                                                                             ement
    all_candidates = self.find_all_candidates(req.name)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 423, in find_all_ca                                                                                                             ndidates
    for page in self._get_pages(url_locations, project_name):
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 568, in _get_pages
    page = self._get_page(location)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 683, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 792, in get_page
    "Cache-Control": "max-age=600",
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/s                                                                                                             essions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/pip/download.py", line 386, in request
    return super(PipSession, self).request(method, url, *args, **kwargs)
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/s                                                                                                             essions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/s                                                                                                             essions.py", line 630, in send
    history = [resp for resp in gen] if allow_redirects else []
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/s                                                                                                             essions.py", line 190, in resolve_redirects
    **adapter_kwargs
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/s                                                                                                             essions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachec                                                                                                             ontrol/adapter.py", line 47, in send
    resp = super(CacheControlAdapter, self).send(request, **kw)
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/a                                                                                                             dapters.py", line 423, in send
    timeout=timeout
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/con                                                                                                             nectionpool.py", line 643, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/uti                                                                                                             l/retry.py", line 315, in increment
    total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
  • Third

使用 sudo apt-get install python-pip

pi@raspberrypi:~ $ sudo apt-get install python3-pip
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
python3-pip 已经是最新版 (9.0.1-2+rpt2)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 98 个软件包未被升                                                                                                             级。

没报错, 但依然无法安装

  • fourth

使用 sudo python -m pip install --upgrade --force pip
再次sudo pip安装,无果

  • last

感谢大神的分享
我才发觉这是一个新手的通用问题:

树莓派自带的Linux系统附带了一个来自前寒武纪的pip版本,
如果您不想花费数小时来调试与pip相关的问题,
那么就必须升级它。

甚至是Ubuntu系统亦是如此
使用如下代码:

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

可能有小问题, 别忘了sudo

pi@raspberrypi:~/Desktop $ python get-pip.py
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting pip
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))                                                                                                              after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection                                                                                                             .VerifiedHTTPSConnection object at 0x758bc4b0>: Failed to establish a new connec                                                                                                             tion: [Errno -3] \xe5\x9f\x9f\xe5\x90\x8d\xe8\xa7\xa3\xe6\x9e\x90\xe6\x9a\x82\xe                                                                                                             6\x97\xb6\xe5\xa4\xb1\xe8\xb4\xa5',)': /simple/pip/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x758bcf10>: Failed to establish a new connection: [Errno -3] \xe5\x9f\x9f\xe5\x90\x8d\xe8\xa7\xa3\xe6\x9e\x90\xe6\x9a\x82\xe6\x97\xb6\xe5\xa4\xb1\xe8\xb4\xa5',)': /simple/pip/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x758bced0>: Failed to establish a new connection: [Errno -3] \xe5\x9f\x9f\xe5\x90\x8d\xe8\xa7\xa3\xe6\x9e\x90\xe6\x9a\x82\xe6\x97\xb6\xe5\xa4\xb1\xe8\xb4\xa5',)': /simple/pip/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x758bc570>: Failed to establish a new connection: [Errno -3] \xe5\x9f\x9f\xe5\x90\x8d\xe8\xa7\xa3\xe6\x9e\x90\xe6\x9a\x82\xe6\x97\xb6\xe5\xa4\xb1\xe8\xb4\xa5',)': /simple/pip/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x758bc030>: Failed to establish a new connection: [Errno -3] \xe5\x9f\x9f\xe5\x90\x8d\xe8\xa7\xa3\xe6\x9e\x90\xe6\x9a\x82\xe6\x97\xb6\xe5\xa4\xb1\xe8\xb4\xa5',)': /simple/pip/
  Downloading https://www.piwheels.org/simple/pip/pip-18.0-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 336kB/s
Installing collected packages: pip
Could not install packages due to an EnvironmentError: [Errno 13] 权限不够: '/usr/local/lib/python2.7/dist-packages/pip'
Consider using the `--user` option or check the permissions.
pi@raspberrypi:~/Desktop $ sudo python get-pip.py
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting pip
  Downloading https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 19kB/s
Installing collected packages: pip
Successfully installed pip-18.0

最后再通过之前的whl文件安装Django, 就OK了, 当然 sudo pip install django 亦可

pi@raspberrypi:~/Desktop $ sudo pip3 install Django-2.1.2-py3-none-any.whl
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Processing ./Django-2.1.2-py3-none-any.whl
Collecting pytz (from Django==2.1.2)
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))                                                                                                              after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection                                                                                                             .VerifiedHTTPSConnection object at 0x757b5610>: Failed to establish a new connec                                                                                                             tion: [Errno -3] 域名解析暂时失败',)': /simple/pytz/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None))                                                                                                              after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection                                                                                                             .VerifiedHTTPSConnection object at 0x757b5510>: Failed to establish a new connec                                                                                                             tion: [Errno -3] 域名解析暂时失败',)': /simple/pytz/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None))                                                                                                              after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection                                                                                                             .VerifiedHTTPSConnection object at 0x757b5af0>: Failed to establish a new connec                                                                                                             tion: [Errno -3] 域名解析暂时失败',)': /simple/pytz/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None))                                                                                                              after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection                                                                                                             .VerifiedHTTPSConnection object at 0x757b59b0>: Failed to establish a new connec                                                                                                             tion: [Errno -3] 域名解析暂时失败',)': /simple/pytz/
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))                                                                                                              after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection                                                                                                             .VerifiedHTTPSConnection object at 0x75764550>: Failed to establish a new connec                                                                                                             tion: [Errno -3] 域名解析暂时失败',)': /packages/30/4e/27c34b62430286c6d59177a08                                                                                                             42ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl
  Downloading https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d591                                                                                                             77a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB                                                                                                             )
    100% |████████████████████████████████| 512kB 33kB/s
Installing collected packages: pytz, Django
Successfully installed Django-2.1.2 pytz-2018.5

大功告成!如果之后我在搭建服务器时遇到问题, 我依旧会分享, 同志们晚安!

阅读 4.5k

此号作废!

355 声望
12 粉丝
0 条评论
你知道吗?

此号作废!

355 声望
12 粉丝
宣传栏