473,749 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,g lobals())
# 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 1680
Slawomir Nowaczyk wrote:
Hello,

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

def eexecfile(file) :
# do other stuff
execfile(file,g lobals())
# 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,g lobals())
# 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.h kwrote:

#Slawomir Nowaczyk wrote:
# Hello,
#
# Let's say I have a module "emacs", defining function eexecfile(file) :
#
# def eexecfile(file) :
# # do other stuff
# execfile(file,g lobals())
# # 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,g lobals())
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.eexecf ile("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,g lob)

main:

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

--
Piet van Oostrum <pi**@cs.uu.n l>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
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
3049
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 a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less like a collection to me. Even though it's still intended as a data-only structure, the use cases...
5
3653
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 these menus are then placed into the page by calling Server.Execute(rs_Modules("ModulePath")). This works fine for up to 15 "menus" After that, the session variables that were set (not including those called by Global.ASA) are no longer set. ...
5
1595
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 have to copy the same function into all aspx page which may need it. Is it possible that I define this function something in the application, I can use it on every .aspx page??
11
1369
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 so that I could import it. I was hoping to write my functions so that they'd be available the same as functions from system.data namespaces and the like are available. Is this not possible? TIA
10
4443
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 and it appears that shelve probably isn't the best option for various reasons. This lead me to try messing w/ pickle, but I can't figure out how to use it with classes. I've found many examples of using pickle w/ non-OOP code but nothing that...
15
2820
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 was typed one after the other in a interactive session?
3
4937
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 "D:/softwares/Apache2.2/htdocs"> AddHandler mod_python .py PythonHandler mptest PythonDebug On </Directory>
4
3332
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 a config file like: class=DBHandler
0
1110
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 project/src/myPackage/module2.py project/src/myPackage/test/
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9386
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.