ctrl-alt-delor
ctrl-alt-delor

Reputation: 7745

Dynamic import of several names from a module

I am trying to import some symbols from one package into another. I have tried the following, with no luck as both are syntax errors.

from signal import SIG*    

or

import _signal
import _re
from signal import [i for i in dir(_signal) if _re.search("^SIG",i)!=None ]

Is there a way to do this.

Upvotes: 3

Views: 138

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124238

Use importlib:

import importlib

mod = importlib.import_module('signal')
loc = locals()
for name in dir(mod):
    if name.startswith('SIG'):
        loc[name] = getattr(mod, name)
del mod, loc, importlib

Upvotes: 7

Related Questions