【问题标题】:Import at the top of the file with gi使用 gi 在文件顶部导入
【发布时间】:2016-12-20 11:29:52
【问题描述】:

对于 libnotify,我使用以下代码

import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify

然后flake8 抱怨说:

E402 module level import not at top of file

另一方面,使用 gi 时必须指定版本:http://mednis.info/use-girequire_versiongtk-30-before-import.html

我该怎么办?忽略 linter 的消息或删除 gi.require_version 行?

【问题讨论】:

    标签: python python-import pep8


    【解决方案1】:

    # noqa: E402 放在文件顶部以忽略此文件中的此错误。

    有时,不在文件顶部的导入是必要的。例如,为了避免循环导入,避免在调用某个函数之前无意中听到初始化模块的情况,或者在此处进行配置。导入可能会产生显着的副作用,因此在这种边缘情况下,从顶部往下进行导入是完全可以接受的。

    【讨论】:

    • 这似乎对我不起作用。如果我把# noqa: E402 on every import line it works, but that is unwieldy
    猜你喜欢
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多