使用pydev调试

pdb 是调试Python脚本的好工具,但它的实用性有一些限制。例如,您必须修改代码以插入断点,并且它的命令行接口可能有点钝。

许多开发人员使用自定义文本编辑器,允许他们向基本命令行环境添加包装器,并支持Git和其他开发工具。然而,在许多情况下,调试支持基本上只是一个围绕基本 pdb 功能。

PyDev 是一个 Eclipse python语言的插件,提供集成开发环境,包括内置的python解释器、Git支持、与任务管理的集成以及其他有用的开发功能。

pydev调试器允许您在不修改源代码以设置断点的情况下执行代码,并且具有一个允许您检查和修改内部状态的GUI接口。

Lars Vogella has provided a clear tutorial 关于设置pydev和开始使用pydev调试器。可以找到有关使用pydev调试器的完整文档 here . You can also debug programs not running under Eclipse using the Remote Debugging 特征。

pydev允许您将系统配置为使用您在计算机上安装的任何python intepreter,并且通过适当的配置,您可以同时支持2.x和3.x语法。

为virtualenv配置pydev

Most of the time you want to be running your code in a virtualenv in order to be sure that your code is isolated and all the right versions of your package dependencies are available. 你可以 pip install virtualenv if you like, but I recommend virtualenvwrapper 这就消除了设置virtualenv的许多繁琐工作。

PyDev will look through all the libraries on your PYTHONPATH 要解决所有外部引用(如导入等),您需要在 PYTHONPATH 避免不必要的名称解析问题。

要将pydev与virtualenv结合使用,需要一些上面教程中没有介绍的额外配置。基本上,您只需要确保您的virtualenv库位于 PYTHONPATH .

备注

如果您从未为工作区配置过python解释器,那么您将无法在不这样做的情况下创建项目。您应该按照下面的步骤配置python,但不应该为它包含任何virtualenv库。然后,您将能够使用这个主要的Python解释器创建项目。创建项目之后,您应该按照下面的步骤为您的项目配置一个新的解释器,该解释器包含virtualenv库。这样,每个项目都可以与特定的virtualenv关联,而不会产生混淆。

First, open the project properties by right clicking over the project name and selecting 属性.

在“属性”对话框中,选择 pydev-解释器/语法, 确保项目类型 Python 被选中。Click on the "Click here to configure an interpreter not listed" link. 这个 Preferences 对话框将出现 Python Interpreters 第页,选择了当前的解释器。单击*新建…*按钮。

Enter a name (e.g. pytest_python )并浏览到virtualenv bin目录(例如 ~/.virtual_envs/pytest/bin/python ) to select the python interpreter in that location, then select OK.

A dialog will then appear asking you to choose the libraries that should be on the PYTHONPATH . 大多数必需的库都应该自动选择。击中 OK, 现在已经配置了virtualenv python。

备注

在Mac上,未选择系统库。全部选择。

You will finally be back on the dialog for configuring your project python interpreter/grammar. Choose the interpreter you just configured and click OK. 此时还可以选择语法级别(2.7、3.0等)。

此时,以前对安装在virtualenv中的库的未解析引用将不再作为错误调用。(在新的解释器生效之前,您必须关闭并重新打开任何python模块。)

还请记住,在使用pydev控制台时,要选择与项目关联的解释器,以便正确解析控制台中的引用。

Running/Debugging Pyramid under Pydev

(这多亏了迈克尔·威尔逊——看 Setting up Eclipse (PyDev) for Pyramid

备注

This section assumes you have created a virtualenv with Pyramid installed, and have configured your PyDev as above for this virtualenv. We further assume you are using virtualenvwrapper (见上文)因此 $WORKON_HOME 是你的位置吗 .virtualenvs 目录和 proj_venv is the name of your virtualenv. $WORKSPACE 是包含项目的pydev工作区的名称

要创建工作示例,请复制 pyramid tutorial step03 代码到$workspace/tutorial。

复制代码后,CD到 $WORKSPACE/tutorial 并运行 python setup.py develop

现在应该可以设置pydev来运行教程step03代码了。

我们将设置pydev以作为运行或调试配置的一部分运行pserver。

第一,复制 pserve.py 从您的VielalEnv到项目库路径之外的位置:

$ cp $WORKON_HOME/proj_venv/bin/pserve.py $WORKSPACE

备注

重要提示:不要将其放在项目库路径中!

现在我们需要让pydev默认运行这个。要创建新的运行配置,请右键单击项目并选择 Run As -> Run Configurations.... Select Python Run as your configuration type, and click on the new configuration icon. Add your project name (or browse to it), in this case "tutorial".

Add these values to the Main tab:

  • 项目: RunPyramid

  • 主要模块: ${{workspace_loc}}/pserve.py

Add these values to the Arguments tab:

  • Program arguments: ${{workspace_loc:tutorial/development.ini}} --reload

备注

不加 --reload 如果您尝试使用Eclipse进行调试。据报道,这会引起问题。

We recommend you create a separate debug configuration without the --reload ,而不是在“显示在收藏夹菜单”中选中“运行”,而是选中“调试”。

Common tab:

  • 取消选中“在后台启动”

  • 在标记为“在收藏夹菜单中显示”的框中,选中“运行”

击中 Rundebug)立即运行(调试)您的配置,或 Apply 创建配置而不运行它。

You can now run your application at any time by selecting the Run/Play button and selecting the RunPyramid 命令。同样,您可以通过选择 Debug button and selecting the DebugPyramid command (or whatever you called it!).

控制台应该显示服务器已经启动。要验证,请打开浏览器至127.0.0.1:6547。您应该看到Hello World文本。

请注意,在调试时,可以将断点设置为普通代码,但只有在提供包含断点的视图时才会命中这些断点。