import requests 出错,搜了很多,没解决,求教.

2018-04-21 20:04:18 +08:00
 xxbing

requests 依赖的库都已经安装了.

Requirement not upgraded as not directly required: chardet<3.1.0,>=3.0.2 in d:\p
ython27\lib\site-packages (from requests) (3.0.4)
Requirement not upgraded as not directly required: certifi>=2017.4.17 in d:\pyth
on27\lib\site-packages (from requests) (2018.4.16)
Requirement not upgraded as not directly required: urllib3<1.23,>=1.21.1 in d:\p
ython27\lib\site-packages (from requests) (1.22)
Requirement not upgraded as not directly required: idna<2.7,>=2.5 in d:\python27
\lib\site-packages (from requests) (2.5)
pip install requests -U

提示

Successfully installed requests-2.18.4

pip 版本

pip 10.0.1 from d:\python27\lib\site-packages\pip (python 2.7)

然后我在 python 执行 import requests 报错:

>>> import requests
D:\Python27\lib\site-packages\requests\__init__.py:80: RequestsDependencyWarning
: urllib3 (1.9.1) or chardet (3.0.4) doesn't match a supported version!
  RequestsDependencyWarning)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\site-packages\requests\__init__.py", line 90, in <module
>
    from urllib3.exceptions import DependencyWarning
ImportError: cannot import name DependencyWarning
8816 次点击
所在节点    Python
10 条回复
zeyexe
2018-04-21 20:08:56 +08:00
是不是用了 TUNA 的 index,最近他们家的好像有问题。我之前用 TUNA 的镜像遇到 urllib3 的问题,换阿里云的好了。
xxbing
2018-04-21 20:15:13 +08:00
@zeyexe 没用.我删除了 4 个依赖库+requests 库.再使用阿里云的源重装也一样.
AgentK
2018-04-21 21:16:53 +08:00
所以为什么要用 virtualenv
Hstar
2018-04-21 21:30:11 +08:00
试着执行下 from urllib3.exceptions import DependencyWarning
然后试下能否 import 其他库, 比如一些内建库, 然后装个其他库试试是否是普遍问题
然后检查下当前 path: import sys; print(sys.path)
以前遇到不能 import 基本都是路径问题
blless
2018-04-21 21:57:55 +08:00
文件夹里面可能有 http.py net.py 之类的文件?
xxbing
2018-04-21 22:14:25 +08:00
@blless 没有.
xxbing
2018-04-21 22:16:48 +08:00
@Hstar
```
from urllib3.exceptions import DependencyWarning
```
```
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name DependencyWarning
```
```
import sys,json,urllib3,MySQLdb
```
都没有错误.

```
import sys; print(sys.path)
```
返回
```
['', 'C:\\Windows\\system32\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\
\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27
', 'D:\\Python27\\lib\\site-packages']
```
Kilerd
2018-04-21 22:26:14 +08:00
Requirement not upgraded as not directly required: urllib3<1.23,>=1.21.1 in d:\p
ython27\lib\site-packages (from requests) (1.22)

首先 requests 里面要求 urllib3 要符合这个条件<1.23,>=1.21.1



D:\Python27\lib\site-packages\requests\__init__.py:80: RequestsDependencyWarning
: urllib3 (1.9.1) or chardet (3.0.4) doesn't match a supported version!
你的报错信息是这个。证明安装的 urllib3 是 1.9.1 版本。

所以出错啊。

解决方法:
1 用官方源重装试试。先 pip uninstall requests urllib3

2 手动升级 urllib3

3 试下用 virtualenv / pipenv / pyenv 隔离 py 环境


其实这个问题主要的发生场景是 有一个 package 依赖于 urllib3 较早版本,然后你再安装 requests 的时候会导致依赖冲突,就只有一个 package 可以用了。
xxbing
2018-04-21 23:04:56 +08:00
@Kilerd 感谢,已经解决了.
xxbing
2018-04-21 23:07:48 +08:00
@Kilerd 问题的原因在于 D:\Python27\Lib 下有个 urllib3 文件夹 里面是 1.9 的版本.
新版 urllib3-1.22 在 D:\Python27\Lib\site-packages 下
可能是 requests 在搜索依赖包 urllib3 时,优先找到了 Lib 下的.
我删除 1.9 的版本 ,就修复了.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/448752

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX