102

I am trying to run my script but keep getting this error:

File ".\checkmypass.py", line 1, in <module>
  import requests 
line 3, in <module>
  response = requests.get(url) 
AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import)

How can I fix it?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Karthik Bhandary
  • 1,305
  • 2
  • 7
  • 16

4 Answers4

216

This can happen when there's a local file with the same name as an imported module – Python sees the local file and thinks it's the module.

In my case, I had a file I created in the same folder called requests.py. So my code was actually importing that file and not the actual requests module you install with pip. Then I had another issue with a file I created called logging.py. I renamed both files and the issue was resolved.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Eugene
  • 2,169
  • 2
  • 5
  • 3
91

Make sure the name of the file is not the same as the module you are importing – this will make Python think there is a circular dependency.

Also check the URL and the package you are using. "Most likely due to a circular import" refers to a file (module) which has a dependency on something else and is trying to be imported while it's already been imported. Once it's correct, you should have something like this:

import requests

r = requests.get("http://google.com")       
print(r.status_code)

# 200
meetar
  • 7,443
  • 8
  • 42
  • 73
Rojin
  • 1,045
  • 9
  • 15
  • 2
    I used the code you gave above,but I'm getting the same error can you explain more elaborately.Thank you in advance. – Karthik Bhandary Jan 17 '20 at 04:01
  • 3
    you have a local file with the same name as the imported module, hence it tries to recursively import itself. just rename this file. – alex May 16 '21 at 10:12
  • 2
    It can happen when you already have declared your list of file to load in the \_\_all\_\_ array in the \_\_init\_\_.py' file of your module. Modules names are already used so Python return an error – MrSo Feb 28 '22 at 16:19
-3

In my particular case, this resulted from the following sequence of commands when installing vaex:

conda install pydantic[dotenv]
# This failed: "import vaex" so retried pip.
pip install pydantic[dotenv]
# On "import vaex", got error in OP.

And the fix:

conda uninstall pydantic[dotenv]
pip install pydantic[dotenv] --force-reinstall
# Now "import vaex" worked perfectly.
Contango
  • 76,540
  • 58
  • 260
  • 305
-3

I was also getting the same error. What worked for me is: I deleted the virtual environment and did fresh installation. In my case some modules were installed repeatedly and I was able to see them in the venv/Lib folder which was causing the issue.

di_gupt10
  • 154
  • 2
  • 4
  • 11