from flask_sqlalchemy import SQLAlchemy
File "D:\ProgramData\anaconda3\envs\python31014\lib\site-packages\flask_sqlalchemy\__init__.py", line 5, in <module>
from .extension import SQLAlchemy
File "D:\ProgramData\anaconda3\envs\python31014\lib\site-packages\flask_sqlalchemy\extension.py", line 9, in <module>
import sqlalchemy as sa
File "D:\ProgramData\anaconda3\envs\python31014\lib\site-packages\sqlalchemy\__init__.py", line 12, in <module>
from . import util as _util
File "D:\ProgramData\anaconda3\envs\python31014\lib\site-packages\sqlalchemy\util\__init__.py", line 15, in <module>
from ._collections import coerce_generator_arg as coerce_generator_arg
File "D:\ProgramData\anaconda3\envs\python31014\lib\site-packages\sqlalchemy\util\_collections.py", line 38, in <module>
from .typing import is_non_string_iterable
File "D:\ProgramData\anaconda3\envs\python31014\lib\site-packages\sqlalchemy\util\typing.py", line 56, in <module>
from typing_extensions import TypeAliasType as TypeAliasType # 3.12
ImportError: cannot import name 'TypeAliasType' from 'typing_extensions' (D:\ProgramData\anaconda3\envs\python31014\lib\site-packages\typing_extensions.py)
这个问题的原因是Python 环境中的 typing_extensions
模块版本与 SQLAlchemy
或 flask_sqlalchemy
的依赖要求不兼容。特别是,TypeAliasType
是 typing_extensions
模块中的一个新功能,但在较旧版本中不存在。
解决方法:
-
升级
typing_extensions
模块:
运行以下命令升级到最新版本的typing_extensions
,这通常可以解决问题:pip install --upgrade typing_extensions
-
升级
SQLAlchemy
和flask_sqlalchemy
:
确保您使用的是最新版本的SQLAlchemy
和flask_sqlalchemy
,它们可能已经修复了与较旧版本typing_extensions
的兼容性问题:pip install --upgrade SQLAlchemy flask_sqlalchemy
-
检查 Python 版本:
如果您使用的 Python 版本较旧(例如低于 3.8),可能与某些依赖库的兼容性较差。建议升级到最新的 Python 版本(例如 3.10 或 3.11)。 -
降级
SQLAlchemy
(临时方案):
如果升级typing_extensions
或SQLAlchemy
后问题仍未解决,您可以尝试降级SQLAlchemy
到一个兼容的较旧版本。例如:pip install SQLAlchemy==1.4.49
-
验证环境:
如果以上步骤完成后仍然出错,可以创建一个全新的虚拟环境,安装所需依赖项以确保没有版本冲突:python -m venv new_env source new_env/bin/activate # Linux/MacOS new_env\Scripts\activate # Windows pip install flask flask_sqlalchemy