0
import urllib2
response=urllib2.urlopen("http://yts.ag")
print response.info()
response.close()

The above program shows the following error in python 2.7.

Traceback (most recent call last):
  File "url_test.py", line 1, in <module>
    import urllib2
  File "C:\Python27\lib\urllib2.py", line 111, in <module>
    from urllib import (unwrap, unquote, splittype, splithost, quote,
  File "D:\Python\Python Test codes\urllib.py", line 4, in <module>
    "Names and Addresses, URIs, URLs, URNs, URCs", at
AttributeError: 'module' object has no attribute 'urlopen'

I tried replacing the library file but did not help.

cs95
  • 379,657
  • 97
  • 704
  • 746
A3L2J
  • 17
  • 4
  • 1
    Editing the question to become completely different defeats the purpose of answering it and makes this thread useless to future readers. Open a new question if you have to. I did a rollback to your original question. If you have further queries, first close this question and post a new one. – cs95 Jun 20 '17 at 06:10

2 Answers2

1

Judging from your traceback, it shows that one of your file in your cwd is called urllib.py, you can’t call it that because urllib2 needs to import a module that is also named urllib. It’s currently trying to import your file located D:\Python\Python Test codes\urllib.py.

To fix the error, you just need to rename that file.

Note: you should never mess around changing the library’s source code. It will mess the library up.

Taku
  • 31,927
  • 11
  • 74
  • 85
  • The above error is shown now – A3L2J Jun 20 '17 at 06:04
  • 1
    @A3L2J Can you really expect to have people solve all your issues? The main subject of your question has been answered. Close this and open a new one. – cs95 Jun 20 '17 at 06:09
  • Please do not post another question in place of your existing one, ask a new question. But, I think it’s pretty self explanatory, you cannot access the webpage with the current setup as the website refuse to connect, you can try using `https://yts.ag` or try setting your own user agent – Taku Jun 20 '17 at 06:10
1

Look at these 2 lines in your traceback:

 File "C:\Python27\lib\urllib2.py", line 111, in <module>
    from urllib import (unwrap, unquote, splittype, splithost, quote,

 File "D:\Python\Python Test codes\urllib.py", line 4, in <module>
    "Names and Addresses, URIs, URLs, URNs, URCs", at

It's a clear naming conflict between one of your files and one of the inbuilt modules urllib2 calls. It's confusing your urllib.py with its inbuilt one. All you need to do is rename your file, and it should work.

Note: Never name your files similar to to the libraries you use, otherwise you'll run into some "unexpected" errors.

Krishh
  • 602
  • 1
  • 8
  • 25