5

I'm trying this simple code:

import requests
print requests.__file__
r = requests.get('https://github.com/timeline.json')

It works flawlessly on the command line when I type the lines one by one, but not whenen when I execute it as a script or in Sublime Text 2. Here's the stack trace:

C:\Python27\lib\site-packages\requests\__init__.pyc
Traceback (most recent call last):
  File "C:\Users\Bruce\Desktop\http.py", line 1, in <module>
    import requests
  File "C:\Python27\lib\site-packages\requests\__init__.py", line 53, in <module>
    from requests.packages.urllib3.contrib import pyopenssl
  File "C:\Python27\lib\site-packages\requests\packages\__init__.py", line 3, in <module>
    from . import urllib3
  File "C:\Python27\lib\site-packages\requests\packages\urllib3\__init__.py", line 16, in <module>
    from .connectionpool import (
  File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 15, in <module>
    from http.client import HTTPConnection, HTTPException
  File "C:\Users\Bruce\Desktop\http.py", line 3, in <module>
    r = requests.get('https://github.com/timeline.json')
AttributeError: 'module' object has no attribute 'get'
[Finished in 0.2s with exit code 1]

Answers on 'Module object has no attribute 'get' Python error Requests? didn't help much.

Could this be some error in my ST2 Python build system? I tried removing all requests modules in case there were multiples of them by using pip and reinstalled them.

7
  • 1
    Do you've a requests.py file in the Desktop folder? Commented Jul 8, 2013 at 13:45
  • @AshwiniChaudhary No! Commented Jul 8, 2013 at 14:01
  • Could you double check and do print requests.__file__ please... Commented Jul 8, 2013 at 14:04
  • @Bruce okay - and then a print dir(requests)? Commented Jul 8, 2013 at 14:09
  • @JonClements If the script has just one line print dir(requests) after import requests, I'm getting two arrays as output - one having all attributes with underscores, and the other which has the get method. I guess the script is running twice? pastebin.com/WcBa7Qr5 Commented Jul 8, 2013 at 14:16

1 Answer 1

9

Edit After reading the stacktrace again, you can see that urllib3 tries to import something from the http module. Your file is called http.py and is thus imported instead of the expected one.

The actual error happens because of the circular nature of the import. Since requests hasn't finished importing completely yet. The get function in requests isn't defined yet when the http import reaches import requests again.

Note: You will also want to always guard your entry point with the if __name__ == '__main__' construct. This will often avoid nasty errors for unsuspecting future developers (including yourself).

Sign up to request clarification or add additional context in comments.

3 Comments

@Bruce ah sorry, I was close though. I just noticed that urllib3 does an from http.client import HTTPConnection, HTTPException. It seems your http.py file is shadowing that instead? Try renaming it.
By the way, my point still stands. Always guard your entry point!
My error was 'module' object has no attribute 'post' and my filename was email.py. Renaming the file fixed it! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.