Referring to a module by a string without using eval()

Steve D'Aprano steve+python at pearwood.info
Wed May 17 05:00:48 EDT 2017


On Wed, 17 May 2017 05:14 pm, Terry Reedy wrote:

> On 5/17/2017 2:04 AM, jeanbigboute at gmail.com wrote:
> 
>> Question: Is there a solution to this "turn a string into the module it
>> represents" problem?
> 
> Built-in function __import__


Like all dunders, __import__ is intended for the interpreter's use. In
modern Python, the way to import a module by name is:

import importlib
module = importlib.import_module("name")




-- 
Steve
Emoji: a small, fuzzy, indistinct picture used to replace a clear and
perfectly comprehensible word.




More information about the Python-list mailing list