Fork me on GitHub

ipdb ImportError: cannot import name decorator

说起Python调试工具,首先想到的就是Python自带的pdb。但是对于用惯了IPython的我来说,没有命令、变量自动补全功能的pdb显然无法取悦我。既然Python都有IPython了,pdb肯定也有ipdb了。安装ipdb很简单,一条命令即可:

1
pip install ipdb

在公司的开发机安装完ipdb后,使用vim设置断点调试程序时却报错了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/yeyq/Webpage/yzb/auth/views.py", line 44, in login_page
import ipdb;ipdb.set_trace()
File "/usr/local/lib/python2.7/dist-packages/ipdb/__init__.py", line 7, in <module>
from ipdb.__main__ import set_trace, post_mortem, pm, run # noqa
File "/usr/local/lib/python2.7/dist-packages/ipdb/__main__.py", line 34, in <module>
from IPython.core.debugger import Pdb, BdbQuit_excepthook
File "/usr/local/lib/python2.7/dist-packages/IPython/__init__.py", line 48, in <module>
from .core.application import Application
File "/usr/local/lib/python2.7/dist-packages/IPython/core/application.py", line 23, in <module>
from traitlets.config.application import Application, catch_config_error
File "/usr/local/lib/python2.7/dist-packages/traitlets/config/__init__.py", line 6, in <module>
from .application import *
File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 17, in <module>
from decorator import decorator
ImportError: cannot import name decorator

无奈只好求助于google大法,不过网上给出的解决方案大多都是升级ipython,但是我安装的是5.3.0版本的ipython,已经是Python2环境下的最新可用版本了。ipython在github上也有相关的issue,ipython not opening [ubuntu] #5876,参照里面给出的几种方案我都尝试了一遍,然并卵。没办法,只好硬着头皮自己去填坑了。

最后,捣鼓了一下午,终于解决了。既然错误提示在/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py中无法导入decorator模块,但是decorator模块明明已经安装,那我直接在/usr/local/lib/python2.7/dist-packages/traitlets/config/目录下建立一个指向decorator的软链接,总该能导入了吧。果不其然,执行

1
sudo ln -s /usr/local/lib/python2.7/dist-packages/decorator.py /usr/local/lib/python2.7/dist-packages/traitlets/config/decorator.py

异常信息变成了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/yeyq/Webpage/yzb/auth/views.py", line 44, in login_page
import ipdb;ipdb.set_trace()
File "/usr/local/lib/python2.7/dist-packages/ipdb/__init__.py", line 7, in <module>
from ipdb.__main__ import set_trace, post_mortem, pm, run # noqa
File "/usr/local/lib/python2.7/dist-packages/ipdb/__main__.py", line 34, in <module>
from IPython.core.debugger import Pdb, BdbQuit_excepthook
File "/usr/local/lib/python2.7/dist-packages/IPython/__init__.py", line 49, in <module>
from .terminal.embed import embed
File "/usr/local/lib/python2.7/dist-packages/IPython/terminal/embed.py", line 16, in <module>
from IPython.core.magic import Magics, magics_class, line_magic
File "/usr/local/lib/python2.7/dist-packages/IPython/core/magic.py", line 25, in <module>
from decorator import decorator
ImportError: cannot import name decorator

同理,再次运行

1
sudo ln -s /usr/local/lib/python2.7/dist-packages/decorator.py /usr/local/lib/python2.7/dist-packages/IPython/core/decorator.py

问题就迎刃而解了。

梦想还是要有的,万一有人赏了呢