osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


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.