【问题标题】:Python: import all submodules' method? [duplicate]Python:导入所有子模块的方法? [复制]
【发布时间】:2016-02-18 03:08:30
【问题描述】:

我将我的辅助方法组织成这样:

所有方法都在当前文件中使用,所以我需要像这样导入它们:

from helpers.utility_helper import *
from helpers.app_helper import *
from helpers.gmm_helper import *
from helpers.plot_helper import *

所以我可以直接在每个子模块中使用方法。例如,使用my_helper() 而不是helpers.utility_helper.my_helper()

但这看起来很冗长,是否可以将它们组合成一行?有些东西可能看起来像

import * from helpers/*

【问题讨论】:

    标签: python


    【解决方案1】:

    我建议使用显式导入而不是整体导入:

    from helpers.utility_helper import my_helper, your_helper
    

    这将有助于避免错误。

    对于短名称,您可以使用 as

    import helpers.utility_helper as util
    import helpers.app_helper as app
    import helpers.gmm_helper as gmm
    import helpers.plot_helper as plot
    

    这是很正常的做法。

    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 2011-03-22
      • 2020-07-15
      相关资源
      最近更新 更多