在Python3.6中加载模块

在Python3.6中加载模块,python,python-3.x,Python,Python 3.x,在python3.6中加载模块时出错 spec = importlib.util.spec_from_file_location(load_module,path) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) 我得到以下错误: mod = importlib.util.module_from_spec(spec) File "<frozen importlib._boo

python3.6
中加载模块时出错

   spec = importlib.util.spec_from_file_location(load_module,path)
   mod = importlib.util.module_from_spec(spec)
   spec.loader.exec_module(mod)
我得到以下错误:

mod = importlib.util.module_from_spec(spec) File "<frozen importlib._bootstrap>",
line 568, in module_from_spec AttributeError: 'NoneType' object has no attribute 'loader'

具有路径中模块的路径。这适用于
Python3.7

,因此您可以按如下方式以编程方式导入模块:

my_module = importlib.import_module('my_module')
要指定可使用的自定义路径,请执行以下操作:

spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
如果出现以下错误,则表示
spec\u from\u file\u location
无法找到指定的模块和路径,并返回
None

in module_from_spec AttributeError: 'NoneType' object has no attribute 'loader'

使用
importlib.machine
是一种非常适合我的选择:
in module_from_spec AttributeError: 'NoneType' object has no attribute 'loader'