Luke Davis Luke Davis - 1 year ago 71
Linux Question

Lost important .py file (overwritten as 0byte file), but the old version still LOADED IN IPYTHON as module -- can it be retrieved?

Due to my stupidity, while managing several different screen sessions with vim open in many of them, in the process of trying to "organize" my sessions I somehow managed to overwrite a very important .py script with a 0Byte file.

HOWEVER, I have an ipython instance open that, when running that same .py file as a module, still remembers the code that used to be there!

So did I just learn a hard lesson about backups (my last one was done by vim about a week ago, which would leave me with a lot of work to do), or is there any possible, conceivable way to retrieve the .py file from an already loaded module? I probably deserve this for being so cavalier, but I'm seriously desperate here.

Answer Source

As noted in comments, inspect.getsource will not work because it depends on the original file (ie, module.__file__).

Best option: check to see if there's a .pyc file (ex, foo.pyc should be beside If there is, you can use Decompile Python 2.7 .pyc to decompile it.

The inspect modules also caches the source. You may be able to get lucky and use inspect.getsource(module), or inspect.getsourcelines(module.function) if it has been called in the past.

Otherwise you'll need to rebuild the module "manually" by inspecting the exports (ie, module.__globals__). Constants and whatnot are obvious, and for functions you can use func.func_name to get its name, func.__doc__ to get the docstring, inspect.getargspec(func) to get the arguments, and func.func_code to get details about the code: co_firstlineno will get the line number, then co_code will get the code. There's more on decompiling that here: Exploring and decompiling python bytecode

For example, to use uncompyle2:

>>> def foo():
...     print "Hello, world!"
>>> from StringIO import StringIO
>>> import uncompyle2
>>> out = StringIO()
>>> uncompyle2.uncompyle("2.7", foo.func_code, out=out)
>>> print out.getvalue()
print 'Hello, world!'

But, no — I'm not aware of any more straight forward method to take a module and get the source code back out.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download