简体   繁体   English

调试用 Python 编写的 Ubuntu Nautilus 脚本的方法

[英]Methods to debug Ubuntu Nautilus scripts written in Python

When writing a Nautilus Script (eg using Python), I currently am aware of two methods for basic debugging:在编写Nautilus 脚本(例如使用 Python)时,我目前知道两种基本调试方法:

  1. Using (eg Zenity) pop-up windows as "print" statements.使用(例如 Zenity)弹出窗口作为“打印”语句。
  2. Capturing stderr and stdout in text files for later reference.在文本文件中捕获 stderr 和 stdout 以供以后参考。

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.上下文:我寻求调试从 Nautilus 中的 Ubuntu 右键菜单启动的 python 脚本的方法。

Using cedric's answer of relaunching nautilus with --no-desktop option means that stderr and stdout becomes visible at the terminal window.使用 cedric 使用 --no-desktop 选项重新启动 nautilus 的答案意味着 stderr 和 stdout 在终端窗口中变得可见。 However, this terminal does not appear to be usable as a pdb console for debugging (it seems to be output-only).但是,此终端似乎不能用作调试的 pdb 控制台(它似乎仅用于输出)。

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.) (另外,虽然我同意单元测试和日志记录是最佳实践,但我认为仍然存在交互式调试的地方,尤其是小脚本。)

What you asked for is just to see your script outputs, that can be done by relaunching nautilus with --no-desktop option:您要求的只是查看您的脚本输出,这可以通过使用 --no-desktop 选项重新启动 nautilus 来完成:

$ nautilus -q
$ nautilus --no-desktop

This way you will see any outputs (stderr / stdout) made by your script or by the python interpreter.通过这种方式,您将看到脚本或 python 解释器生成的任何输出(stderr / stdout)。 Should be usefull...应该有用...

  1. A debugger (pdb or Winpdb)调试器(pdb 或 Winpdb)

  2. Use python's logging module使用 python 的日志记录模块

  3. Use a debug decorator - see http://paulbutler.org/archives/python-debugging-with-decorators/使用调试装饰器 - 请参阅http://paulbutler.org/archives/python-debugging-with-decorators/

  4. More useful tips at How would you write a @debuggable decorator in python?更多有用的提示在如何在 python 中编写 @debuggable 装饰器?

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? ):作为查看输出并与终端中的脚本进行交互的一个技巧,我将脚本拆分为两个文件(请参阅问题如何在 gnome 终端窗口内执行用 Python 编写的 nautilus 脚本并保持打开状态? ):

  • ~/.local/share/nautilus/scripts/firstfile.sh which opens a terminal, executes the script in it, and leave it open: ~/.local/share/nautilus/scripts/firstfile.sh打开一个终端,在其中执行脚本,并保持打开状态:

     #!/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: ~/.local/share/nautilus/scripts/.secondfile.py包含实际的 Python 脚本并从nautilus 脚本菜单中隐藏:

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

Of course this is just for basic debugging but it might be useful to someone.当然,这只是用于基本调试,但它可能对某些人有用。

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.在为从 Nautilus 右键单击​​菜单的脚本子菜单中启动的 python 脚本寻找交互式调试的方法后,我找到了以下解决方案。

One can use WingIDE to listen for and connect to external processes.可以使用 WingIDE 侦听并连接到外部进程。 This makes it possible to use the WingIDE debugging capabilities for externally-launched code such as my Python Nautilus script.这使得可以将 WingIDE 调试功能用于外部启动的代码,例如我的 Python Nautilus 脚本。

I just needed to turn on the WingIDE preference "Enable Passive Listen", then copy wingdbstub.py to the script directory.我只需要打开 WingIDE 首选项“启用被动监听”,然后将 wingdbstub.py 复制到脚本目录。 I then added "import wingdbstub" in the script and set a breakpoint in the script code, opened in Wing.然后我在脚本中添加了“import wingdbstub”,并在脚本代码中设置了一个断点,在 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.当我从 Nautilus 右键单击​​菜单运行脚本时,该进程已连接到 WingIDE,并且我能够使用所有 WingIDE 调试功能。

Details here: 5.12 Debugging Externally Initiated Processes .此处的详细信息: 5.12 调试外部启动的进程

Unit-test your scripts using PyUnit.使用 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.它还可以降低临时调试代码留在脚本中的风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM