473,761 Members | 7,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

execfile then import back

I am seeing something strange with execfile. I've simplified the code
to:

########## t.py ##########
print "here"
v = None

def f():
global v
v = 6

if __name__ == "__main__":
f()
print "0:", v
execfile("x.py" )
print "0:", v
execfile("y.py" )
print "0:", v

########## x.py and y.py (they are identical) ##########
import testexec
print "1:", v, testexec.v
testexec.v = 7

Runing "python t.py" (with Python 2.4.2), the printout I got is:

here
0: 6
here
1: 6 None
0: 6
1: 6 7
0: 6

So there is apparently 2 different instances of v at run time. Can
someone please explain (1) why this is the case, and (2) assuming this
is correct behavior, how I can avoid this? Thanks.

Jun 2 '06 #1
2 1479
> ########## x.py and y.py (they are identical) ##########
import testexec
print "1:", v, testexec.v
testexec.v = 7


Oops, testexec should be changed to t instead. That is:

########## x.py and y.py (they are identical) ##########
import t
print "1:", v, t.v
t.v = 7

Jun 2 '06 #2
Dennis Lee Bieber wrote:
And the problem you are seeing is that the initial "v" in the t.py
that you "run", is considered "__main__.v ", NOT "t.v"


Yes, the 2 different copies of v apparently imply that __main__ and t
are 2 different modules. But I had expected __main__ to be an alias of
t. Can you point out the passage in Python doc that explains this?

Jun 2 '06 #3

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

Similar topics

1
3110
by: Bo Jacobsen | last post by:
I have a number of files compiled to bytecode using py_compile.compile(). The .pyc files can be invoked by python directly ($python file.pyc), but "loading" them by execfile just throws an exception ? Any suggestions Bo.
3
6747
by: baur79 | last post by:
Hi everybody i need to execute this command line (different source for n times) filename.exe -type png -source sourcearg -file filename.png i try with python (python script and filename.exe in same directory) import os
2
1797
by: Alex Popescu | last post by:
Hi all! not be present in Py3k. So, I am wondering what will be its replacement? Considering that most probably Py3k will keep eval and exec, this will still be possible (indeed requiring manual loading of the file string), so I would really appreciate some enlightning comments on this. Background: Basically this question is related to my learning process/working project. In this, I have the need to allow the final user to provide a...
1
2267
by: Fernando Perez | last post by:
Hi all, I'm finding the following behavior truly puzzling, but before I post a bug report on the site, I'd rather be corrected if I'm just missing somethin obvious. Consider the following trivial script: # Simple script that imports something from the stdlib from math import sin, pi
2
2157
by: Dave Westerman | last post by:
I've got a Jython script where I'm doing an execfile() to pull in a small script that contains a function, which I then call. execfile(app_applscript) optionList = createOptionList(applOptions) When I do this at the global level in my main script, it works just fine. However, when I move this code into a function in my main script, then it fails, telling me it can't find the function createOptionList().
5
9392
by: George Sakkis | last post by:
I maintain a few configuration files in Python syntax (mainly nested dicts of ints and strings) and use execfile() to read them back to Python. This has been working great; it combines the convenience of pickle with the readability of Python. So far each configuration is contained in a single standalone file; different configurations are completely separate files. Now I'd like to factor out the commonalities of the different...
1
1553
by: moijes12 | last post by:
Hi i have 3 python files and i want to execute the files sequentially using the execfile command.Hence ,i have written the following program fileList = for fileName in fileList : execfile(fileName)
2
1633
by: Igor Kaplan | last post by:
Hello python gurus. I got quite unusual problem and all my searches to find the answer on my own were not successful. Here is the scenario: I have the python program, let's call it script1.py, this program needs to execute another python script, let's call it script2.py. In script1.py I have the statement: execfile('script2.py') Everything is fine beside one thing. The script2.py is pretty big python
4
7083
by: TP | last post by:
Hello, I have a script that uses the "optparse" package to parse the command line. For example: $ script.py --help # displays help about script.py Is this possible to call such a script with execfile('') once in the Python interactive shell?
0
9522
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
9336
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
10111
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
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
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
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.