Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pydantic BaseModel not found in Fastapi

I have python3 3.6.9 on Kubuntu 18.04. I have installed fastapi using pip3 install fastapi. I'm trying to test drive the framework through its official documentation and I'm in the relational database section of its guide.

In schemas.py:

from typing import List

from pydantic import BaseModel

class VerseBase(BaseModel):
    AyahText: str
    NormalText: str

class Verse(VerseBase):
    id: int

    class Config:
        orm_mode = True

VS code highlights an error in from pydantic import BaseModel and it tells that: No name 'BaseModel' in module 'pydantic'. Additionally, when I try to run uvicorn main:app reload I have gotten the following error:

File "./main.py", line 6, in <module>
   from . import crud, models, schemas
ImportError: attempted relative import with no known parent package

I have tried to renstall pydantic using pip3 but it tells me that:

Requirement already satisfied: dataclasses>=0.6; python_version < "3.7" in ./.local/lib/python3.6/site-packages (from pydantic) (0.7)
like image 673
SaidbakR Avatar asked Jan 22 '26 07:01

SaidbakR


2 Answers

This is a common problem with binary/C extensions. For further details, check here: (Pylint & C extensions)

To fix it, you need to add the following to .pylintrc file (You can add this file to your current project folder if you like)

[MASTER]
extension-pkg-allow-list=pydantic

Note that switching to mypy (as suggested by another answer here) is not the right approach since pylint & mypy are two different things (the former is a linter while the latter is sort of a type checker)

like image 136
Salem Artin Avatar answered Jan 24 '26 21:01

Salem Artin


The first thing that you noticed

pydantic BaseModel not found in Fastapi

is related to your linter. I got the same warning when my linter was pylint, so I changed the linter from pylint to mypy and the problem disappeared.

  1. Install mypy via pip

    pip install mypy

  2. Open the command palette in VScode

    Ctrl+Shift+P

  3. Type this in command palett:

    Python: Select Linter

  4. Then Select mypy in the list of linters

like image 24
Unique_boy96 Avatar answered Jan 24 '26 21:01

Unique_boy96



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!