How to Resolve Python "AttributeError: module 'requests' has no attribute 'get' (or 'post')"
Encountering AttributeError: module 'requests' has no attribute 'get'
or AttributeError: module 'requests' has no attribute 'post'
in Python is almost always caused by a simple naming conflict. It usually means you have accidentally created your own Python file named requests.py
in your project directory, which interferes with Python's ability to find and load the actual installed requests
library.
This guide explains why this happens and provides the straightforward solution.
Understanding the Error: Namespace Collision
When you use import requests
, the Python interpreter searches for the requests
module in a specific order:
- Built-in modules.
- The directory containing the input script (or the current directory).
- Directories listed in the
PYTHONPATH
environment variable. - Installation-dependent default paths (like
site-packages
where pip installs libraries).
If you have a file named requests.py
in the same directory as the script you are running, Python finds your file first (step 2) and imports it instead of the installed requests
library from site-packages
. Since your local requests.py
file likely doesn't define functions like get
or post
, attempting to call requests.get()
or requests.post()
results in the AttributeError
. Your local file effectively "shadows" the real library.
The Primary Cause: A Local requests.py
File
The overwhelming reason for this error is creating a Python script and naming it exactly requests.py
.
# ⛔️ Problematic Code - IF THIS FILE IS NAMED requests.py ⛔️
import requests # This imports the file itself, not the library!
def make_get_request():
try:
# ⛔️ AttributeError: module 'requests' has no attribute 'get'
res = requests.get('https://httpbin.org/get', timeout=10)
print(res.json())
except AttributeError as e:
print(e)
def make_post_request():
try:
# ⛔️ AttributeError: module 'requests' has no attribute 'post'
res = requests.post('https://httpbin.org/post', data={'key':'value'}, timeout=10)
print(res.json())
except AttributeError as e:
print(e)
make_get_request()
make_post_request()
Running the code above when saved as requests.py
will produce the AttributeError
.
The Solution: Rename Your Local File
The fix is simple: Rename your local Python file to something that doesn't conflict with standard library or installed package names. Good alternatives include main.py
, app.py
, api_client.py
, or something descriptive of its purpose.
# ✅ Corrected Code - Saved as main.py (or any other non-conflicting name) ✅
import requests # Now imports the actual installed requests library
def make_get_request():
try:
# ✅ Works now
res = requests.get('https://httpbin.org/get', timeout=10)
print("GET Request Successful:")
print(res.json())
except requests.exceptions.RequestException as e:
print(f"GET Request Failed: {e}")
except AttributeError as e:
print(f"Unexpected AttributeError: {e}") # Should not happen now
def make_post_request():
try:
# ✅ Works now
res = requests.post('https://httpbin.org/post', data={'key':'value'}, timeout=10)
print("\nPOST Request Successful:")
print(res.json())
except requests.exceptions.RequestException as e:
print(f"POST Request Failed: {e}")
except AttributeError as e:
print(f"Unexpected AttributeError: {e}") # Should not happen now
make_get_request()
make_post_request()
After renaming your file, the import requests
statement will correctly find and load the installed library from your Python environment's site-packages
directory, and methods like requests.get()
and requests.post()
will be available.
Debugging: Confirming the Filename Conflict
If you're unsure whether a filename conflict is the cause, you can easily check:
Checking the Imported Module's File Path (__file__
)
Add print(requests.__file__)
after your import statement. This shows the path to the file Python actually loaded.
import requests
# ✅ Add this line for debugging
print(requests.__file__)
# ... rest of your code ...
- Incorrect (Shadowed): If the output path points to your project directory and ends with
requests.py
, you have a filename conflict./home/user/my_project/requests.py
- Correct (Library): If the output path points to your Python environment's
site-packages
directory (often inside avenv
or system Python path), then the correct library is being loaded./home/user/my_project/venv/lib/python3.9/site-packages/requests/__init__.py
Checking the Imported Module's Attributes (dir()
)
You can list the available attributes of the imported requests
module using dir()
.
import requests
# ✅ Add this line for debugging
print(dir(requests))
# ... rest of your code ...
- Incorrect (Shadowed): If you have a local
requests.py
that's mostly empty or just contains your script's code, the output ofdir(requests)
will be very short and will not includeget
,post
,put
,delete
,Session
, etc. It will look something like this:['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'make_get_request', 'make_post_request'] # Includes functions from your file, but not library functions
- Correct (Library): If the correct library is imported,
dir(requests)
will produce a long list of attributes, including the HTTP methods (get
,post
, etc.) and many other classes and functions provided by the library.
Related Error: Circular Import (partially initialized module
)
Sometimes, particularly within a file named requests.py
, you might see a slightly different but related error:
AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import)
This happens because the file requests.py
is trying to import itself during its own initialization process. Python detects this circular dependency and partially initializes the module, which means attributes like get
haven't been defined yet when your code tries to use them.
The solution is exactly the same: Rename your requests.py
file.
Conclusion
The AttributeError: module 'requests' has no attribute 'get'
or 'post'
is almost exclusively caused by naming your own script requests.py
. This prevents Python from loading the installed requests
library due to how the import system prioritizes the current directory.
- Simply rename your local file to resolve the error.
- Use
requests.__file__
anddir(requests)
as debugging tools to confirm if you are indeed importing your local file instead of the actual library.