【问题标题】:error "time" is not defined: UndefinedVariable错误“时间”未定义:UndefinedVariable
【发布时间】:2021-06-22 15:05:00
【问题描述】:
def time_stats(df):
    start_time = time.time()

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

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

"time" is not defined: UndefinedVariable

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

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

【问题讨论】:

  • 听起来像是 VS Code 或 PyLance 的问题,而不是 Python。您可能希望将VS Code 和/或 [pylance] 标签添加到您的问题中,以便选择这些标签的人更容易找到您的问题。
  • 显然 PyLance 有很多问题,谷歌自动完成 "pylance no""pylance not detection imports"
  • 这能回答你的问题吗? VSCode settings for Pylance (并尝试将您的 python libs 目录添加到列表中?)
  • 谢谢大家!我很高兴听到这可能与我的代码无关。
  • @maud 你能正确运行你的代码吗?你能启用 flake8 linter 吗?你可以在你的 settings.json 文件中添加这个: "python.linting.flake8Enabled": true,

标签: python visual-studio-code pylance


【解决方案1】:

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

  • 尝试禁用pylance

  • 如果您使用的是 python linters(pylint),那么您可能也需要禁用它。查看Github issue了解更多详情。

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

如果这有帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2011-01-07
    • 1970-01-01
    • 2016-06-15
    相关资源
    最近更新 更多