Faking out __name__ == __main__

Alex Martelli aleaxit at yahoo.com
Mon Nov 1 03:06:09 EST 2004


Josiah Carlson <jcarlson at uci.edu> wrote:

> bokr at oz.net (Bengt Richter) wrote:
> > 
> > On Sun, 31 Oct 2004 17:05:59 -0500, Ed Leafe <ed at leafe.com> wrote:
> >  >>> def test():
> >  ...     d = {'__name__': '__xxxx__'}
> >  ...     execfile('tmain.py', d)
> 
> I believe it is technically more Pythonic to use __import__ rather than
> execfile, if only because you have access to the module itself when you
> are done.

Unfortunately, while __import__ does take parameters giving local and
global namespaces, it doesn't use them in anywhere like the way that
execfile does.  In particular, how would you "force __name__" with
__import__, as per this thread's subj?  I don't think you can...


Alex



More information about the Python-list mailing list