Debugging
Whenever you create a SweepMe! driver or a CustomFunction script, you might need to access error messages to debug your code. For this reason, the Debug widget can be shown in the Dashboard; it prints all messages. In general, all Python errors in SweepMe! that are not already caught and handled are shown in this widget.
Debug widget
SweepMe! redirects all error and print messages to the Debug widget, which can be opened with the key 'F2' or via the menu 'Widgets' -> 'Debug'. The Debug widget is a permanent widget of the Dashboard window: it cannot be removed, only hidden. If you cannot see it, press 'F2' to show it again.
debug.log
All debug messages are also saved to the file 'debug.log' in the public SweepMe! folder, by default C:\Users\Public\Documents\SweepMe!\debug.log (see Folder system). The Debug widget has a button "Open file" in its toolbar to open the file.
This file can be used to create a support ticket. Please make sure that it contains no personal information. Whenever SweepMe! starts, the file debug.log is overwritten, so make a copy of it before you restart SweepMe!.
If multiple instances of SweepMe! are open, the first instance writes to debug.log and each additional instance writes its own file with the process id in the file name, e.g. debug_instance12345.log.
Program startup
The debug.log also contains all error messages that occur during the startup of your program. Thus, if SweepMe! does not start correctly, you can check the error messages using the debug file. If SweepMe! cannot start at all, it writes the file startup_error.log to the folder C:\Users\Public\Documents\SweepMe! and opens it.
Alternatively, you can start SweepMe! by opening the Windows command line in the SweepMe! installation folder and typing "SweepMe!.exe". The option '-d' results in further debug messages that might help us to identify your problem if you create a support ticket. In this case, use "SweepMe!.exe -d" (see Command line interface).
Programming
Whenever you create a SweepMe! instrument driver or a CustomFunction script or enter a formula into the Calc module, you might need to debug your code or your program flow.
Basically, all error messages are forwarded to the Debug widget. Here are further strategies that might help you:
- If the code has an Exception, it will be shown in the Debug widget. The Exception is shown with the full traceback so that you should be able to identify the line number in your code that makes trouble. The traceback typically containes all lines of code that have been called to raise the Exception. Thus, you carefully need to read each line of the traceback to follow the Exception until you find the origin of the Exception. Often, the last line of the traceback is the important one.
- For simple scripts it is often sufficient to add print(...) statements to your code to show the value of certain variables in the Debug widget. Any print-statement inside your SweepMe! driver will lead to an output via the the Debug widget. Thus, you can output whatever is needed to debug your code. Whenever, your driver is used, e.g. by running a measurement or pushing buttons like 'Retrieve' or "Apply', SweepMe! checks whether the code of the driver or script has been changed and reloads the driver. A restart of SweepMe! is not needed.
- SweepMe! comes with Pnotepad as an editor to open drivers and scripts. With SweepMe! Pro, you can change the editor via menu 'File' -> 'Options' -> 'Paths (Pro)' -> 'Change editor'. Here, you can select a more advanced IDE like PyCharm or Visual Studio Code that often come with additional code checking features in case you have installed it beforehand.
- Advanced debugging features require to run the code in an own Python environment. SweepMe! itself does not come with advanced Debugging features. In case you are working on a more complex instrument driver or CustomFunction script, you can install the same Python environment as used by SweepMe!. We recommend to use the same Python version as SweepMe!, i.e. Python 3.9 (64 bit) for SweepMe! 1.5 and Python 3.14 (64 bit) for SweepMe! 1.6. The requirements.txt file of SweepMe! can be found in the libs folder of your SweepMe! installation and be used with pip to install necessary Python packages in the correct combination of versions. Now, you are able to use our open-source library pysweepme to run a SweepMe! driver in your Python environment and use all debugging features from your IDE such as investigating/modifying the values of variables during runtime, or stopping the code during interpretation.
- To see every command that your driver sends to and receives from an instrument, set
self.port_properties["debug"] = Truein the driver, or check the box in the column "Debug" of the port in the Port Manager (menu 'Tools' -> 'Port Manager'). All write and read commands of this port are then printed to the Debug widget. See Port manager.
ErrorMessage
You can import the module ErrorMessage from the pysweepme package that comes with SweepMe!. It provides two convenience functions called 'error' and 'debug' that can be used to output the last error traceback or debug messages with timestamp.
from pysweepme.ErrorMessage import error, debug
Usage
from pysweepme.ErrorMessage import error
error("<your message>")
will print <your message> and the last Exception that has been raised. It is typically used in combination with try-except
from pysweepme.ErrorMessage import debug
debug("<your message>")
will print <your message> starting with a time stamp
With debug("<your message>", debugmode_only=True), the message is only printed if SweepMe! was started in debug mode (option -d, see Command line interface).