在 Python 2.7 中导入“urllib3.util”失败?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23361432/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:50:06  来源:igfitidea点击:

Import of 'urllib3.util' failing in Python 2.7?

pythonpython-2.7

提问by Mr B

I'm working on a Python script written by someone else. I'm trying to get it to run without any problems on my local development machine.

我正在编写由其他人编写的 Python 脚本。我试图让它在我的本地开发机器上没有任何问题地运行。

I've installed the modules required by the script (requests, urllib3 and oath2), however I'm getting the following error which I'm struggling to resolve;

我已经安装了脚本所需的模块(请求、urllib3 和 oath2),但是我遇到了以下我正在努力解决的错误;

Traceback (most recent call last):
  File "/home/saeed/ps4/scrape/run.py", line 2, in <module>
    import get_data as gd, time
  File "/home/saeed/ps4/scrape/get_data.py", line 8, in <module>
    import sys, oauth2, requests, json
  File "/usr/local/lib/python2.7/dist-packages/requests/__init__.py", line 58, in <module>
    from . import utils
  File "/usr/local/lib/python2.7/dist-packages/requests/utils.py", line 25, in <module>
    from .compat import parse_http_list as _parse_list_header
  File "/usr/local/lib/python2.7/dist-packages/requests/compat.py", line 7, in <module>
    from .packages import chardet
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/__init__.py", line 3, in <module>
    from . import urllib3
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/__init__.py", line 16, in <module>
    from .connectionpool import (
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py", line 36, in <module>
    from .connection import (
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connection.py", line 43, in <module>
    from .util import (
ImportError: No module named util

The scripts consist of three files; run.py, get_data.py and incr.py. The import statement in run.py is:

脚本由三个文件组成;run.py、get_data.py 和 incr.py。run.py 中的导入语句是:

import get_data as gd, time

In get_data.py:

在 get_data.py 中:

import sys, oauth2, requests, json

In incr.py:

在 incr.py 中:

import time

I assumed that I have to install a module named 'util'. I've searched for this module and cannot find it, therefore I think it seems like a deeper issue rather than just installing a module.

我假设我必须安装一个名为“util”的模块。我已经搜索了这个模块但找不到它,因此我认为这似乎是一个更深层次的问题,而不仅仅是安装一个模块。

I'd really appreciate it if anyone can point me in the right direction to resolve the issue. I am using Python 2.7.3.

如果有人能指出我解决问题的正确方向,我将不胜感激。我正在使用 Python 2.7.3。

采纳答案by jmetz

Broken install

安装损坏

If for some reason your install of urllib3 is failing to include the utilsubmodule, you could simply download the archive from the pypi page and copy the util folder from there to your urllib3 instal location.

如果由于某种原因您的 urllib3 安装未能包含util子模块,您只需从 pypi 页面下载存档并将 util 文件夹从那里复制到您的 urllib3 安装位置。

Outdated urllib3

过时的 urllib3

The error you've posted is saying that within urllib3the relative import of utilis failing.

您发布的错误是说在urllib3相对导入中util失败。

I checked the urllib3website, and most likely you have an old version of urllib3.

我查看了该urllib3网站,很可能您使用的是旧版本的urllib3.

From the changelog:

从变更日志:

1.8.2 (2014-04-17)

Fix urllib3.util not being included in the package.

1.8.2 (2014-04-17)

修复 urllib3.util 未包含在包中的问题。

Try updating the module with

尝试更新模块

sudo pip install urllib3 --upgrade

(or equivalent on your machine)

(或在您的机器上等效)

Alternative

选择

A second reason it could be failing is if you're trying to run the code from within the module. This is generally seen as dangerous and should be avoided.

它可能失败的第二个原因是,如果您试图从模块内运行代码。这通常被认为是危险的,应该避免。

Confirm which module you're loading

确认您正在加载哪个模块

See where your module is by starting a python interpreter and checking where the urllib3module is being loaded from;

通过启动 python 解释器并检查urllib3模块从哪里加载来查看你的模块在哪里;

python -c "import urllib3; print urllib3.__file__"

similarly you can check the version:

同样,您可以检查版本:

python -c "import urllib3; print urllib3.__version__"

Manual checkingYou could also check to make sure that the utilsubmodule is present in the correct location;

手动检查您还可以检查以确保util子模块位于正确的位置;

ls /usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util

回答by parkerproject

Use pip3 : pip3 install urllib3 --upgrade

使用 pip3 : pip3 install urllib3 --upgrade