Pydantic: pylint: No name 'BaseModel' in module 'pydantic' (no-name-in-module)

Created on 1 Oct 2020  Â·  10Comments  Â·  Source: samuelcolvin/pydantic

Checks

  • [x] I added a descriptive title to this issue
  • [x] I have searched (google, github) for similar issues and couldn't find anything
  • [x] I have read and followed the docs and still think this is a bug

Bug

$ pylint main.py --disable=C,W
************* Module main
main.py:1:0: E0611: No name 'BaseModel' in module 'pydantic' (no-name-in-module)

-----------------------------------------------------------------------
Your code has been rated at -40.00/10

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.6.1
            pydantic compiled: True
                 install path: /path/to/.env/lib/python3.8/site-packages/pydantic
               python version: 3.8.5 (default, Sep  4 2020, 02:22:02)  [Clang 10.0.0 ]
                     platform: macOS-10.15.4-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

Reproduce

main.py:

from pydantic import BaseModel

Quick way to reproduce env:

mkdir pydantic-pylint
cd pydantic-pylint
echo "from pydantic import BaseModel" > main.py
conda create --prefix ./.env python=3.8 && conda activate ./.env
pip install "pydantic==1.6.1" "pylint==2.6.0"
pylint main.py --disable=C,W
bug

Most helpful comment

@darkclouder I've been running into this error for a while with Python 3.7, I thought it was just because of Pydantic and its syntax. You can have Pylint ignore it if needed by adding this snippet at the top of the file:

# pylint: disable=no-name-in-module
# pylint: disable=no-self-argument

All 10 comments

@darkclouder I've been running into this error for a while with Python 3.7, I thought it was just because of Pydantic and its syntax. You can have Pylint ignore it if needed by adding this snippet at the top of the file:

# pylint: disable=no-name-in-module
# pylint: disable=no-self-argument

I'm also getting this error when using Python 3.9 and pylint 2.6.0

Hey guys,
I can reproduce but the issue comes from pylint, not pydantic!
I guess you just need to add pydantic in the whitelist to make pylint build an AST from the live module

â–¶ pylint main.py
************* Module main
main.py:2:0: E0611: No name 'BaseModel' in module 'pydantic' (no-name-in-module)
main.py:5:0: R0903: Too few public methods (0/2) (too-few-public-methods)

---------------------------------------------------------------------
Your code has been rated at -5.00/10
â–¶ pylint --extension-pkg-whitelist='pydantic' main.py

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: -5.00/10, +15.00)

If it solves your problem for all of you I'll close this issue!
Cheers

Did not solve the issue for me. It says I have a syntax issue at the 2:0
image

The problem has not been solved for me?
isues

The workaround by @NazimAli2017 works but disable some pylint feature for the concerned file. (it's fine thought).

Note : the following remark havn't been tested, I'm a Python beginner trying it for a side project, I encountered the problem following a FastApi tutorial : https://fastapi.tiangolo.com/tutorial/sql-databases/.

Since pylint is widely used maybe consider adding an __init__.py file in the pydantic module ?

The warning is evoked in this stackoverflow question :
https://stackoverflow.com/questions/40334643/no-name-in-module-error-from-pylint

The official documentation link :
https://docs.python.org/3/reference/import.html#regular-packages

+100 to getting this fixed. I think it's important to consider this from the perspective of enterprise organizations who are afraid to move to newer better libraries, having to comment a pylint exception everywhere pydantic is used is raising a lot of unnecessary red flags for my org.

Since pylint is widely used maybe consider adding an __init__.py file in the pydantic module ?

It already exists, is there a particular problem with it?


@wozniakty I'd love to fix this and support pylint more. Please research the problem and develop a fix, I'd be very happy to review a PR.

This is a common problem with binary extensions. The pylint official stance is, that you need to add the following to .pylintrc:

[MASTER]
extension-pkg-whitelist=pydantic

Cf: https://github.com/PyCQA/pylint/issues/1524#issuecomment-508934142

I guess if you want to automate this, you need to extend pylint. This is not something which should be handled in individual modules.

Thank you so much @bodograumann

Closing this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmfigol picture dmfigol  Â·  38Comments

marlonjan picture marlonjan  Â·  37Comments

koxudaxi picture koxudaxi  Â·  25Comments

chopraaa picture chopraaa  Â·  18Comments

bradodarb picture bradodarb  Â·  22Comments