David Faizulaev
David Faizulaev

Reputation: 5721

Python - installing urllib

I'm trying to use urllib (for backward compatibility), but I always get an error when running the script. I tried installing urllib but got this error

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

Python version - 2.7.16

this is the import part in the script which fails:

from urllib import request, parse
from urllib import error as urllib_error

import error: ImportError: cannot import name request

Please advise.

Upvotes: 0

Views: 2392

Answers (4)

David Faizulaev
David Faizulaev

Reputation: 5721

The issue was that my environment was configured to use Python2 and not Python3, once I resolved this, the import worked as expected.

Upvotes: 0

iqmaker
iqmaker

Reputation: 2262

There is no urllib.request module in Python 2, that module only exists in Python 3, You can use urllib2 and Request:

from urllib2 import Request

Upvotes: 0

Nanthakumar J J
Nanthakumar J J

Reputation: 911

You should import like this: Download latest version of python from here

import urllib.request,urllib.parse, urllib.error

Upvotes: 1

vbn
vbn

Reputation: 274

From the 2.7 documentation :

The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.

So if you are trying to import urllib.request, parse and error separately - that constructs are available in Python 3.x version.

Please go through the documentation for 2.7 at : https://docs.python.org/2.7/library/urllib.html

Or upgrade to latest verion of Python 3.x to import the way you have described in your post.

Upvotes: 2

Related Questions