Mars Lee Mars Lee - 7 months ago 11
Python Question

How to make Python executable pause when it raises an error?

So I know I can make Python executable using

pyinstaller
.

However, every time it raises an error, it will instantly end the program, so I can't find what is the error.

I know I probably can use
time.sleep(30000)
to stop it.

But if the code raises error before it meets
time.sleep(30000)
, it will just shut down.

To sum up, how to make it keep not shutting down, so I can see where is the mistake?

Answer

If you are running the executable just by double clicking, try running it from console. For e.g. in Windows:

In cmd, run

cd executable_path myexecutable.exe

A better way would be to introduce logger so that other modules with logging implemented can write to files.

Also, you can put the code in try-except block:

try:
    #my_code_here
    print('Here is where the exception is')
except Exception as e:
    print('Unexpected error:' + str(e))