我可以让 Python 调试器 pdb 以颜色输出吗?

发布于 2024-09-14 21:53:11 字数 128 浏览 6 评论 0原文

我经常使用 PDB,如果我可以添加 systax 颜色突出显示,似乎会更好。

理想情况下,我希望代码的路径颜色更浅。 实际代码行将被语法突出显示。

我正在使用 OS X 和终端应用程序。 Python 2.7

I'm using PDB a lot and it seems it would be even better if I could add systax highlighting in color.

Ideally, I'd like to have to the path to the code a lighter color.
The line of actual code would be syntax highlighted.

I'm using OS X and the Terminal app.
Python 2.7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

零時差 2024-09-21 21:53:11

pdb 不支持着色。然而,获得它并不难,即使您是一个命令行迷(就像我一样;-)——您不必为了在调试 Python 时获得着色而切换到 GUI/IDE。特别是,当您通过 SSH 访问远程计算机时,命令行工具通常可以更好地工作,从而节省大量带宽和延迟问题,而这些问题是对 GUI 和 IDE 的远程访问可能给您造成的。 ;-)。

具体来说,对于您询问的任务,请考虑 ipdb (您还需要 ipython,它提供了比普通交互式 Python 更高级的 shell,ipdb< /code> 依赖)。两者都为您提供了良好的制表符补全、增强的回溯、着色 - ipython 用于您的正常交互工作,ipdb 具有相同的功能,当您重新调试(否则与 pdb 几乎相同)。

pdb doesn't support colorization. However, it's not that hard to get it, even if you're a command-line addict (as I am;-) -- you don't have to switch to GUIs/IDEs just to get colorization while debugging Python. In particular, command-line tools usually work much better when you're accessing a remote machine via SSH, saving a lot of the bandwidth and latency issues that any remote access to GUIs and IDEs can inflict on you;-).

Specifically, for the task you're asking about, consider ipdb (you also need ipython, which offers a far more advanced shell than plain interactive Python, on which ipdb relies). Both offer you good tab completion, enhanced tracebacks, and colorization -- ipython for your normal interactive work, ipdb with the same features when you're debugging (otherwise just about the same as pdb).

゛时过境迁 2024-09-21 21:53:11

看看 pdb++ - 它是 pdb 的直接替代品,可以满足您的所有要求并添加其他一些不错的功能,例如制表符完成和新命令,例如监视和粘性。

这是一个启用颜色的示例配置文件(在创建文件后添加此文件:touch ~/.pdbrc.py):

import pdb

class Config(pdb.DefaultConfig):
    use_pygments = True
    pygments_formatter_class = "pygments.formatters.TerminalTrueColorFormatter"
    pygments_formatter_kwargs = {"style": "monokai"}

Take a look at pdb++ - it is a drop-in replacement for pdb that fills all your requirements and adds some other nice features such as tab completion and new commands such as watch and sticky.

Here is a sample config file that will enable colours (add this after creating the file: touch ~/.pdbrc.py):

import pdb

class Config(pdb.DefaultConfig):
    use_pygments = True
    pygments_formatter_class = "pygments.formatters.TerminalTrueColorFormatter"
    pygments_formatter_kwargs = {"style": "monokai"}
错爱 2024-09-21 21:53:11

您可以尝试 pudb,它在终端中工作,如下所示:

pudb snapshot

我还没有尝试过其他答案中提到的一些选项,但从 PyPI 页面判断,pudb 维护得更好,文档也更好。

You could try pudb, which works in the terminal and looks like this:

pudb screenshot

I haven't tried some of the options mentioned in other answers, but to judge from the PyPI pages, pudb is better maintained and better documented.

北方。的韩爷 2024-09-21 21:53:11

这对您来说可能是不可能的,但是您是否尝试过使用图形调试器(例如 eclipse/pydev 中的调试器)?它将为您提供语法突出显示等功能。

如果没有选择,我只会直接使用 pdb,因为图形界面要好得多。

This might not possible for you, but have you tried using a graphical debugger (like the one in eclipse/pydev)? It will give you your syntax highlighting and much more.

I only use pdb directly if I don't have an option, because a graphical interface is just that much nicer.

清醇 2024-09-21 21:53:11

万一有人在控制台中遇到着色问题。

我的控制台有白色背景,而 ipdb 也在语法中添加了相当浅的颜色(例如变量是白色的)。按 man ipython 显示我们有 3 种颜色可用:“nocolor”、“linux”、“LightBG”。在我的例子中,Ipdb 通过 easy_install 安装到我的 virtualenv 中。因此,查看 ipdb 源代码并对其进行修改是很简单的(提示在您的环境中搜索 ipdb/init.py)。然后我修改了以下内容:

def set_trace():
    ip = ipapi.get()
    + def_colors = ip.options.colors
    + def_colors = 'LightBG'
    Pdb(def_colors).set_trace(sys._getframe().f_back)

这是一个有点黑客的解决方案,但它用于在我的工作站上进行调试,所以它足够了。但如果有人发现更好的东西。请给我留言告诉我该怎么做。

In case someone hit the problem with colorization in a console.

My console had white background while ipdb was also adding rather light colors to syntax (for example variables were white). Pressing man ipython shows that we have 3 colors available: 'nocolor', 'linux', 'LightBG'. Ipdb was in my case installed via easy_install into my virtualenv. So it was trivial to look into ipdb source and modify it (hint search for ipdb/init.py in your env). Then I've modified following:

def set_trace():
    ip = ipapi.get()
    + def_colors = ip.options.colors
    + def_colors = 'LightBG'
    Pdb(def_colors).set_trace(sys._getframe().f_back)

It's a kinda hackish solution but well its for debugging purpose on my working station so its sufficient. But if anyone finds something better. Please send me a message on what to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文