0

I am new to the library of requests. I am trying to import the library requests and was learning from geekforgeeks about the library(https://www.geeksforgeeks.org/python-requests-tutorial/. The following is the code

import requests 
print(dir(requests))

the following is the output

Traceback (most recent call last):
  File "C:\Users\SUCHIISWAR\AppData\Local\Programs\Python\Python310\noth.py", line 1, in <module>
    import requests
  File "C:\Users\SUCHIISWAR\AppData\Local\Programs\Python\Python310\requests.py", line 4, in <module>
    r = requests.get('https://api.github.com/users/naveenkrnl')
AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import)

To fix the problem i referred to this question on stack overflow https://stackoverflow.com/questions/59035409/python-import-requests-results-in-traceback-partially-initialized-module-req

  File "C:\Users\SUCHIISWAR\AppData\Local\Programs\Python\Python310\noth.py", line 1, in <module>
    import requests
  File "C:\Users\SUCHIISWAR\AppData\Local\Programs\Python\Python310\requests.py", line 4, in <module>
    r = requests.get('https://api.github.com/users/naveenkrnl')

This part of the error is not even part of the document in which i am writing code but an altogether different .py file in which i was working before the code displayed to you.

enter image description here This is the name of the different .py file i was talking about

I tried referring to the stack overflow question and tried renaming the file. It did not make any difference

  • 3
    Your file is named `requests.py`, it's overriding the `requests` module. Change your file name to something else and it'll work – Gugu72 Sep 01 '23 at 14:49
  • My file is named request.py not requests.py i assume i shouldn't get the error. – Suchiiswar Paudmanabhan Sep 01 '23 at 14:53
  • 3
    According to the error message you have a file named `requests.py`. And it seems as if you store your files in the Python installation folder. Don't do that. – Matthias Sep 01 '23 at 15:05
  • i have posted an image for the clarification of the name of the file – Suchiiswar Paudmanabhan Sep 01 '23 at 15:21
  • 3
    @SuchiiswarPaudmanabhan: The existence of `request.py` does not preclude the existence of `C:\Users\SUCHIISWAR\AppData\Local\Programs\Python\Python310\requests.py` *as well*. That file *definitely* exists, and it *definitely* is trying to import the real `requests` module, which fails because it imports itself instead. You clearly have at least three files, named `noth.py`, `requests.py` and `request.py` in that folder (and probably others, since it's in the Python installation folder). Stop putting your new source files in the Python installation itself, and don't name-shadow modules you use. – ShadowRanger Sep 01 '23 at 15:25

0 Answers0