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.
views:
136answers:
2
+6
A:
The builtin function execfile
does this.
execfile(filename)
Jason Orendorff
2010-01-07 15:32:12
you have to love python. How do you *exec*ute a *file* in python: execfile.
James Brooks
2010-01-07 19:09:13
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
2010-01-07 16:13:04
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
2010-01-07 17:21:15