fjames 0 Posted January 28, 2021 I was wondering if there is a way to prevent any external DLL calls from closing the Application. I ran into an issue with matplotlib raising an error, and after the error, the Application closed, and I cannot find a way of 'catching' the exception to prevent the crash. Stepping through the execution it seems that the call to PyRun_String in TPythonEngine.Run_CommandAsObjectWithDict never returns, and the application is terminated, and would like a way of preventing this. I have been able to reproduce this problem in Demo01 by performing the following steps: Install Anaconda 32bit Create new environment (Python 3.6.12) and install matplotlib using 'conda install matplotlib' in the new environment Start Demo01 in the new environment Can either start Demo01.exe from activated cmd prompt, or just open the IDE from an activated cmd prompt that way you can debug in the environment as well Change script to simple: 'from matplotlib import pyplot as plt; plt.plot(range(10)); plt.show()' What results is a popup much like the one here. After clicking OK, the program exits Its worth noting, that I was able to overcome the above issue by following the steps in this answer, however I foresee more issues like this in the future, and I would like some way of preventing my application from crashing if one of Python's third party modules encounters an error. Thanks for any advice! Share this post Link to post
David Heffernan 2345 Posted January 28, 2021 Any code can terminate the process. The way you avoid this is to ensure that the code you run doesn't lead to fatal errors. In this case it sounds like the issue is that you need to make sure that your environment has the correct packages installed. Share this post Link to post
fjames 0 Posted January 28, 2021 That is the answer I was afraid of. That means crashes like this are almost impossible to prevent. Making "sure that your environment has the correct packages installed" is very tricky it seems. For example in the matplotlib example, matplotlib seems to be installed correctly and only produces error calling 'show()', and actually if I start python directly from the activated cmd prompt, matplotlib executed successfully, so the problem was resulting from some dlls not being in the correct place when being run from within a Delphi Application (but installing qt and pyqt put the dlls in a place that works from both within and outside a Delphi application). So to properly ensure the environment was correct, I would need to know all the dlls needed by each python module, and ensure they are in the place that the module is expecting. I guess if you are willing to allow the importing of third-party modules, this is a risk you have to take. Thanks for the quick response! Share this post Link to post
Bob Devine 11 Posted January 29, 2021 You need to use RPyC to run your Python code - this decouples the Python from your main app. Check the source in PyScripter to see how it's done. There's code for internal interpreter and remote interpreter - you need the code in the latter. Share this post Link to post
Fr0sT.Brutal 900 Posted January 29, 2021 Exception raised by a DLL function will crash the app. So you can ask DLL developers /change DLL yourself to handle all exceptions. Share this post Link to post
Der schöne Günther 316 Posted January 29, 2021 Running the code in an auxiliary process instead of your own could also be an option if you cannot fix the cause and only battle the symptoms. 1 Share this post Link to post
David Heffernan 2345 Posted January 29, 2021 1 hour ago, Der schöne Günther said: Running the code in an auxiliary process instead of your own could also be an option if you cannot fix the cause and only battle the symptoms. That's what RPyC would gain you 1 Share this post Link to post
pyscripter 689 Posted February 7, 2021 Sometimes it helps to trap the SystemExit exception. This would avoid many abnormal application exits. try: """ Add your Python code here""" except SystemExit as e: 1 Share this post Link to post