By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

Using the imp module

P: n/a
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?

--
Claus Tondering

Oct 10 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Claus Tondering wrote:
>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>

Oct 10 '06 #2

P: n/a
Claus Tondering wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 10 '06 #3

P: n/a
Fredrik Lundh wrote:
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.

--
Claus Tondering

Oct 10 '06 #4

P: n/a
I wrote:
Fredrik Lundh wrote:
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.
Oops, I forgot to add that I realize that the update(vars(...)) feature
works well with the imp module too.

--
Claus Tondering

Oct 10 '06 #5

P: n/a
Claus Tondering wrote:
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>

Oct 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.