Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python requests import error

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?

like image 967
murzagurskiy Avatar asked Nov 19 '25 01:11

murzagurskiy


1 Answers

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.

like image 81
Martijn Pieters Avatar answered Nov 21 '25 15:11

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!