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

9

16

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:

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?

Seal answered 12/12, 2014 at 8:59 Comment(0)
C
17

There is a difference between the standard urllib and urllib2 and the third-party urllib3.

It looks like twill does not install the dependencies so you have to do it yourself. Twill depends on requests library which comes with and uses urllib3 behind the scenes. You also need lxml and cssselect libraries.

You can install them on terminal as follows:

pip install requests

pip install lxml

and

pip install cssselect

Controller answered 12/12, 2014 at 9:30 Comment(3)
If you are installing generally you may need to switch to root or prefix each of the above commands with sudo and type in your password after the first.Nereus
Yes, I've already installed these packages. First I installed python-pip, then using pip, I installed the other packages. There's no change in the error message. I even re-installed Python, but no luck.Seal
I followed these steps having the same error, I needed to use sudo pip install --upgrade each time to make it work.Hubbs
K
24

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:

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

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

Kopple answered 28/7, 2016 at 15:22 Comment(3)
in setup add requires=['requests']Unmuzzle
Wouldn't that find the old version of requests, dependencies are now met, and we get the failure the OP has observed?Kopple
install_requires=['requests>=2.9']Kopple
C
17

There is a difference between the standard urllib and urllib2 and the third-party urllib3.

It looks like twill does not install the dependencies so you have to do it yourself. Twill depends on requests library which comes with and uses urllib3 behind the scenes. You also need lxml and cssselect libraries.

You can install them on terminal as follows:

pip install requests

pip install lxml

and

pip install cssselect

Controller answered 12/12, 2014 at 9:30 Comment(3)
If you are installing generally you may need to switch to root or prefix each of the above commands with sudo and type in your password after the first.Nereus
Yes, I've already installed these packages. First I installed python-pip, then using pip, I installed the other packages. There's no change in the error message. I even re-installed Python, but no luck.Seal
I followed these steps having the same error, I needed to use sudo pip install --upgrade each time to make it work.Hubbs
P
9

python3

#note that requests.packages.urllib3 is just an alias for urllib3
from urllib3 import disable_warnings
from urllib3.exceptions import InsecureRequestWarning
disable_warnings(InsecureRequestWarning)
Poltroonery answered 4/4, 2019 at 3:18 Comment(1)
While this might answer the question, you should edit your answer to include some explanation for why this solves the issue in the question. This makes it more valuable to those who come across the same issue later onLollard
P
5

If you are having a RHEL based flavour, then:

yum install -y python-requests

Debian/Ubuntu based flavour:

apt-get install -y python-requests

Arch Linux based flavour:

pacman -S python-requests

Profanatory answered 16/9, 2016 at 14:0 Comment(0)
A
4

Problem solved by:

pip install --upgrade urllib3==1.19.1
pip install --upgrade requests
Anny answered 2/1, 2019 at 9:1 Comment(1)
Yeap, this fixes it!Collie
M
0

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, i.e. run the script as

python3 <script>.py

not

python <script>.py
Mien answered 9/4, 2021 at 16:50 Comment(0)
C
0

The problem here is depending on something implicitly by way of a sub-dependency. This style is tantamount to referring to a class's dunder-methods since a dependency's own dependencies may change (e.g say if requests stops using/exposing urllib3).

You can avoid this issue by being explicit with your dependencies, and expressing them (i.e urllib3) as a dependency in your requirements.txt/pyproject.toml file.

Cadmarr answered 26/5, 2023 at 18:45 Comment(0)
S
0

This is possible due to requests version that you use, have different version of urlib3 then the one you are using in environment.

Slink answered 22/3 at 17:1 Comment(0)
B
0

I found solution here https://medium.com/@rajputgajanan50/cannot-import-requests-packages-urllib3-util-retry-7351c03abd3a.

from urllib3.util import Retry

Beanstalk answered 29/7 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.