Is it possible to break the execution of a Python script called with the execfile function without using an if/else statement? I've tried exit(), but it doesn't allow main.py to finish.
# main.py
print "Main starting"
execfile("script.py")
print "This should print"
# script.py
print "Script starting"
a = False
if a == False:
# Sa...
I have some code that loads a default configuration file and then allows users to supply their own Python files as additional supplemental configuration or overrides of the defaults:
# foo.py
def load(cfg_path=None):
# load default configuration
exec(default_config)
# load user-specific configuration
if cfg_path:
...
I looked at a number of existing questions about NameError exceptions when scripts are run with exec statements or execfile() in Python, but haven't found a good explanation yet of the following behavior.
I want to make a simple game that creates script objects at runtime with execfile(). Below are 4 modules that demonstrate the proble...
I have a script that I want to exit early under some condition:
if not "id" in dir():
print "id not set, cannot continue"
# exit here!
# otherwise continue with the rest of the script...
print "alright..."
[ more code ]
I run this script using execfile("foo.py") from the Python interactive prompt and I would like the script ...
I have a Tkinter GUI running two threads, the main tread for the GUI and a worker thread. The worker thread creates a subprocess using the following code:
myProcess = subprocess.Popen(['python', '-u', 'runTests.py'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
The file ru...
I'm working from inside an ipython shell and often need to reload the script files that contain my functions-under-construction.
Inside my main.py I have:
def myreload():
execfile("main.py")
execfile("otherfile.py")
Calling myreload() works fine if I have already ran in the same ipython session the execfile commands directly.
...
Using atexit.register(function) to register a function to be called when your python script exits is a common practice.
The problem is that I identified a case when this fails in an ugly way: if your script it executed from another python script using the execfile().
In this case you will discover that Python will not be able to locate...
My problem is the following: I would like to open an instance of ipython in a terminal of mine by simply typing, as usual:
$ ipython -pylab
and be able to have other processes order this ipython instance to execute a file as the execfile() would. This could be any other process, for instance, vim could ask ipython to run the currently...