current script path via execfile?

Adam Pletcher adam at volition-inc.com
Wed Nov 14 12:29:11 EST 2007


That works well, thank you!

- Adam

> -----Original Message-----
> From: python-list-bounces+adam=volition-inc.com at python.org
> [mailto:python-list-bounces+adam=volition-inc.com at python.org] On Behalf
> Of Gabriel Genellina
> Sent: Wednesday, November 14, 2007 12:34 AM
> To: python-list at python.org
> Subject: Re: current script path via execfile?
> 
> En Tue, 13 Nov 2007 13:42:04 -0300, Adam Pletcher <adam at volition-
> inc.com>
> escribió:
> 
> > I have an app with an embedded Python interpreter.  In that
> interpreter,
> > I want to use "execfile" (or something similar) to execute a script
> from
> > disk.  The script needs to somehow acquire the full path to itself,
> but
> > I can't work out how to do this.
> >
> >
> > Since it's run with execfile in the embedded interpreter, there's no
> > sys.argv to look at.  Is there another way to get the current
> script's
> > full pathname?
> 
> Set a __file__ variable in the globals that you pass to execfile:
> 
> fullpath = os.path.abspath(your script file)
> g = globals().copy()
> g['__file__'] = fullpath
> execfile(fullpath, g)
> 
>  From inside the executed script, just inspect __file__
> 
> --
> Gabriel Genellina
> 



More information about the Python-list mailing list