V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
xxbing
V2EX  ›  Python

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

  •  
  •   xxbing · 2018-04-21 20:04:18 +08:00 · 8759 次点击
    这是一个创建于 2187 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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
    
    10 条回复    2018-04-21 23:07:48 +08:00
    zeyexe
        1
    zeyexe  
       2018-04-21 20:08:56 +08:00
    是不是用了 TUNA 的 index,最近他们家的好像有问题。我之前用 TUNA 的镜像遇到 urllib3 的问题,换阿里云的好了。
    xxbing
        2
    xxbing  
    OP
       2018-04-21 20:15:13 +08:00
    @zeyexe 没用.我删除了 4 个依赖库+requests 库.再使用阿里云的源重装也一样.
    AgentK
        3
    AgentK  
       2018-04-21 21:16:53 +08:00
    所以为什么要用 virtualenv
    Hstar
        4
    Hstar  
       2018-04-21 21:30:11 +08:00
    试着执行下 from urllib3.exceptions import DependencyWarning
    然后试下能否 import 其他库, 比如一些内建库, 然后装个其他库试试是否是普遍问题
    然后检查下当前 path: import sys; print(sys.path)
    以前遇到不能 import 基本都是路径问题
    blless
        5
    blless  
       2018-04-21 21:57:55 +08:00 via Android
    文件夹里面可能有 http.py net.py 之类的文件?
    xxbing
        6
    xxbing  
    OP
       2018-04-21 22:14:25 +08:00
    @blless 没有.
    xxbing
        7
    xxbing  
    OP
       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
        8
    Kilerd  
       2018-04-21 22:26:14 +08:00   ❤️ 1
    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
        9
    xxbing  
    OP
       2018-04-21 23:04:56 +08:00
    @Kilerd 感谢,已经解决了.
    xxbing
        10
    xxbing  
    OP
       2018-04-21 23:07:48 +08:00   ❤️ 1
    @Kilerd 问题的原因在于 D:\Python27\Lib 下有个 urllib3 文件夹 里面是 1.9 的版本.
    新版 urllib3-1.22 在 D:\Python27\Lib\site-packages 下
    可能是 requests 在搜索依赖包 urllib3 时,优先找到了 Lib 下的.
    我删除 1.9 的版本 ,就修复了.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1637 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 16:53 · PVG 00:53 · LAX 09:53 · JFK 12:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.