【发布时间】:2019-01-14 18:47:00
【问题描述】:
我做了以下自动加载器
from os import listdir
from os.path import dirname
from importlib import import_module
def replace(dst: dict, src: dict):
dst.clear()
dst.update(src)
replace(globals(), {
module: getattr(import_module('.' + module, __package__), module)
for module in [
file.rstrip('.py')
for file in listdir(dirname(__file__))
if not file.startswith('__') and file.endswith('.py')
]
})
简化从结构如下的目录加载:
mod/
__init__.py (see above)
foo.py
goo.py
....py
这样我就不用写丑陋的代码了
from mod.foo import foo
但我可以这样做
from mod import foo
不幸的是,IntelliJ IDEA 社区无法处理它并将 foo 识别为模块。
如何才能在此 IDE 中正确解析我的模块?
【问题讨论】:
-
为什么那个 import 很难看?你用
os.path做同样的事情。 -
@Makoto 不准确,
os.path包含的内容不仅仅是dirname和dirname未命名为path
标签: python intellij-idea pycharm