15

I have 3.6.9 on Kubuntu 18.04. I have installed 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)
asked Jun 2, 2020 at 13:15
0

6 Answers 6

17

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)

answered May 16, 2021 at 19:14
Sign up to request clarification or add additional context in comments.

Comments

6

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

answered Oct 10, 2020 at 7:09

Comments

6

Tested on vscode:

In your workspace folder, specify Options in

pylintrc

or

.pylintrc

options file, as specified in Pylint command line argument, using this command: pylint --generate-rcfile > .pylintrc

Look for extension-pkg-allow-list and add pydantic after = It should be like this after generating the options file: extension-pkg-allow-list=

When you add pydantic, it should be like this:

extension-pkg-allow-list=pydantic

This should solve your problem.

Razvan Stefanescu
4691 gold badge5 silver badges17 bronze badges
answered Nov 10, 2021 at 22:36

Comments

2

The problem of highlighting in VS code may be a problem due to the fact that you did not open the folder. It's quite annoying as it happens often to me as well (and I have basically your same config).

Regarding the second problem you mention, it is probably due to the fact that the folder in which the script lays, does not have a __init__.py file. If you add it, it should work since python will interpret the folder as a module.

As an alternative, you could try to import with the full path from the top folder (e.g. from app.module.main import app).

For more information about modules see the following links:

Python 3.8 Modules

Real Python

answered Jun 2, 2020 at 20:18

2 Comments

I have noticed something, when importing it from the project's root, the import works as regarded in the tutorial, but if I have tried to run it from the myprojectRoot/db, I have to modify the import statements
Without the folder structure it's not easy for me to provide an answer, but that's how python imports work (or at least I understand for your project structure)
2

Building on the excellent answers that explain how to modify .pylintrc, if you are using pyproject.toml for your project you can do this instead:

[tool.pylint.main]
extension-pkg-allow-list = ["pydantic"]
answered Mar 19, 2023 at 16:37

1 Comment

And in .pylintrc, you can use extension-pkg-allow-list=pydantic, see documentation
0

My linter was Pylint. Disabling pylint was worked for me. I also have Pylance but it didn't give me the same error

answered Apr 4, 2024 at 8:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.