Run a Python Script

A Python script can run on the same thread as the user interface of DigitalMicrograph® (the foreground thread), or you can create a new thread (a background thread) to run the script on that. A checkbox at the bottom of the editor window controls this choice.

The default is to run the script on a background thread, because scripts run on the same thread as the DigitalMicrograph's user interface prevent all user interaction with the application during the script run time. It is not possible to interrupt the Python script in this case, and it may become necessary to close DigitalMicrograph to escape from a poorly written script. Some Python packages will not perform correctly if run on a background thread, and may even crash DigitalMicrograph if they are not run on the foreground thread. One example of such a package is the commonly used Matplotlib package. Only one Python script may run at a time. If you attempt to run a second script, DigitalMicrograph will display an error message.

To run a Python script:

  1. Click on the Execute button in the scripting window or select Ctrl-Enter on the keyboard.

The script in the window will be run on either the user interface (foreground thread) or a background thread, depending on the state of the Execute in background checkbox. Only one Python script can be executing at any time.

To interrupt a Python script running on a background thread:

  1. Choose the key combination of Ctrl-Shift-Q.

If there is a Python script running on a background thread it will be stopped. There are times when the Python interpreter may be holding on to the Python Global Interpreter Lock and at these times the DigitalMicrograph cannot stop the running script and will instead present an information dialog explaining the problem.

There is no way to stop or interrupt a Python script that is running in the foreground. If DigitalMicrograph is unresponsive or the script is going to take an unreasonable amount of time to complete, you will have to stop the DigitalMicrograph application through the task manager. It can be helpful to run problem Python scripts in a stand-alone Python interpreter to gain more insight into the problem.