Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

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

In get_data.py:

import sys, oauth2, requests, json

In 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.

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.

like image 569
Mr B Avatar asked Apr 29 '14 09:04

Mr B


People also ask

How do I import from urllib3 to Python?

Type "cmd" in the search bar and hit Enter to open the command line. What is this? Type “ pip install urllib3 ” (without quotes) in the command line and hit Enter again. This installs urllib3 for your default Python installation.

How do I use urllib3 in Python?

Python urllib3 send JSON The example sends JSON data. We encode the JSON data into binary format. We specify the Content-Type header in the request. We decode the returned data back to text and print it to the console.

How do I know if urllib3 is installed?

To check which version of urllib3 is installed, use pip show urllib3 or pip3 show urllib3 in your Windows CMD, command line, or PowerShell.

Is urllib3 a standard Python library?

The Python 3 standard library has a new urllib which is a merged/refactored/rewritten version of the older modules. urllib3 is a third-party package (i.e., not in CPython's standard library).


1 Answers

Broken install

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.

Outdated urllib3

The error you've posted is saying that within urllib3 the relative import of util is failing.

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

From the changelog:

1.8.2 (2014-04-17)

Fix urllib3.util not being included in the package.

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 urllib3 module is being loaded from;

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

similarly you can check the version:

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

Manual checking You 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
like image 97
jmetz Avatar answered Oct 22 '22 13:10

jmetz