import & __import__

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Sun Mar 12 20:57:26 EST 2000


[Moved your self-reply around so it looks better]
Chris Armstrong wrote in comp.lang.python:
> In article <2MUy4.5284$bt3.17551 at newsfeed.slurp.net>, Chris Armstrong <punck at SpamIsBadPenguinPowered.com> wrote:
> > Hi all. I'm pretty much a newbie to Python, and I have a question about 
> > import vs __import__. I'm implementing dynamically imported modules in
> > a game I'm writing, and I need to use __import__ to do this. One thing I
> > noticed is that __import__ doesn't seem to execute the modules that it
> > imports, while import does.. The docs for __import__ don't mention anything
> > about this, and I'm just curious about it. I tested this by making a simple
> > module that just has `` print "Hi." '' in it. `` import modulename '' caused
> > it to print out "Hi." while `` foobar = __import__("modulename") '' didn't.
> > This isn't really important to me, I was just curious about it.
>
> Bah. My bad. foobar = __import__("modulename") _does_ print out "Hi." I
> thought I had checked that. :P
> What _doesn't_ print out "Hi." is foobar.append(__import__("modulename")).
> *shrug*

foobar.append(__import__("module")) simply works.

Probably, you tried it _while the module was already loaded_. The print
would only happen the first time. Use reload() if you want to see "Hi." again.


-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
  3:56am  up 6 days, 16:16,  7 users,  load average: 0.08, 0.02, 0.01



More information about the Python-list mailing list