1

当我在本地运行 Django 站点时,ipdb 为我触发了导入错误。我正在使用 Python 2.7 和虚拟环境。

which ipdb显示 path (/usr/local/bin/ipdb)which ipython这让我感到惊讶,因为我认为它应该显示我的 venv 路径(但如果它是全局的,它不应该工作吗?)。所以我尝试pip install --target=/path/to/venv ipdb了,现在它出现了pip freeze(以前没有),但仍然给我一个导入错误。

which pip/Users/myname/.virtualenvs/myenv/bin/pip/

我自己的路: /Users/myname/.virtualenvs/myenv/bin:/Users/myname/.venvburrito/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/myname/bin:/usr/local/bin

系统路径: '/Users/myname/Dropbox/myenv', '/Users/myname/.venvburrito/lib/python2.7/site-packages/pip-1.4.1-py2.7.egg', '/Users/myname/.venvburrito/lib/python2.7/site-packages', '/Users/myname/.venvburrito/lib/python2.7/site-packages/setuptools-8.2-py2.7.egg', '/Users/myname/.virtualenvs/myenv/lib/python27.zip', '/Users/myname/.virtualenvs/myenv/lib/python2.7', '/Users/myname/.virtualenvs/myenv/lib/python2.7/plat-darwin', '/Users/myname/.virtualenvs/myenv/lib/python2.7/plat-mac', '/Users/myname/.virtualenvs/myenv/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/myname/.virtualenvs/myenv/Extras/lib/python', '/Users/myname/.virtualenvs/myenv/lib/python2.7/lib-tk', '/Users/myname/.virtualenvs/myenv/lib/python2.7/lib-old', '/Users/myname/.virtualenvs/myenv/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/myname/.virtualenvs/myenv/lib/python2.7/site-packages']

如果我从终端运行 ipdb,它工作正常。我试过重新启动我的终端。

堆栈跟踪:

Traceback (most recent call last):
  File "/Users/myname/.virtualenvs/myenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/myname/.virtualenvs/myenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/myname/.virtualenvs/myenv/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/myname/.virtualenvs/myenv/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/myname/.virtualenvs/myenv/lib/python2.7/site-packages/django/views/generic/base.py", line 157, in get
    context = self.get_context_data(**kwargs)
  File "/Users/myname/Dropbox/blog/views.py", line 22, in get_context_data
    import ipdb; ipdb.set_trace()
ImportError: No module named ipdb
4

2 回答 2

1

我只是设置了一个完整的虚拟环境来尝试一下,因为它必须是一个简单的修复。我设法在我的虚拟环境中进行设置ipdb,我将逐步编写我所做的。

$ virtualenv foo
$ cd foo
$ source ./bin/activate  # activate venv

...此时which pythonwhich pip在我的虚拟环境中为我提供了正确的 python 可执行文件。然后接下来:

(venv: foo)$ pip install ipython

此时,which ipython在我的虚拟环境中为我提供了正确的 ipython 可执行文件。确保它指向正确的可执行文件很重要,如果它没有显示正确的可执行文件,而是全局的,请重新激活您的虚拟环境。ipython(和你所有的可执行文件)指向你的 virtualenv 中正确的可执行文件是至关重要的。

然后我将尝试导入 ipdb:

(venv: foo)$ ipython
In [1]: import ipdb
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-2d6f026194dd> in <module>()
----> 1 import ipdb

ImportError: No module named 'ipdb'

未找到模块,因为它尚未安装。我们开始做吧:

(venv: foo)$ pip install ipdb

再试一次:

(venv: foo)$ ipython                                                                                                          [ 16-05-24 22:28 ]
Python 3.5.1 (default, Jan 29 2016, 19:58:36) 
Type "copyright", "credits" or "license" for more information.

IPython 4.2.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import ipdb

In [2]: 

它似乎对我有用。我正在使用zshpython3但它应该没关系。您的问题很可能是没有安装在正确的位置,这意味着使用全局可执行文件而不是 virtualenv 中的可执行文件。

在我的 virtualenv 中,您可以看到已安装 ipdb:

(venv: foo)$ find . -name ipdb
./lib/python3.5/site-packages/ipdb

我希望所有这些写下来有帮助:)

于 2016-05-24T20:36:54.477 回答
1

我正在使用virtualenv-burrito。删除我的 venv,更新 burrito ( virtualenv-burrito upgrade) 并创建一个新的虚拟环境解决了这个问题。

于 2016-05-25T12:26:04.320 回答