3

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.

1
  • 1
    What makes you think that syntax would work? Commented Nov 6, 2012 at 10:56

1 Answer 1

7

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
Sign up to request clarification or add additional context in comments.

2 Comments

In the documentation for locals() there is the note: The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter. Is it safe in your case and what would be an alternative?
@halex: It's safe in this case, but if it makes you feel better you can substitute it with globals(); at module scope they are one and the same object anyway. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.