【问题标题】:pylint shows import-error with directories inside the working directorypylint 显示工作目录内的目录的导入错误
【发布时间】:2020-06-10 00:53:16
【问题描述】:

安装新版本的 Python(从 3.8.1 到 3.8.3)并将其与 Visual Studio Code 一起使用后,我遇到了升级前没有发生的 pylint 问题。

我的工作目录树如下:

????bot
 ┣ ????cogs
 ┃ ┗ ????cog.py
 ┣ ????utils
 ┃ ┗ ????__init__.py
 ┃ ┗ ????format.py
 ┣ ????__init__.py
 ┣ ????core.py
????run.py

run.py 是脚本的“入口点”,它初始化所有内容,在其中执行import bot 工作正常,pylint 没有问题。相对导入对 pylint 没有任何问题,因为 core.py 使用 from .utils import ...

但是,当在 cog.py 中执行 from bot.utils import ... 时,pylint 一直显示 import-error 问题,因为代码运行良好,所以该问题不应该出现。

我很困惑为什么在升级之前没有出现这个问题,我做错了什么吗?如果是,我该如何改变这种行为?

【问题讨论】:

  • 在有人建议在 cog.py 中进行相对导入之前,我知道这是可能的,但它会影响我个人的可读性。
  • 在 cog.py 中进行相对导入。这是蟒蛇的方式。如果你不这样做,你就会被绊倒。尽管可读性强,但您需要养成习惯。

标签: python python-3.x visual-studio-code discord.py pylint


【解决方案1】:

事实证明,我不需要对 VSC 或 pylint 进行任何更改,我在 cogs 中添加了一个 __init__.py 文件,但出于某种奇怪的原因,它起作用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 2021-01-14
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多