anaconda+youcompleteme


簡單記錄一下混合使用anaconda和youcompleteme的經歷

最近python寫得比較多,使用vim+youcompleteme總覺得補全好差,任何第三方庫都搞不定,想不通這玩意怎么和我另一台電腦里的功能差距有點大?我的archlinux+python+vim+ycm不是好好的嗎?

之前忙,沒時間管這事,即使補全差點,加上ipython湊合也還能用。

今晚有些受不了,於是就思考了一番,還試了試jedi-vim,結果不合意,棄。仔細思考后發現不是ycm菜,是我蠢。ycm默認解釋器路徑是系統自帶的python,找尋的庫默認也是找系統的。而我平時使用默認的python是anaconda的,庫也是安裝在anaconda下,難怪檢索不到。

解決方案:

1. 安裝基本工具:

sudo apt install cmake build-essential python-dev python3-dev

2. 將vimrc中的下面語句注釋

Plugin 'Valloric/YouCompleteMe'

  打開一個vim窗口,執行:BundleClean刪了ycm。

       再取消注釋前面的那句,執行:BundleInstall重新安裝。

3. 根據https://github.com/Valloric/YouCompleteMe/issues/1241,作者本人表示無可奈何,有個好心人boulund給出如下方案:

  1. Add/install YouCompleteMe using Vundle (by adding Plugin 'Valloric/YouCompleteMe' to your ~/.vimrc)
  2. Restart vim and run :PluginInstall, wait for it to complete (takes a while). When it finishes, close vim.
  3. Go to ~/.vim/bundle/YouCompleteMe/
  4. Run install.py from inside the YouCompleteMe directory; it will fail. Remember the Python library and include paths, these paths are required in the next step.
  5. For me the paths were:
  6. -DPYTHON_LIBRARY=/home/fredrikb/anaconda3/lib/libpython3.5m.so and
  7. -DPYTHON_INCLUDE_DIR=/home/fredrikb/anaconda3/include/
  8. Go to ~/.vim/bundle/YouCompleteMe/third_party/ycm/cpp/. If there is a CMakeCache.txt, delete it.
  9. Run: cmake -G "Unix Makefiles" -DPYTHON_LIBRARY=/home/fredrikb/anaconda3/lib/libpython3.5m.so -DPYTHON_INCLUDE_DIR=/home/fredrikb/anaconda3/include/ -DUSE_PYTHON2=OFF, then run make.
  10. Start vim on a *.py file to verify that YouCompleteMe now works.

        我總結一下,到目錄~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp下,里面有個CMakeLists.txt文件,運行下面的命令:

cmake -G "Unix Makefiles" -DPYTHON_LIBRARY=$you_home_dir/anaconda3/lib/libpython3.6m.so -DPYTHON_INCLUDE_DIR=$your_home_dir/anaconda3/include -DUSE_PYTHON2=OFF 

將上面的$you_home_dir改為家目錄即可。另外,為了確保正確,最好查看動態庫是否真是libpython3.6m.so。如果需要C/C++補全,請自行下載llvm+clang(鏈接),解壓,結束后該目錄下應該有lib,bin等目錄,在上面語句加入參數:

-DPATH_TO_LLVM_ROOT=$your_llvm_dir

然后運行如下命令即可:

cmake --build . --target ycm_core 

注意,運行完你正常情況下是會失敗的。大概進行到百分之十幾的時候,因為需要python-dev。你可能會很奇怪這玩意不是通過包管理器安裝了嗎?問題在於,你用的頭文件是anaconda的,引用不到你系統的頭文件。(個人猜測)

根據google結果(結果),運行下面命令即可。注意,這些操作都必須在一個終端下完成,否則后果自負。另外,檢查自己的python頭文件具體在哪。

export CPLUS_INCLUDE_PATH=/usr/include/python3.5m

可能還需要運行

make clean

然后重新執行:

cmake --build . --target ycm_core 

即可。不出意外,你不會碰到錯誤。不過你試着補全,發現一點效果都沒有,查看/tmp目錄下的錯誤日志,發現錯誤大致是`GLIBCXX_3.4.20' not found。根據http://blog.csdn.net/u010987458/article/details/71702938,anaconda自帶的gcc版本有點土,需要更新,使用下面命令即可:

conda install libgcc

不出意外,這時YouCompleteMe能正常工作,而且效果很好。

 


 

Update

export CPLUS_INCLUDE_PATH=/usr/include/python3.5m

這一步需要根據具體的python版本選擇頭文件目錄,比如python版本是3.6,頭文件目錄應該為python3.6m,python版本見cmake那行的python動態庫版本。

 

另外,直接這樣安裝會導致對於Go文件無法使用YcmCompleter GoToDefinition,提示ValueError: No semantic completer exists for filetypes: ['go']。對此,我的解決辦法是重新clone,運行install.py版本(記得附帶--go-completer),之后再有選擇的執行本文前面說的步驟。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM