【发布时间】:2019-12-04 07:37:47
【问题描述】:
VS 代码显示“继承'Base',它不是一个类” 给出以下错误消息:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Integer , String , Column
Base = declarative_base()
class Socio(Base):
__tablename__ = 'socios'
id = Column(Integer, autoincrement = True , primary_key = True)
dni = Column(Integer , unique = True)
nombre = Column(String(250))
apellido= Column(String(250))
为什么会这样?我该如何解决?
【问题讨论】:
-
不要相信你的 linter(或任何其他 Python 静态分析工具)告诉你的一切:运行代码并查看它是否/如何实际上失败(然后包括您问题中的错误消息),而不是假设 VS Code 告诉您真相。
declarative_base()确实返回了一个类。 -
...也就是说:您发布的内容实际上并不是来自 Python 的错误。这是来自 VS Code 的警告。请告诉我们你从 Python 得到的实际错误,如果有的话。 (警告不会阻止代码真正运行;它只是表示 VS Code 的分析认为代码可能无法成功运行,但该分析并非来自 Python 本身,而且并不总是正确的) .
-
感谢您的回答。如果我在 python 终端中运行,我会收到此错误: nombre = Column(String(250)) Traceback (last recent call last): File "
", line 1, in NameError: name 'Column' is未定义 -
您在终端中运行的内容中是否包含
from sqlalchemy import Integer, String, Column? -
如果我在终端中运行它,我现在没有错误,但是当我尝试从其他 .py 文件导入“Socio”时,我收到此错误:“文件”/home/gastonpalav/Workspace/frro -soporte-2019-08/practico_05/ejercicio_02.py",第 6 行,在
中 from practico_05.ejercicio_01 import Base ,Socio ModuleNotFoundError: No module named 'practico_05'"
标签: python python-3.x visual-studio-code sqlalchemy