catBuddy
catBuddy

Reputation: 397

ImportError: cannot import name 'FastAPI' from partially initialized module 'fastapi' : circular import

My initial project setup was working fine but, after installing psutil, I started getting a circular import error for fastApi. I tried uninstalling psutil but, the error persists.

File ".\project.py", line 1, in <module>
        from fastapi import FastAPI
      File "c:\users\nikhi\pycharmprojects\fastapi\venv\lib\site-packages\fastapi\__init__.py", line 7, in <module>
        from .applications import FastAPI as FastAPI
      File "c:\users\nikhi\pycharmprojects\fastapi\venv\lib\site-packages\fastapi\applications.py", line 4, in <module>
        from fastapi import routing
      File "c:\users\nikhi\pycharmprojects\fastapi\venv\lib\site-packages\fastapi\routing.py", line 30, in <module>
        from fastapi.encoders import DictIntStrAny, SetIntStr, jsonable_encoder
      File "c:\users\nikhi\pycharmprojects\fastapi\venv\lib\site-packages\fastapi\encoders.py", line 9, in <module>
        from fastapi import FastAPI
    ImportError: cannot import name 'FastAPI' from partially initialized module 'fastapi' (most likely due to a circular import) 

Upvotes: 4

Views: 8389

Answers (1)

MatsLindh
MatsLindh

Reputation: 52892

If your project is named fastapi and installed as a module, or you have a file named fastapi.py in your project, those will override the internal dependency loading for the module themselves (depending on the path set up by the Python interpreter).

Upvotes: 10

Related Questions