views:

136

answers:

2

I need to execute a Python script from an already started Python session, as if it were launched from the command line. It should be similar to doing source in bash or sh.

+6  A: 

The builtin function execfile does this.

execfile(filename)
Jason Orendorff
you have to love python. How do you *exec*ute a *file* in python: execfile.
James Brooks
A: 

If you're running ipython (which I highly recommend for interactive python sessions), you can type:

%run filename 

or

%run filename.py

to execute the module (rather than importing it). You'll get file-name completion, which is great for ReallyLongModuleName.py (not that you'd name your modules like that or anything).

Noah
thanks for the suggestion, I usually use ipython, but unluckily for this task I needed to debug a C library that was called from python via Ctypes. So I needed to load the python binary in gdb, run it, import ctypes, load the library, write down the load address, break to gdb, add the library symbols, set the breakpoints, continue executing python and finally load the module... using ipython would have added an extra step, as it's not a binary itself that can be loaded directly in gdb, but another python script :-)
fortran