简体   繁体   English

如何修复ImportError:没有名为packages.urllib3 的模块?

[英]How to fix ImportError: No module named packages.urllib3?

I'm running Python 2.7.6 on an Ubuntu machine.我在 Ubuntu 机器上运行 Python 2.7.6。 When I run twill-sh (Twill is a browser used for testing websites) in my Terminal, I'm getting the following:当我在终端中运行twill-sh (Twill 是用于测试网站的浏览器)时,我得到以下信息:

Traceback (most recent call last):
  File "dep.py", line 2, in <module>
    import twill.commands
  File "/usr/local/lib/python2.7/dist-packages/twill/__init__.py", line 52, in <module>
    from shell import TwillCommandLoop
  File "/usr/local/lib/python2.7/dist-packages/twill/shell.py", line 9, in <module>
    from twill import commands, parse, __version__
  File "/usr/local/lib/python2.7/dist-packages/twill/commands.py", line 75, in <module>
    browser = TwillBrowser()
  File "/usr/local/lib/python2.7/dist-packages/twill/browser.py", line 31, in __init__
    from requests.packages.urllib3 import connectionpool as cpl
ImportError: No module named packages.urllib3

However, I can import urllib in Python console just fine.但是,我可以在 Python 控制台中导入 urllib 就好了。 What could be the reason?可能是什么原因?

If you already have 'requests' installed from a default build, you may have to如果您已经从默认版本安装了“请求”,则可能必须

sudo pip install --upgrade requests

Credit to @bkzland from comment on previous answer:归功于@bkzland 对先前答案的评论:

I followed these steps having the same error, I needed to use sudo pip install --upgrade each time to make it work.我按照这些步骤执行了相同的错误,我每次都需要使用 sudo pip install --upgrade 才能使其工作。 – bkzland Dec 17 '15 at 12:57 – bkzland 2015 年 12 月 17 日 12:57

---now, how do I make this a dependency in my setup.py? ---现在,我如何使它成为我的 setup.py 中的依赖项?

There is a difference between the standard urllib and urllib2 and the third-party urllib3 .标准urlliburllib2与第三方urllib3之间存在差异。

It looks like twill does not install the dependencies so you have to do it yourself.看起来 twill 没有安装依赖项,所以你必须自己做。 Twill depends on requests library which comes with and uses urllib3 behind the scenes. Twill 依赖于在幕后附带并使用urllib3 requests库。 You also need lxml and cssselect libraries.您还需要lxmlcssselect库。

You can install them on terminal as follows:您可以按如下方式在终端上安装它们:

pip install requests

pip install lxml

and

pip install cssselect

If you are having a RHEL based flavour, then:如果你有一个基于 RHEL 的风格,那么:

yum install -y python-requests

Debian/Ubuntu based flavour:基于 Debian/Ubuntu 的风格:

apt-get install -y python-requests

Arch Linux based flavour:基于 Arch Linux 的风格:

pacman -S python-requests

Problem solved by:通过以下方式解决的问题:

pip install --upgrade urllib3==1.19.1
pip install --upgrade requests

python3蟒蛇3

#note that requests.packages.urllib3 is just an alias for urllib3
from urllib3 import disable_warnings
from urllib3.exceptions import InsecureRequestWarning
disable_warnings(InsecureRequestWarning)

It should be pointed out, you can also get this error if you are making the rookie mistake I did, running a python 3 script with the "old" python command, ie run the script as应该指出的是,如果您犯了我犯过的菜鸟错误,使用“旧”python 命令运行 python 3 脚本,即运行脚本为

python3 <script>.py

not不是

python <script>.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM