6

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.

jmetz
  • 12,144
  • 3
  • 30
  • 41
Mr B
  • 3,980
  • 9
  • 48
  • 74

2 Answers2

16

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
jmetz
  • 12,144
  • 3
  • 30
  • 41
  • I just checked the version of urllib3 I installed and it is 1.8.2 yet I am still getting the error :S – Mr B Apr 29 '14 at 10:25
  • @Sid: Weird; the error you've posted is definitely a missing **relative import** in urllib3... perhaps try reinstalling `urllib3`? – jmetz Apr 29 '14 at 10:27
  • I've attempted the upgrade but still getting the same error. Thanks for your help so far. – Mr B Apr 29 '14 at 10:32
  • @Sid - Can you also confirm that you're not trying to run the code from within the module? – jmetz Apr 29 '14 at 10:33
  • Sorry, I'm not sure what you mean, (I'm new to Python). I have the Python script and importing modules using the import statement. – Mr B Apr 29 '14 at 10:35
  • Scrap my last question: your post already had the answer; can I confirm that you're not calling the script when your current directory is within a module? – jmetz Apr 29 '14 at 10:44
  • I'm not sure if the problem is with the urllib3 module. I just uninstalled the urllib3 module and ran my python script and it gave the same error; 'ImportError: No module named util' – Mr B Apr 29 '14 at 10:50
  • @Sid: Ohh in that case you probably have multiple versions of the urllib3 module - probably an old one with the above problem, and your newer one which isn't being loaded anyway. – jmetz Apr 29 '14 at 10:52
  • @Sid: Use the command I posted in my answer under "Confirm which module you're loading" to test which urllib3 you're loading. – jmetz Apr 29 '14 at 10:55
  • It seems to be loaded from the correct directory and the version is also the lastest one, 1.8.2. However when I did the manual check, util.py is not in the /usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/ directory – Mr B Apr 29 '14 at 11:09
  • @Sid: I guess that's your problem then! – jmetz Apr 29 '14 at 11:11
  • I uninstalled urllib3 by running 'sudo pip uninstall urllib3'. I then downloaded the module from https://pypi.python.org/pypi/urllib3, uncompressed it (ensured it had a util folder with files). I then ran 'sudo python setup.py install'. Seems to have installed successfully but I'm getting the same error when running my script. Maybe I missed out a step during installation? – Mr B Apr 29 '14 at 11:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51679/discussion-between-jmetz-and-sid) – jmetz Apr 29 '14 at 11:59
0

Use pip3 : pip3 install urllib3 --upgrade

parkerproject
  • 2,138
  • 1
  • 17
  • 14