473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to execute a file outside module's namespace?

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
( Sl***************@cs.lth.se )

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

Aug 10 '06 #1
4 1670
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
Aug 10 '06 #2
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
Aug 10 '06 #3
On Fri, 11 Aug 2006 01:23:14 +0800
Angelo Zhou <mz***@cs.hku.hkwrote:

#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
( Sl***************@cs.lth.se )

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."

Aug 10 '06 #4
>>>>Slawomir Nowaczyk <sl*******************@student.lu.se(SN) wrote:
>SNHello,
SNLet's say I have a module "emacs", defining function eexecfile(file):
>SNdef eexecfile(file):
SN # do other stuff
SN execfile(file,globals())
SN # do other stuff
>SNNow, assume I have file test.py containing an assignment "x=1"
>SNIf I run python and do:
>SNimport emacs
SNemacs.eexecfile("test.py")
SNprint emacs.x # works, x was put in module namespace
SNprint x # doesn't work, x is not defined in main script namespace
>SNWhat 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 <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Aug 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
5
by: Phil Grimpo | last post by:
I have a very odd situation here. I have an administration page, where based on a users permissions, a recordset is called from the SQL server which has a list of paths to "Module Menus". Each of...
5
by: Guoqi Zheng | last post by:
Dear sir, If I need to use a function on xxx.aspx page, e.g. on a repeater control, I have to define this function inside xxx.aspx.vb file, if I put it in the module class, it will not work so I...
11
by: Don | last post by:
I created a module to work with INI files and compiled it to a DLL file. Then when I try using one of the functions, it tells me that the function is not defined. I even put it inside a namespace...
10
by: crystalattice | last post by:
I'm creating an RPG for experience and practice. I've finished a character creation module and I'm trying to figure out how to get the file I/O to work. I've read through the python newsgroup...
15
by: tmp123 | last post by:
Hello, Thanks for your time. We have very big files with python commands (more or less, 500000 commands each file). It is possible to execute them command by command, like if the commands...
3
by: joe jacob | last post by:
I configured apache to execute python scripts using mod_python handler. I followed below mentioned steps to configure apache. 1. In http.conf I added <Directory...
4
by: Frank Aune | last post by:
Hello, I've been playing with the python logging module lately, and I manage to log to both stderr and MySQL database. What I'm wondering, is if its possible to specify the database handler in...
0
by: Gabriel Rossetti | last post by:
Hello, I have been developping something in python that has the following hierarchy : project/src/myPackage/ project/src/myPackage/__init__.py project/src/myPackage/module1.py...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.