我收到错误“ImportError:无法从‘pydantic’导入名称‘model_validator’”

问题描述 投票:0回答:1

我最近从 PyCharm Pro 的试用版切换到社区版,并开始在使用 aiogram 的 Python 项目中遇到错误。切换之前没有任何问题。

我已采取的步骤:

  • 将 aiogram 从 3.0.0 更新到 3.4.1。
  • 使用
    pip show aiogram
    pip show pydantic
    检查软件包版本。

该错误仅出现在 PyCharm 社区版中,专业版中不会出现。这是错误消息:

    from pydantic import BaseModel, ConfigDict, model_validator
ImportError: cannot import name 'model_validator' from 'pydantic' (C:\Users\shock\AppData\Local\Programs\Python\Python39\lib\site-packages\pydantic\__init__.cp39-win_amd64.pyd)

代码:

from sqlalchemy import create_engine, Column, Integer, BigInteger, String, DateTime, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import datetime
Base = declarative_base()

class UserPhoto(Base):
    __tablename__ = 'user_photos'
    id = Column(Integer, primary_key=True)
    user_id = Column(BigInteger, unique=True)
    photos_sent = Column(Integer, default=0)
    processing = Column(Boolean, default=False)
class UserLanguage(Base):
    __tablename__ = 'user_languages'
    id = Column(Integer, primary_key=True)
    user_id = Column(BigInteger, unique=True, nullable=False)
    language_code = Column(String(8), nullable=False)
class UserActivity(Base):
    __tablename__ = 'user_activity'
    id = Column(Integer, primary_key=True)
    user_id = Column(BigInteger, unique=True, nullable=False)
    first_interaction = Column(DateTime, default=datetime.datetime.utcnow)
    last_interaction = Column(DateTime, onupdate=datetime.datetime.utcnow)


engine = create_engine('mysql+pymysql://User:password@localhost/telegram_bot_ai_photo')
Base.metadata.create_all(engine)

Session = sessionmaker(bind=engine)
Base.metadata.create_all(engine)

版本:

Name: aiogram
Version: 3.4.1
Summary: Modern and fully asynchronous framework for Telegram Bot API
Home-page:
Author:
Author-email: Alex Root Junior <[email protected]>
License:
Location: c:\users\shock\pycharmprojects\bot\.venv\lib\site-packages
Requires: aiofiles, aiohttp, certifi, magic-filter, pydantic, typing-extensions
Required-by:
(.venv) PS C:\Users\shock\PycharmProjects\bot> pip show pydantic
Name: pydantic
(.venv) PS C:\Users\shock\PycharmProjects\bot> pip show pydantic
Name: pydantic
Version: 2.5.3
Summary: Data validation using Python type hints
Home-page: 
Author: 
Author-email: Samuel Colvin <[email protected]>, Eric Jolibois <[email protected]>, Hasan Ramezani <[email protected]>, Adrian Garcia Badaracco <[email protected]>, Terrence Dorsey <[email protected]>, David Montague <[email protected]>, Serge Matveenko <[email protected]>, Marcelo Trylesinski <[email protected]>, Sydney Runkle <[email protected]>, David Hewitt <[email protected]>
License: 
Location: c:\users\shock\pycharmprojects\bot\.venv\lib\site-packages
Requires: annotated-types, pydantic-core, typing-extensions
Required-by: aiogram

我正在 Windows 11 上使用 Python 3.9。什么可能导致此问题以及如何解决它?

python pydantic aiogram
1个回答
0
投票

尝试

pip install --upgrade pydantic

它对我有用(旧版本是

pydantic_core-2.14.6
pydantic-1.9.1

© www.soinside.com 2019 - 2024. All rights reserved.