调试用 Python 编写的 Ubuntu Nautilus 脚本的方法
在编写 Nautilus 脚本(例如使用 Python)时,我目前知道两种方法基本调试:
- 使用(例如 Zenity)弹出窗口作为“打印”语句。
- 在文本文件中捕获 stderr 和 stdout 以供以后参考。
这些方法效果相当好,但我怀疑还有我不知道的更有效的方法。有人可以建议其他方法吗?
编辑:
上下文:我寻找调试从 Nautilus 中的 Ubuntu 右键菜单启动的 python 脚本的方法。
使用 cedric 的使用 --no-desktop 选项重新启动 nautilus 的答案意味着 stderr 和 stdout 在终端窗口中变得可见。但是,该终端似乎不能用作调试的 pdb 控制台(它似乎仅输出)。
在我寻找一种从右键单击菜单启动时获取对脚本进程的输入和输出访问的方法时,我找到了下面答案中列出的信息。
(另外,虽然我同意单元测试和日志记录是最佳实践,但我认为仍然存在交互式调试的空间,特别是对于小脚本。)
When writing a Nautilus Script (e.g. using Python), I currently am aware of two methods for basic debugging:
- Using (e.g. Zenity) pop-up windows as "print" statements.
- 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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您所要求的只是查看脚本输出,这可以通过使用 --no-desktop 选项重新启动 nautilus 来完成:
这样您将看到脚本或 python 解释器生成的任何输出(stderr / stdout)。应该有用...
What you asked for is just to see your script outputs, that can be done by relaunching nautilus with --no-desktop option:
This way you will see any outputs (stderr / stdout) made by your script or by the python interpreter. Should be usefull...
调试器(pdb 或 Winpdb)
使用 python 的日志记录模块
使用调试装饰器 - 请参阅http://paulbutler.org/archives/python-debugging-with-decorators/
更多有用的提示,请访问如何在 python 中编写 @debuggable 装饰器?
A debugger (pdb or Winpdb)
Use python's logging module
Use a debug decorator - see http://paulbutler.org/archives/python-debugging-with-decorators/
More useful tips at How would you write a @debuggable decorator in python?
作为查看输出并与终端中的脚本交互的技巧,我将脚本分成两个文件(请参阅问题如何执行在保持打开状态的 gnome 终端窗口内用 Python 编写的 nautilus 脚本?):
~/.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
包含实际的 Python 脚本,并且在 nautilus 脚本菜单中隐藏:当然这只是为了基本的调试,但它可能对某人有用。
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:~/.local/share/nautilus/scripts/.secondfile.py
which contains the actual Python script and is hidden from nautilus script menu:Of course this is just for basic debugging but it might be useful to someone.
在寻找一种对从 Nautilus 右键菜单的脚本子菜单启动的 python 脚本进行交互式调试的方法后,我找到了以下解决方案。
可以使用 WingIDE 监听并连接到外部进程。这使得可以对外部启动的代码(例如我的 Python Nautilus 脚本)使用 WingIDE 调试功能。
我只需要打开 WingIDE 首选项“启用被动监听”,然后将 wingdbstub.py 复制到脚本目录。然后我在脚本中添加了“import wingdbstub”,并在脚本代码中设置了一个断点,在 Wing 中打开。
当我从 Nautilus 右键菜单运行脚本时,该进程已连接到 WingIDE,并且我能够使用所有 WingIDE 调试功能。
详细信息请参见:5.12 调试外部启动的进程。
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 .
使用 PyUnit 对您的脚本进行单元测试。
这将比“打印”语句更有效,并且可重复以帮助您防止回归。它还会降低脚本中留下临时调试代码的风险。
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.