gpt4 book ai didi

python - ipdb 正在触发 ImportError

转载 作者:行者123 更新时间:2023-12-01 03:57:09 25 4
gpt4 key购买 nike

当我在本地运行 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,它工作正常。我尝试过重新启动我的终端。

堆栈跟踪:

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(以及所有可执行文件)指向 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

我希望这篇文章能有所帮助:)

关于python - ipdb 正在触发 ImportError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37254610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com