Module 'requests' has no attribute 'get' or 'post' [Fixed]

avatar
Borislav Hadzhiev

Last updated: Jan 28, 2023
5 min

banner

# Table of Contents

  1. AttributeError module 'requests' has no attribute 'get'
  2. AttributeError module 'requests' has no attribute 'post'

# AttributeError module 'requests' has no attribute 'get'

The Python "AttributeError module 'requests' has no attribute 'get'" occurs when we have a local file named requests.py and try to import from the requests module.

To solve the error, make sure to rename any local files named requests.py.

attributeerror module requests has no attribute get

Here is an example of how the error occurs in a file called requests.py.

requests.py
import requests def make_request(): # ⛔️ AttributeError: module 'requests' has no attribute 'get' res = requests.get('https://reqres.in/api/users', timeout=10) parsed = res.json() print(parsed) make_request()

The most likely cause of the error is having a local file named requests.py which shadows the official requests module.

# Make sure you haven't named your file requests.py

Make sure to rename your local file to something other than requests.py to solve the error.

main.py
import requests def make_request(): res = requests.get( 'https://reqres.in/api/users', timeout=10 ) parsed = res.json() print(parsed) # ✅ Works make_request()

make sure to not name your file requests py

You can rename your file to main.py or any other name that doesn't clash with another module.

Another thing to look out for is having an incorrect import statement. If you do import requests, then access the get method as requests.get(...).

The Python interpreter first looks for the imported module in the built-in modules, then in the current directory, then in the PYTHON PATH, then in the installation-dependent default directory.

So, when we create a local file with the same name as that of a third-party module, we effectively shadow the official module with our local file.

# Checking where the requests module is located

You can access the __file__ property on the imported module to see whether it is shadowed by a local file.

main.py
import requests print(requests.__file__) # ⛔️ result if shadowed by local file # /home/borislav/Desktop/bobbyhadz_python/requests.py # ✅ result if pulling in correct module # /home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.10/site-packages/requests/__init__.py

check where requests module is located

The first result shows what the output looks like if you have a local file that's named requests.py.

The local file shadows the original requests module and causes the issue.

The second result shows what the output should look like if the correct requests module is being pulled.

# Printing the attributes of the requests module

A good way to start debugging is to print(dir(your_module)) and see what attributes the imported module has.

Here is what printing the attributes of the requests module looks like when I have a file requests.py in the same directory.

requests.py
import requests # ['__builtins__', '__cached__', '__doc__', '__file__', # '__loader__', '__name__', '__package__', '__spec__'] print(dir(requests))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

If you try to access any attribute that is not in this list, you would get the "AttributeError: module has no attribute".

We can see that the imported requests module doesn't have a get attribute, which makes it evident that we are shadowing the official requests module with our local requests.py file.

If you try to import the requests module in a file called requests.py, you would get a little different error message that means the same thing.

requests.py
import requests print(dir(requests)) def make_request(): # ⛔️ AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import) res = requests.get('https://reqres.in/api/users') parsed = res.json() print(parsed) make_request()

Renaming your file solves the error.

# AttributeError module 'requests' has no attribute 'post'

The Python "AttributeError module 'requests' has no attribute 'post'" occurs when we have a local file named requests.py and try to import from the requests module.

To solve the error, make sure to rename any local files named requests.py.

attributeerror module requests has no attribute post

Here is an example of how the error occurs in a file called requests.py.

requests.py
import requests def make_request(): # ⛔️ AttributeError: module 'requests' has no attribute 'post' res = requests.post( 'https://reqres.in/api/users', data={'name': 'Bobby Hadz', 'job': 'programmer'} ) print(res.json()) make_request()

The most likely cause of the error is having a local file named requests.py which shadows the official requests module.

# Make sure you don't have a local file named requests.py

Make sure to rename your local file to something other than requests.py to solve the error.

main.py
import requests def make_request(): res = requests.post( 'https://reqres.in/api/users', data={'name': 'Bobby hadz', 'job': 'programmer'} ) print(res.json()) # ✅ Works make_request()

Another thing to look out for is having an incorrect import statement. If you do import requests, then access the post method as requests.post(...).

The Python interpreter first looks for the imported module in the built-in modules, then in the current directory, then in the PYTHON PATH, then in the installation-dependent default directory.

So, when we create a local file with the same name as that of a third-party module, we effectively shadow the official module with our local file.

You can access the __file__ property on the imported module to see whether it is shadowed by a local file.

main.py
import requests print(requests.__file__) # ⛔️ result if shadowed by local file # /home/borislav/Desktop/bobbyhadz_python/requests.py # ✅ result if pulling in correct module # /home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.10/site-packages/requests/__init__.py

A good way to start debugging is to print(dir(your_module)) and see what attributes the imported module has.

# Printing the attributes of the requests module

Here is what printing the attributes of the requests module looks like when I have a file requests.py in the same directory.

requests.py
import requests # ['__builtins__', '__cached__', '__doc__', '__file__', # '__loader__', '__name__', '__package__', '__spec__'] print(dir(requests))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

If you try to access any attribute that is not in this list, you would get the "AttributeError: module has no attribute".

We can see that the imported requests module doesn't have the post attribute, which makes it evident that we are shadowing the official requests module with our local requests.py file.

If you try to import the requests module in a file called requests.py, you would get a little different error message that means the same thing.

requests.py
import requests def make_request(): # ⛔️ AttributeError: partially initialized module 'requests' has no attribute 'post' (most likely due to a circular import) res = requests.post( 'https://reqres.in/api/users', data={'name': 'Bobby Hadz', 'job': 'programmer'} ) print(res.json()) make_request()

Renaming your file solves the error.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.