首页 > 解决方案 > 错误“时间”未定义:UndefinedVariable

问题描述

def time_stats(df):
    start_time = time.time()

当我在 VS Code 中运行此代码时,它运行良好,但是“时间”带有下划线: 带下划线的词

如果我将鼠标悬停在那里,我会看到来自 PyLance 的错误消息,其中指出:

"time" is not defined: UndefinedVariable

我在文件开头做了,但在https://docs.python.org/3/library/time.html#time.timeimport time中找不到线索。

我想这不是一个大问题,但由于我正在学习,我很想得到一些建议,这样我就可以从一开始就避免坏习惯。

标签: pythonvisual-studio-codepylance

解决方案


你的代码看起来不错。您可以检查的几件事

  • 尝试禁用pylance.

  • 如果您使用的是 python linters(pylint),那么您可能也需要禁用它。检查此Github 问题以获取更多详细信息。

    python.linting.pylintEnabled": false使用此代码在您的 setting.json 文件中禁用 pylint。

让我知道这是否有帮助。


推荐阅读