问题描述
我正在编写由其他人编写的 Python 脚本.我正在尝试让它在我的本地开发机器上正常运行.
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.
我已经安装了脚本所需的模块(请求、urllib3 和 oath2),但是我遇到了以下我正在努力解决的错误;
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;
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
脚本由三个文件组成;run.py、get_data.py 和 incr.py.run.py中的import语句为:
The scripts consist of three files; run.py, get_data.py and incr.py. The import statement in run.py is:
import get_data as gd, time
在 get_data.py 中:
In get_data.py:
import sys, oauth2, requests, json
在 incr.py 中:
In incr.py:
import time
我假设我必须安装一个名为util"的模块.我已经搜索了这个模块但找不到它,因此我认为这似乎是一个更深层次的问题,而不仅仅是安装一个模块.
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.
如果有人能指出我解决问题的正确方向,我将不胜感激.我使用的是 Python 2.7.3.
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.
推荐答案
安装失败
如果由于某种原因您的 urllib3 安装未能包含 util
子模块,您可以简单地从 pypi 页面下载存档并将 util 文件夹从那里复制到您的 urllib3 安装位置.
If for some reason your install of urllib3 is failing to include the util
submodule, you could simply download the archive from the pypi page and copy the util folder from there to your urllib3 instal location.
过时的 urllib3
您发布的错误是说在 urllib3
中 util
的相对导入失败.
The error you've posted is saying that within urllib3
the relative import of util
is failing.
我查看了 urllib3
网站,很可能您使用的是旧版本的 urllib3
.
I checked the urllib3
website, and most likely you have an old version of urllib3
.
来自变更日志:
1.8.2 (2014-04-17)
修复 urllib3.util 未包含在包中的问题.
Fix urllib3.util not being included in the package.
尝试使用
sudo pip install urllib3 --upgrade
(或您机器上的等价物)
(or equivalent on your machine)
替代方案
它可能失败的第二个原因是,如果您试图从模块内运行代码.这通常被认为是危险的,应该避免.
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.
确认您正在加载哪个模块
通过启动 python 解释器并检查 urllib3
模块从哪里加载来查看你的模块在哪里;
See where your module is by starting a python interpreter and checking where the urllib3
module is being loaded from;
python -c "import urllib3; print urllib3.__file__"
同样可以查看版本:
python -c "import urllib3; print urllib3.__version__"
手动检查您还可以检查以确保 util
子模块位于正确的位置;
Manual checkingYou could also check to make sure that the util
submodule is present in the correct location;
ls /usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util
这篇关于在 Python 2.7 中导入“urllib3.util"失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!