'FastAPI error: Cannot import name 'FastApi' from 'fastapi'

I want to use FastAPI. I installed it using pip, and when I am adding it to my project like that:

from fastapi import FastApi 

and running it, Iam getting the error:

cannot import name 'FastApi' from 'fastapi' (C:\Users\xxx\PycharmProjects\MyBank\venv\lib\site-packages\fastapi\__init__.py).

Can someone help me?



Solution 1:[1]

That should be as follows (with "API" being capitalised at the end):

from fastapi import FastAPI

Please have a look at the documentation.

Solution 2:[2]

Might come from the fact that the name of your python file is actually fastapi and python is messed up by the fact that import is primarily looking at the current folder to import files then the libs.

meaning that if you have a file named : fastapi.py python will think that import fastapi means import the fastapi.py file from the current working dir and will fail.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris
Solution 2 guignol