简体   繁体   English

在python 3.6中加载模块

[英]Loading Module in python 3.6

I am getting an error when I load the module in Python 3.6 . 我在Python 3.6加载模块时收到错误。

   spec = importlib.util.spec_from_file_location(load_module,path)
   mod = importlib.util.module_from_spec(spec)
   spec.loader.exec_module(mod)

I get the below error: 我得到以下错误:

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

How do I do it in the right way? 我该如何以正确的方式做到这一点?

In the past, I have been using: 在过去,我一直在使用:

mod = importlib.import_module(load_module)

with the path of modules in the path. 与路径中的模块路径。 This works for python 3.7 这适用于python 3.7

so you can import a module programatically like so: 所以你可以像这样以编程方式导入模块:

my_module = importlib.import_module('my_module')

To specify a custom path you can use: 要指定可以使用的自定义路径:

spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

If you're getting the error below, it means that spec_from_file_location couldn't locate the module and path you specified and returned None . 如果您收到以下错误,则表示spec_from_file_location无法找到您指定的模块和路径并返回None

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM