4

Question might seem confusing but let me explain.

I was working on a jupyter notebook. I had some of my functions and classes in .py files that I was using with importing them in the notebook. I accidentally deleted (rm) these .py files. However, they are still imported in jupyter notebook. Can I restore the code that exists in these .py files from jupyter notebook.

Details with example:

I had parser.py file.

I imported the class that I was going to use

from parser import Parser

I can still user Parser class, however, parser.py is gone. Hence, whenever I will stop this notebook. I will lose Parser forever...

  • If the byte compiled file is still present you may be able to reconstruct it, see here: https://stackoverflow.com/q/56817475/3279716 – Alex Oct 15 '21 at 21:47
  • Unfortunately this post did not help. I am still able to create new objects. So, notebook still knows what was happening in that class. Therefore, I believe there should be a way to get the source code of this class from jupyter notebook... but how... – Fatih Beyhan Oct 15 '21 at 22:24
  • You should look for a folder called `__pycache__` in there you might have a `parser.pyc`. You may be able to use a [python decompiler](https://stackoverflow.com/questions/5287253/is-it-possible-to-decompile-a-compiled-pyc-file-into-a-py-file) – Alex Oct 15 '21 at 22:33

1 Answers1

-1

If you can continue to modify the notebook where you have parser imported, you can try:

import inspect
print(inspect.getsource(parser))

This should print the contents of the module that was loaded when you originally imported Parser from parser.py. If that doesn't work, try replacing parser with Parser.

Taken from this answer on StackOverflow.

Da Chucky
  • 781
  • 3
  • 13