Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top