Debugging python-fu scripts in Windows - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Tutorials and tips (https://www.gimp-forum.net/Forum-Tutorials-and-tips) +--- Thread: Debugging python-fu scripts in Windows (/Thread-Debugging-python-fu-scripts-in-Windows) |
Debugging python-fu scripts in Windows - Ofnuts - 03-13-2017 Debugging python-fu scripts As Brian Kernighan wrote, "The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.". On Linux (and likely OSX) things are easy: if you start Gimp from a terminal, all the output from print statements in Python plugins will be displayed in the terminal. But on Windows, the output of plain print statements cannot be seen, so you have two options: Using gimp_message() This is a simple method which is OK to diagnose small and local problems, but it is better used on code which is known to be mostly working.
This method is a bit more complex, but all the output is visible, even from parts of Python that you don't control (error messages, etc...) We just have to overcome a few hurdles, namely:
Code: import sys;
Code: # retrieve the image (if you have several images, it will be a bit more complicated)
Code: reload(plugin)
Routing python outputs to file (this method inspired by a post by SeldomNeedy on the Gimp user mailing list) This is a rather simple method, which is best used with an editor clever enough to reload the file when it notices changes. With this method, everything Python would write on the console (print statements, error messages... ) goes to a file, that you can check with a text editor. At the beginning of the file, do: Code: import sys Other locations are possible: Code: sys.stderr = open(gimp.directory+'/../My Documents/python-fu-output.txt','a') (contrary to popular belief, forward slashes are usable as path separators on Windows, only the command prompt parser doesn't like them). When you are done with debugging, you can disable all the tracing by simply replacing the initial file opening. Code: Code: import os os.path.devnull is the right "bit bucket" for the platform the code is running on, so this makes your plugin compatible with Windows, Linux, and OSX. RE: Debugging python-fu scripts in Windows - Kevin - 03-13-2017 One advantage of the stderr re-direction to a file is that you can then get the error messages from registration: Code: Traceback (most recent call last): |