Forums

urllib2: Successful install but module not found?

Hello -

I have always used urllib2 to get info from other websites. I noticed it wasn't installed, so I went into the bash console and installed it:

17:11 ~ $ pip install --user urllib2
Collecting urllib2
  Using cached urllib2-1498656401.94-py2-none-any.whl
Installing collected packages: urllib2
Successfully installed urllib2-1498656401.94

Great! Now, I go to run my code with urllib2:

import urllib2
from urllib2 import Request, urlopen

Traceback (most recent call last):
  File "/path/to/python/script.py", line 1, in <module>
import urllib2
  ImportError: No module named 'urllib2'

So, I try installing different versions of urllib2, and all to no avail. Why is it that the script I'm running is not recognizing the module even after I installed it?

Also, maybe urllib2 could be part of the "batteries included"?

I'm guessing you didn't install it for the same version of python as you're running it with? try eg pip3.6 install --user urrlib2?

My version of python is 2.7.6. When I type in pip2.7 install --user urllib2, I get the following error:

00:14 ~ $ pip2.7 install --user urllib2
Traceback (most recent call last):
  File "/usr/local/bin/pip2.7", line 7, in <module>
    from pip import main
  File "/usr/local/lib/python2.7/dist-packages/pip/__init__.py", line 16, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/usr/local/lib/python2.7/dist-packages/pip/vcs/mercurial.py", line 9, in <module>
    from pip.download import path_to_url
  File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 39, in <module>
    from pip._vendor import requests, six
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/__init__.py", line 64, in <module>
    from . import utils
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/utils.py", line 24, in <module>
    from .compat import parse_http_list as _parse_list_header
  File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/compat.py", line 38, in <module>
    from urllib2 import parse_http_list
ImportError: cannot import name parse_http_list

wait. urrlib2 is a standard library module in Python, you shouldn't need to pip install it at all.

I think the attempts to do so may have caused an incorrect version of urrlib2 to get installed in the --user location. first try removing it like this:

pip uninstall urllib2

then just try using urllib2 in your python code as normal

or better, use requests which is a much nicer way of doing HTTP stuff with python. that's also installed by default.

I get the same error when I try pip uninstall urllib2. Also tried doing pip uninstall --user urllib2.

ok let's use the nuclear option then:

rm -rf ~/.local/*

(that will remove everything you've installed with --user)

Hah, wow. Luckily I don't have much installed...

I did that, then typed in pip2.7 install --user urllib2. I run my script and I get the same error (no module name urllib2). I guess what I'll do is look into the requests module and use that instead (or learn how to use pandas, which has been on the back burner for quite some time.).

I'm sorry, I guess I didn't make that clear. You do not need to install urllib2. It's part of the default Python2.7 standard library: https://docs.python.org/2/library/urllib2.html

Hence why I posted here in the first place - I thought it was a standard package. When it wasn't importing, I tried installing.

Also, now I've run into another issue where I can't install any packages at all using pip... Ever since I used rm -rf ~/.local/*, I haven't been able to install any packages. I tried re-installing geopandas, only to get this error: ImportError: cannot import name parse_http_list, and this holds true for every package that I need installed.

Whatever it is that gets installed when you pip install urllib2 breaks pip. Remove your .local directory again and don't pip install urllib2.