Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to access variables after an error when running Python

I am using Pycharm Community 4.5.4. I am running a program that takes a very long time to complete.

Unfortunately, I have a print statement that comes before the line that saves the data accumulated which has an error. I ran the print line by itself and confirmed that the line will fail.

It is possible that I made the change after I ran the program and so it won't fail but I can't be sure. The portion of code takes about 90 hours to complete and I have about 30 left if the execution time stays around the average.

So, if it fails, is there any way to recover the data?

Just to be clear, I am running and not debugging. Also, I am line profiling to see how each 2.5 hour iteration's time is spent.

like image 993
Chris Avatar asked Feb 18 '26 20:02

Chris


1 Answers

So I told, you shoud update your main module to exit on the breakpoint

import pdb
import random
import time
import os
import traceback
import sys


def main(*args, **kw):
    for i in range(10):
        print("OK I'm in %d" % os.getpid())
        time.sleep(4)
        if i == 9:
            raise Exception

if __name__ == '__main__':
    try:
        main()
    except:
        type, value, tb = sys.exc_info()
        traceback.print_exc()
        pdb.post_mortem(tb)

Starting python debugger automatically on error Credit.

And use Pycharm attach to process. if an error is raised so you will be able to debug with pycharm.

like image 65
Ali SAID OMAR Avatar answered Feb 20 '26 08:02

Ali SAID OMAR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!