confused
confused

Reputation: 1323

Get rid of urllib not defined error

Keep getting the same thing every time I try to run the program, urllib is not defined. How do I get rid of it?

from urllib.request import urlopen
from urllib.error import HTTPError

aname = 'http://website.com'
try:
    htm = urlopen(aname + '/').read()
except urllib.error.HTTPError as e:
    print(e)

Yes, I do have another problem I sure wish I could figure out as well, I can't get bs4 to install correctly, it keep trying to install to Python 2.7, which I don't have any 2.7 interpreter installed, only 3.4.3 is installed on the computer. I have a strange feeling that might be causing me some other issues as well with some some other programs.

Upvotes: 0

Views: 1306

Answers (1)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160657

Just because you have the line from urllib.error import HTTPError or the from urllib.request import urlopen doesn't mean that urrlib is available as a name in your script.

This is specified in the documentation for the import statement specifically in the section dealing with the from form; see the examples there to see what becomes available and what doesn't.

Therefore, when you except:

except urllib.error.HTTPError as e:

it fails when trying to find the name urrlib. Either just use HTTPError, as imported and bound to your namespace, in the except clause:

except HTTPError as e:

or, if you need the name for urllib available in your namespace, import urllib instead of from urllib.error import HTTPError and use the original except clause.

As for the installation issue, try using pip3 instead of pip.

Upvotes: 1

Related Questions