ipdb正在触发ImportError.当我在本地运行Django站点时,



ipdb触发了一个导入错误。我正在一个虚拟环境中研究Python 2.7。

which ipdb显示路径(/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,它工作得很好。我已尝试重新启动我的终端。

Stacktrace:

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

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

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

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

(venv: foo)$ pip install ipython

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

然后我将尝试导入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的可执行文件。

从我的虚拟机中,你可以看到ipdb已经安装:

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

我希望所有这些都能有所帮助:)

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

相关内容

  • 没有找到相关文章

最新更新