0

I have a string that contains the text of a Python module. I want to import this as a Python module bound to a local name.

I can import it globally in a way such as the following:

exec myModuleString in globals()

What I want to do is import it bound to a local name, much as in the following way:

import numpy as np

How could I do this?

d3pd
  • 7,935
  • 24
  • 76
  • 127
  • The equivalent of `import numpy as np` using a string as the name would be `np = __import__("numpy")`. There is a wrapper for this in [`importlib`](https://docs.python.org/2/library/importlib.html). – jonrsharpe Feb 03 '15 at 14:38
  • I can see the overlap, but I'm not sure this question is exactly a duplicate. The emphasis in this question is the binding to a local variable. I'll leave the decision to the wisdom of the moderators. – d3pd Feb 03 '15 at 14:43

0 Answers0