execfile

Stop execution of a script called with execfile

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

Python: execfile from other file's working directory?

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

Why doesn't import prevent NameError in a python script run with execfile()?

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

Exit to command line in Python

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

How to stop a python subprocess which is running unit tests right away? Terminate and kill not working

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

Python shell and execfile scope

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

Alternative to python atexit module that works when called from other scripts

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

IPython threading server execfile()

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