Using the imp module

C

Claus Tondering

I understand that you can use the imp module to programmatically mimic
the "import xyzzy" statement.

But is there any way to programmatically mimic the "from xyzzy import
*" statment?
 
F

Fredrik Lundh

Claus said:
I understand that you can use the imp module to programmatically mimic
the "import xyzzy" statement.

"imp" sounds like overkill for that purpose; the usual way is do do that is to
explicitly call __import__:

xyzzy = __import__("xyzzy")
But is there any way to programmatically mimic the "from xyzzy import
*" statment?

you can use getattr() to pick out the objects your need. or you could do
something like:

globals().update(vars(__import__("xyzzy")))

</F>
 
S

Steve Holden

Claus said:
I understand that you can use the imp module to programmatically mimic
the "import xyzzy" statement.

But is there any way to programmatically mimic the "from xyzzy import
*" statment?
See the docs for __import__().

I think imp is pretty much dead nowadays.

regards
Steve
 
C

Claus Tondering

Fredrik said:
globals().update(vars(__import__("xyzzy")))

Thank you, I can use that in some cases. However, I need to import a
file that is not in the load path. The imp module allows me to specify
a path name; but as far as I know, the __import__ function does not
allow me to do that.
 
C

Claus Tondering

I said:
Thank you, I can use that in some cases. However, I need to import a
file that is not in the load path. The imp module allows me to specify
a path name; but as far as I know, the __import__ function does not
allow me to do that.

Oops, I forgot to add that I realize that the update(vars(...)) feature
works well with the imp module too.
 
F

Fredrik Lundh

Claus said:
Thank you, I can use that in some cases. However, I need to import a
file that is not in the load path.

that's pretty straightforward:

path = list(sys.path)
sys.path.insert(0, "directory")
try:
module = __import__("module")
finally:
sys.path[:] = path # restore

or, often as useful,

namespace = {}
execfile("directory/module.py", namespace)

the latter executes the external code everything you run it (unlike import,
which uses the sys.modules cache), and puts everything defined by the
external script into the given dictionary.

to import all that into your own module, you can do:

globals().update(namespace)

for extra bonus, you may want to check the __all__ attribute, and if that's
not present, filter out anything that starts with an underscore:

all_names = namespace.get("__all__")
if all_names is None:
all_names = (key for key in namespace where key[0] != "_")
my_namespace = globals()
for name in all_names:
my_namespace[name] = namespace[name]

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top