0

I am trying using urllib but it shows error, version is Python 2, i also tried using urllib2 and urllib.request.open().

import urllib
import sys

print (sys.version)

myurl = urllib.urlopen('http://www.profmcmillan.com')
print (myurl)

output:

2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]
..
AttributeError: 'module' object has no attribute 'urlopen'

with urllib2

import urllib2
import sys

print (sys.version)

myurl = urllib2.urlopen('http://www.profmcmillan.com')
print (myurl)

output:

2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]
..
    myurl = urllib2.urlopen('http://www.profmcmillan.com')
AttributeError: 'module' object has no attribute 'urlopen'

same if I import urllib and use urllib.request.open

laike9m
  • 18,344
  • 20
  • 107
  • 140
garg10may
  • 5,794
  • 11
  • 50
  • 91

1 Answers1

3

It may be that you named your file urllib.py or you have such a file in your python path. So with:

import urllib

it doesn't import the real urllib library but one of your files. You can check the module file path with:

import urllib
print urllib.__file__

For example mine refers to: /usr/lib/python2.7/urllib.pyc

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115