How to make an import python?

Import a module from a string. The problem is that the module name can be the same as with a built-in module and then imported built-in. This code solves the problem:
from importlib import util
spec = util.spec_from_file_location("module.name", "/path/to/file.py")
foo = util.module_from_spec(spec)
spec.loader.exec_module(foo)


Is there any easier way to do this?
March 12th 20 at 08:11
1 answer
March 12th 20 at 08:13
Yes, a little tense with the names of the modules to avoid intersections
Or scopes, to put the same name module in a package and import as

import package

And refer to it as
package.module

Or you can import a module from a package, giving it a different name:

from package import module as my_module

Benefit packages in Python -- it's easy: make a folder, create an empty __init__.py . Well, to add a module to the package, moving it to the folder you created. Importantly, the folder lay next to run file, otherwise you'll have an additional path for the import to register. - savanna_Kirlin9 commented on March 12th 20 at 08:16

Find more questions by tags Python