ipdb正在触发ImportError

时间:2016-05-16 13:04:00

标签: python django ipdb

当我在本地运行我的Django站点时,ipdb正在触发导入错误。我正在使用Python 2.7并在虚拟环境中工作。

which ipdb显示路径(/usr/local/bin/ipdb)which ipython也是如此,这让我感到惊讶,因为我认为它应该显示我的venv路径(但是如果它显示的话,那就不应该这样做了#39}无论如何,这是全球性的吗?所以我尝试了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

sys.path中: '/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

2 个答案:

答案 0 :(得分:1)

我只是设置了一个完整的虚拟环境来尝试这个,因为它必须是一个简单的修复。我设法在我的虚拟环境中设置$http({ method: 'POST', url: 'http://www.test.com/api/app/controller', data: postdata, headers: { 'Content-Type': 'application/json' } }) .then(function(response) { // Do stuff }, function() { // Show error }) .finally(function() { // Cancel loading indicator }); ,我会逐步写下我所做的。

ipdb

...此时$ virtualenv foo $ cd foo $ source ./bin/activate # activate venv which python在我的虚拟环境中为我提供了正确的python可执行文件。接下来:

which pip

此时,(venv: foo)$ pip install ipython 在我的虚拟环境中为我提供了正确的ipython可执行文件。 确保它指向正确的可执行文件非常重要,如果它没有显示正确的可执行文件,那么全局的可执行文件会重新激活您的虚拟环境。至关重要的是,ipython(以及所有可执行文件)指向virtualenv中的正确可执行文件。

然后我要尝试导入ipdb:

which ipython

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

(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]: zsh,但这并不重要。您的问题很可能是它没有安装在正确的位置,这意味着使用全局可执行文件而不是virtualenv中的可执行文件。

从我的virtualenv中你可以看到安装了ipdb:

python3

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

答案 1 :(得分:1)

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