【发布时间】:2016-08-06 09:52:09
【问题描述】:
我有以下代码来动态加载模块:
def load_module(absolute_path):
import importlib.util
module_name, _ = os.path.splitext(os.path.split(absolute_path)[-1])
try:
py_mod = imp.load_source(module_name, absolute_path)
except ImportError:
module_root = os.path.dirname(absolute_path)
print("Could not directly load module, including dir: {}".format(module_root))
spec = importlib.util.spec_from_file_location(
module_name, absolute_path, submodule_search_locations=[module_root, "."])
py_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(py_mod)
return py_mod
它工作得非常好,除了它试图在同一个文件夹中导入脚本(而不是同名包的一部分)的情况。例如,脚本a.py 正在执行import b。它会导致错误 ImportError: No module named 'b'(这在 Python 3 中很常见)。
但我真的很想找到解决这个问题的方法吗?可以通过添加以下内容来解决:
import sys
sys.path.append(".")
为“a”编写脚本。
虽然我希望它可以通过以下方式解决:
submodule_search_locations=[module_root, "."]
哦,是的,理由是我还想支持导入不是正确的包/模块的模块,而只是一些可以在解释器中工作的脚本。
可重现的代码:
~/example/a.py
>import b
~/example/b.py
>print("hi")
~/somewhere_else/main.py(与a/b不同)
import sys, os, imp, importlib.util
def load_module(absolute_path) ...
load_module(sys.argv[1])
然后在命令行运行:
cd ~/somewhere_else
python3.5 main.py /home/me/example/a.py
导致ImportError: No module named 'b'
下面的代码解决了这个问题,但是我们当然不能在所有脚本中手动添加sys.path的东西。
~/example/a.py (2)
import sys
sys.path.append(".")
import b
我真的希望其他人可能有一个我还没有想到的解决方案。
附录
def load_module(absolute_path):
import importlib.util
module_name, _ = os.path.splitext(os.path.split(absolute_path)[-1])
try:
py_mod = imp.load_source(module_name, absolute_path)
except ImportError as e:
if "No module named" not in e.msg:
raise e
missing_module = e.name
module_root = os.path.dirname(absolute_path)
if missing_module + ".py" not in os.listdir(module_root):
msg = "Could not find '{}' in '{}'"
raise ImportError(msg.format(missing_module, module_root))
print("Could not directly load module, including dir: {}".format(module_root))
sys.path.append(module_root)
spec = importlib.util.spec_from_file_location(module_name, absolute_path)
py_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(py_mod)
return py_mod
【问题讨论】:
-
请不要乱跑;删除然后重新发布以逃避您收到的投票。是的,如果您想从同一目录导入但没有包,则需要操作
sys.path。 -
@MartijnPieters 考虑到我对它进行了巨大的改造,不得不等待让它暂停有点愚蠢。
-
这就是审查队列的用途。像这样删除和重新发布可以并且将使您进入(自动)问题禁令。
标签: python import python-3.5 python-importlib