How to execute a file outside module's namespace?

S

Slawomir Nowaczyk

Hello,

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

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

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

If I run python and do:

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

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

import __main__
def eexecfile(file):
# do other stuff
execfile(file, __main__.__dict__)
# do other stuff

seems to work, but it gives me a slightly uneasy feeling. Is this the
right way?

--
Best wishes,
Slawomir Nowaczyk
( (e-mail address removed) )

Today advance is so rapid that even the astronauts who set foot on the
moon in 1969 had never seen a digital watch
 
A

Angelo Zhou

Slawomir said:
Hello,

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

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

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

If I run python and do:

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

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

import __main__
def eexecfile(file):
# do other stuff
execfile(file, __main__.__dict__)
# do other stuff

seems to work, but it gives me a slightly uneasy feeling. Is this the
right way?

"from emacs import x" will expose x to the current namespace
 
A

Angelo Zhou

Slawomir said:
Hello,

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

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

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

If I run python and do:

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

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

import __main__
def eexecfile(file):
# do other stuff
execfile(file, __main__.__dict__)
# do other stuff

seems to work, but it gives me a slightly uneasy feeling. Is this the
right way?

"from emacs import x" will expose x to the current namespace
 
S

Slawomir Nowaczyk

On Fri, 11 Aug 2006 01:23:14 +0800

#> Slawomir Nowaczyk wrote:
#> > Hello,
#> >
#> > Let's say I have a module "emacs", defining function eexecfile(file):
#> >
#> > def eexecfile(file):
#> > # do other stuff
#> > execfile(file,globals())
#> > # do other stuff
#> >
#> > Now, assume I have file test.py containing an assignment "x=1"
#> >
#> > If I run python and do:
#> >
#> > import emacs
#> > emacs.eexecfile("test.py")
#> > print emacs.x # works, x was put in module namespace
#> > print x # doesn't work, x is not defined in main script namespace
#> >
#> > What is the best way to make "print x" work? Using the following:
#> >
#> > import __main__
#> > def eexecfile(file):
#> > # do other stuff
#> > execfile(file, __main__.__dict__)
#> > # do other stuff
#> >
#> > seems to work, but it gives me a slightly uneasy feeling. Is this the
#> > right way?
#> >
#>
#> "from emacs import x" will expose x to the current namespace

True... but I do not know in advance what is the contents of test.py
file -- it could be "a=1" :) Sure, I could go over emacs.__dict__ and
expose everything except eexecfile, but that's even less satisfying
than the solution above.

Anyway, thanks for the suggestion.

--
Best wishes,
Slawomir Nowaczyk
( (e-mail address removed) )

Zawinski's Law: "Every program attempts to expand until it can read mail.
Those programs which cannot so expand are replaced by ones which can."
 
P

Piet van Oostrum

Slawomir Nowaczyk said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top