• modules
  • python
  • exceptions
  • shadowing

I have a script named requests.py which has the following code:

import requests

res = requests.get('http://www.google.ca')

When I run this code, I receive the following error:

Traceback (most recent call last):
  File "/Users/me/dev/rough/requests.py", line 1, in <module>
    import requests
  File "/Users/me/dev/rough/requests.py", line 3, in <module>
AttributeError: module 'requests' has no attribute 'get'

Why is this happening?

The reason why this is happening is because your module that you named requests.py is now shadowing the intended requests module you are trying to use.

To avoid this, you should rename your module to something else to avoid these situations. Furthermore, chances are you will have generated a requests.pyc file as well, local to where your requests.py resides. Make sure you remove that as well after your rename, as the interpreter will still reference that file, re-producing the error.

Example of problem resolution:

renaming file to req.py, removing requests.pyc and running again, results in:

<Response [200]>