Methods to debug Ubuntu Nautilus scripts written in Python
Asked Answered
C

5

7

When writing a Nautilus Script (e.g. using Python), I currently am aware of two methods for basic debugging:

  1. Using (e.g. Zenity) pop-up windows as "print" statements.
  2. Capturing stderr and stdout in text files for later reference.

These methods work reasonably well, but I suspect that there are more effective methods that I am not aware of. Can anyone suggest other methods?

Edit:

Context: I sought methods to debug a python script launched from the Ubuntu right click menu in Nautilus.

Using cedric's answer of relaunching nautilus with --no-desktop option means that stderr and stdout becomes visible at the terminal window. However, this terminal does not appear to be usable as a pdb console for debugging (it seems to be output-only).

In my search for a way to get input and output access to the script process when launched from the right click menu, I found the information listed in my answer below.

(Also, while I agree that unit testing and logging is best practice, I would argue that there is still a place for interactive debugging, particularly with small scripts.)

Casein answered 17/12, 2010 at 20:26 Comment(1)
If you can't use pdb, the standard Python debugger, you might be able to use IPython.Hemato
B
10

What you asked for is just to see your script outputs, that can be done by relaunching nautilus with --no-desktop option:

$ nautilus -q
$ nautilus --no-desktop

This way you will see any outputs (stderr / stdout) made by your script or by the python interpreter. Should be usefull...

Bauble answered 3/1, 2011 at 12:52 Comment(1)
thanks for the nautilus --no-desktop, very useful !!Speroni
A
3
  1. A debugger (pdb or Winpdb)

  2. Use python's logging module

  3. Use a debug decorator - see http://paulbutler.org/archives/python-debugging-with-decorators/

  4. More useful tips at How would you write a @debuggable decorator in python?

Altagraciaaltaic answered 17/12, 2010 at 23:3 Comment(0)
P
3

As a trick to see the output and also interact with the script in a terminal, I split my script in two files (see question How to execute a nautilus script written in Python inside a gnome-terminal window that stays open?):

  • ~/.local/share/nautilus/scripts/firstfile.sh which opens a terminal, executes the script in it, and leave it open:

    #!/bin/bash
    gnome-terminal -- bash -c "python3 ~/.local/share/nautilus/scripts/.secondfile.py; bash"
    
  • ~/.local/share/nautilus/scripts/.secondfile.py which contains the actual Python script and is hidden from nautilus script menu:

    #!/usr/bin/python3
    print("Hello")
    

Of course this is just for basic debugging but it might be useful to someone.

Proscription answered 22/8, 2017 at 11:8 Comment(0)
C
2

After searching for a way to get interactive debugging for a python script launched from the scripts sub-menu of the Nautilus right-click menu, I found the following solution.

One can use WingIDE to listen for and connect to external processes. This makes it possible to use the WingIDE debugging capabilities for externally-launched code such as my Python Nautilus script.

I just needed to turn on the WingIDE preference "Enable Passive Listen", then copy wingdbstub.py to the script directory. I then added "import wingdbstub" in the script and set a breakpoint in the script code, opened in Wing.

When I ran the script from the Nautilus right-click menu, the process was connected to WingIDE and I was able to use all of the WingIDE debugging abilities.

Details here: 5.12 Debugging Externally Initiated Processes .

Casein answered 9/1, 2011 at 3:54 Comment(0)
K
1

Unit-test your scripts using PyUnit.

This will be more effective than 'print' statements and will be repeatable to help you prevent regressions. It will also reduce the risk of temporary debugging code being left in your script.

Kenwrick answered 17/12, 2010 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.