Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to install urllib2 for Python 3.5.1

I'm running Python 3.5.1 for Mac. I want to use urllib2 module. I tried installing it but I was told that it's been split into urllib.request and urllib.error for Python 3.

My command (running from the framework bin directory for now because it's not in my path):

sudo ./pip3 install urllib.request

Returns this:

Could not find a version that satisfies the requirement urllib.request (from versions: )
No matching distribution found for urllib.request

I got the same error before when I tried to install urllib2 in one fell swoop.

like image 362
Eamonn Gormley Avatar asked Oct 12 '22 00:10

Eamonn Gormley


2 Answers

WARNING: Security researches have found several poisoned packages on PyPI, including a package named urllib, which will 'phone home' when installed. If you used pip install urllib some time after June 2017, remove that package as soon as possible.

You can't, and you don't need to.

urllib2 is the name of the library included in Python 2. You can use the urllib.request library included with Python 3, instead. The urllib.request library works the same way urllib2 works in Python 2. Because it is already included you don't need to install it.

If you are following a tutorial that tells you to use urllib2 then you'll find you'll run into more issues. Your tutorial was written for Python 2, not Python 3. Find a different tutorial, or install Python 2.7 and continue your tutorial on that version. You'll find urllib2 comes with that version.

Alternatively, install the requests library for a higher-level and easier to use API. It'll work on both Python 2 and 3.

like image 204
Martijn Pieters Avatar answered Oct 13 '22 13:10

Martijn Pieters


In Python 3, urllib2 was replaced by two in-built modules named urllib.request and urllib.error

Adapted from source


So replace this:

import urllib2

With this:

import urllib.request as urllib2
like image 44
Aryan Beezadhur Avatar answered Oct 13 '22 13:10

Aryan Beezadhur