Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to save the Python interpreter's state to a file?

Tags:

What if, when a user is using my Python application and the application crashes, the state of the application can be saved to a file and sent to me, the developer? I open the Python interpreter and start debugging from the point where the user crashed.


To clarify, when I'm debugging an application and it raises an unhandled exception, I can debug the application post-mortem, getting access to all the local variables and their values which is crucial to quickly fixing bugs. When an user's application crashes though, I only receive the stack trace for when the error occurred, which is helpful, but not nearly as much as debugging interactively would be.

So is it possible to save the state of a Python application to a file, close the interpreter, then resume the execution from that file at a later stage?

like image 205
Hubro Avatar asked Jan 25 '13 09:01

Hubro


People also ask

How do I save a Python shell?

Saving your workRight-click the Python window and select Save As to save your code either as a Python file (. py) or Text file (. txt). If saving to a Python file, only the Python code will be saved.

How do I save an interactive session in Python?

To save a Python interactive session, we can use the readline module. to call readline. write_history_file to save the interactive session content into the /home/ahj/history file.

What is the Python interpreter file?

Python is an interpreter language. It means it executes the code line by line. Python provides a Python Shell, which is used to execute a single Python command and display the result.

Does interpreter helps in executing the Python program?

The interpreter's job is to take these code objects and follow the instructions. You may be surprised to hear that compiling is a step in executing Python code at all. Python is often called an "interpreted" language like Ruby or Perl, as opposed to a "compiled" language like C or Rust.


1 Answers

This tool may help, but you'll need to call the dumper in your code when exception happens. It simply pickles the traceback & frame objects into files

And there's a similar question here.

like image 194
l04m33 Avatar answered Oct 24 '22 21:10

l04m33