debian下使用Sphinx異常“Could not import extension sphinx.builders.linkcheck (exception: cannot import name SSLError)”的解決,debiansphinx
最近使用到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到最新版後問題解決。