在 Emacs 中获取 pdb 以使用来自当前 virtualenv 的 Python 进程 [英] Getting pdb in Emacs to use Python process from current virtualenv

查看:19
本文介绍了在 Emacs 中获取 pdb 以使用来自当前 virtualenv 的 Python 进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pdb 在 emacs 中调试一些 python 代码并遇到一些导入问题.依赖项安装在我定制的 virtualenv 环境之一中.

Pdb 顽固地使用/usr/bin/python 而不是我的 virtualenv 中的 python 进程.

我使用 virtualenv.el 来支持在 emacs 内和通过

中描述的 postactivate 钩子切换环境

http:///jesselegg.com/archives/2010/03/14/emacs-python-programmers-2-virtualenv-ipython-daemon-mode/

这在运行 M-x python-shell 时效果很好

<预><代码>>>>导入系统>>>打印 sys.path

这指向我所有的 virtualenv 库,表明 python-shell 是我的 virtualenv 的.

然而这与 M- 相矛盾!which python,它给 /usr/bin/python

有谁知道我如何告诉 M-x pdb 从当前活动的 virtualenv 中采用 python 进程?

解决方案

python-shell 使用变量 python-default-interpreter 来确定使用哪个 python 解释器.当这个变量的值为cpython时,会参考变量python-python-commandpython-python-command-args来确定口译员和要使用的参数.这两个变量由 virtualenv.el 操作来设置当前的虚拟环境.

因此,当您使用 python-shell 命令时,它可以毫无问题地使用您的虚拟环境.

但是,当您执行 M-! python 时,您没有使用变量 python-python-commandpython-python-command-args.所以它使用它在你的路径中找到的 python 工具.

当你调用 M-x pdb 时,它使用 gud-pdb-command-name 作为默认的 pdb 工具.要重新定义此变量,每次激活环境时,您可以执行以下操作:

(defadvice virtualenv-activate (在 virtual-pdb 之后)(自定义设置变量'(gud-pdb-命令名(concat virtualenv-active "/bin/pdb" ))))(ad-activate 'virtualenv-activate)

要在您的虚拟环境中使用 pdb,请执行以下操作:

cp/usr/bin/pdb/path/to/virtual/env/bin

然后编辑/path/to/virtual/env/bin/pdb 的第一行:

#!/usr/bin/env python

重新激活您的 env,Pdb 现在应该使用您的 virtualenv python 而不是系统范围的 python.

I am debugging some python code in emacs using pdb and getting some import issues. The dependencies are installed in one of my bespoked virtualenv environments.

Pdb is stubbornly using /usr/bin/python and not the python process from my virtualenv.

I use virtualenv.el to support switching of environments within emacs and via the postactivate hooks described in

http://jesselegg.com/archives/2010/03/14/emacs-python-programmers-2-virtualenv-ipython-daemon-mode/

This works well when running M-x python-shell

>>> import sys
>>> print sys.path 

This points to all of my virtualenv libraries indicating that the python-shell is that of my virtualenv.

This is contradicted however by M-! which python, which gives /usr/bin/python

Does anyone know how I can tell M-x pdb to adopt the python process from the currently active virtualenv?

解决方案

python-shell uses variable python-default-interpreter to determine which python interpreter to use. When the value of this variable is cpython, the variables python-python-command and python-python-command-args are consulted to determine the interpreter and arguments to use. Those two variables are manipulated by virtualenv.el to set the current virtual environment.

So when you use python-shell command, it uses your virtual environments without any problem.

But, when you do M-! python, you're not using the variables python-python-command and python-python-command-args. So it uses the python tools it finds in your path.

When you call M-x pdb it uses gud-pdb-command-name as the default pdb tool. To redefine this variable, each time you activate an environment, you could do something like this :

(defadvice virtualenv-activate (after virtual-pdb)
  (custom-set-variables
     '(gud-pdb-command-name
        (concat virtualenv-active "/bin/pdb" ))))

(ad-activate 'virtualenv-activate)

To have pdb in your virtual environment, do the following :

cp /usr/bin/pdb /path/to/virtual/env/bin

Then edit the first line of /path/to/virtual/env/bin/pdb to have :

#! /usr/bin/env python

Reactivate your env and Pdb should now use your virtualenv python instead of the system-wide python.

这篇关于在 Emacs 中获取 pdb 以使用来自当前 virtualenv 的 Python 进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆