最近使用到Sphinx編譯文檔,出現如下異常:
Extension error:
Could not import extension sphinx.builders.linkcheck (exception: cannot import name SSLError)
一開始有點摸不着頭腦,明明是不能導入“sphinx.builders.linkcheck”,錯誤信息卻是“cannot import name SSLError”
於是使用“-P”參數在異常時打開pdb調試,pdb打印信息如下:
Exception occurred while building, starting debugger:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.5b1-py2.7.egg/sphinx/cmdline.py", line 295, in main
opts.warningiserror, opts.tags, opts.verbosity, opts.jobs)
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.5b1-py2.7.egg/sphinx/application.py", line 189, in __init__
self.setup_extension(extension)
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.5b1-py2.7.egg/sphinx/application.py", line 514, in setup_extension
err)
ExtensionError: Could not import extension sphinx.builders.linkcheck (exception: cannot import name SSLError)
可以看到出錯的地方在“application.py”,於是打開該文件一探究竟,源碼如下:
進一步在pdb打印“extension”和“err”
現在可以知道,是在import的時候遇到了importerror,但是不知道具體是哪里錯了,干脆另起python命令行,直接import這個擴展“sphinx.builders.linkcheck”,結果出現如下錯誤:
可以看到錯誤原因是不能從“requests.packages.urllib3.exceptions”導入“SSLError”,經驗證發現,可以在“requests.exceptions”里面導入“SSLError”
而網上搜索,大多也只是提到從“requests.exceptions”導入“SSLError”。由於Sphinx是剛下載的最新版,我懷疑可能是版本不兼容的問題。
查看requests版本發現requests確實不是最新版本(官網目前最新版本為:v2.12.1):
進一步上官網確認,發現官網上有關SSLError的代碼確實已經發生變化:
到此,完全確認是requests版本更新過程中改變了“SSLError”的位置(導入方式),升級requests到最新版后問題解決。