简体   繁体   中英

Loading Module in python 3.6

I am getting an error when I load the module in 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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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