How to execute a file outside module's namespace?

Piet van Oostrum piet at cs.uu.nl
Tue Aug 15 09:40:20 EDT 2006


>>>>> Slawomir Nowaczyk <slawomir.nowaczyk.847 at student.lu.se> (SN) wrote:

>SN> Hello,
>SN> Let's say I have a module "emacs", defining function eexecfile(file):

>SN> def eexecfile(file):
>SN>     # do other stuff
>SN>     execfile(file,globals())
>SN>     # do other stuff

>SN> Now, assume I have file test.py containing an assignment "x=1"

>SN> If I run python and do:

>SN> import emacs
>SN> emacs.eexecfile("test.py")
>SN> print emacs.x   # works, x was put in module namespace
>SN> print x         # doesn't work, x is not defined in main script namespace

>SN> What is the best way to make "print x" work? Using the following:

emacs.py:

def eexecfile(file, glob):
    # do other stuff
    execfile(file,glob)

main:

import emacs
emacs.eexecfile("test.py", globals())
print x

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list