A
A
alexey_korotynski2018-10-10 23:41:30
Python
alexey_korotynski, 2018-10-10 23:41:30

How to do import in python?

I import a module from a string. The problem is that the name of a module can be the same as a built-in module, and then the built-in one is imported. 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?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Shatalov, 2018-10-10
@netpastor

Yes, be a little tense with the names of the modules so that there are no intersections

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question