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

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

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

pythonurllib2urllib3twill

提问by Joseph John

I'm running Python 2.7.6 on an Ubuntu machine. When I run twill-sh(Twill is a browser used for testing websites) in my Terminal, I'm getting the following:

我在 Ubuntu 机器上运行 Python 2.7.6。当我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. What could be the reason?

但是,我可以在 Python 控制台中导入 urllib 就好了。可能是什么原因?

采纳答案by dopstar

There is a difference between the standard urlliband urllib2and the third-party urllib3.

标准urlliburllib2和第三方之间存在差异urllib3

It looks like twill does not install the dependencies so you have to do it yourself. Twill depends on requestslibrary which comes with and uses urllib3behind the scenes. You also need lxmland cssselectlibraries.

看起来 twill 没有安装依赖项,所以你必须自己做。斜纹取决于幕后requests附带和使用的库urllib3。您还需要lxmlcssselect库。

You can install them on terminal as follows:

您可以按如下方式在终端上安装它们:

pip install requests

pip install requests

pip install lxml

pip install lxml

and

pip install cssselect

pip install cssselect

回答by FlipMcF

If you already have 'requests' installed from a default build, you may have to

如果您已经从默认版本安装了“请求”,则可能必须

sudo pip install --upgrade requests

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. – bkzland Dec 17 '15 at 12:57

我按照这些步骤执行了相同的错误,我每次都需要使用 sudo pip install --upgrade 才能使其工作。– bkzland 2015 年 12 月 17 日 12:57

---now, how do I make this a dependency in my setup.py?

---现在,我如何使它成为我的 setup.py 中的依赖项?

回答by Vishnu Kumar

If you are having a RHEL based flavour, then:

如果你有一个基于 RHEL 的风格,那么:

yum install -y python-requests

yum install -y python-requests

Debian/Ubuntu based flavour:

基于 Debian/Ubuntu 的风格:

apt-get install -y python-requests

apt-get install -y python-requests

Arch Linux based flavour:

基于 Arch Linux 的风格:

pacman -S python-requests

pacman -S python-requests

回答by ak3191

Problem solved by:

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

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

回答by Ahmed Soliman

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)