3

When I try to import the requests module:

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

I get this error:

Traceback (most recent call last):
  File "C:\Users\gek0n\Desktop\requests.py", line 1, in <module>
    import requests
  File "C:\Users\gek0n\Desktop\requests.py", line 3, in <module>
    r = requests.get('https://github.com/timeline.json')
AttributeError: 'module' object has no attribute 'get'

Than I checked Stack Overflow and found this:

print dir(requests)

And instead of this:

['ConnectionError', 'HTTPError', 'Request', 'RequestException', 'Response', 'Session', 'Timeout', 'TooManyRedirects', 'URLRequired', '__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__', '_oauth', 'api', 'auth', 'certs', 'codes', 'compat', 'cookies', 'defaults', 'delete', 'exceptions', 'get', 'head', 'hooks', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'safe_mode', 'session', 'sessions', 'status_codes', 'structures', 'utils']

I had this:

['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'requests']
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'requests']

Yes! There are two strings and only six elements. Why is that? What's wrong and how can I fix this?

1
  • 3
    Did you create your own file called requests.py? Judging from the path it looks like you did. Name your file something else. Commented Mar 14, 2014 at 18:57

1 Answer 1

4

You named your script requests.py and it is being imported instead of the library.

Because your import requests line is the first in the module, that is also the only object (apart from the standard double-underscore values) found in your module at the time it imports itself again, which is why you see 'requests' listed in the dir() output.

Rename your script.

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

Comments

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.