LoginSignup
0
1

More than 3 years have passed since last update.

Windows+gVim+Poetryのpython開発環境構築

Posted at

目標

Windows+gVim+Poetryで開発をするときに、ライブラリのコード補完やリンターが効くような環境を構築する。

gVimインストール

image.png

  • ダウンロードしたzipを展開し、gvim.exeがあるフォルダをPATHに追加する。

git for windowsのインストール

image.png

  • ダウンロードしたインストーラを実行する
  • オプション等は特に変更する必要なし。

vimプラグインのインストール

  • vimプラグインであるvim-lsp,ALEをインストールします。
  • powershellで下記コマンドを実行
    • 独自にパッケージ管理をしている方はそちらでインストールしてください
cd ~
New-Item vimfiles/pack/mypackage/opt -ItemType Directory
cd vimfiles/pack/mypackage/opt
git clone https://github.com/prabirshrestha/vim-lsp.git
git clone https://github.com/prabirshrestha/async.vim.git
git clone https://github.com/dense-analysis/ale.git

~/_vimrcに下記追記

let g:ale_completion_enabled = 1
packadd ale 
let g:ale_lint_on_save = 1
let g:ale_sign_column_always = 1
packadd async.vim
packadd vim-lsp
let g:lsp_diagnostics_enabled = 0 " エラー表示はALEで行う
function! s:configure_lsp() abort
  setlocal omnifunc=lsp#complete
  nnoremap <buffer> <C-]> :<C-u>LspDefinition<CR>
  nnoremap <buffer> gd :<C-u>LspDefinition<CR>
  nnoremap <buffer> gD :<C-u>LspReferences<CR>
  nnoremap <buffer> gs :<C-u>LspDocumentSymbol<CR>
  nnoremap <buffer> gS :<C-u>LspWorkspaceSymbol<CR>
  nnoremap <buffer> gQ :<C-u>LspDocumentFormat<CR>
  vnoremap <buffer> gQ :LspDocumentRangeFormat<CR>
  nnoremap <buffer> K :<C-u>LspHover<CR>
  nnoremap <buffer> <F1> :<C-u>LspImplementation<CR>
  nnoremap <buffer> <F2> :<C-u>LspRename<CR>
endfunction

"python用設定
if executable('pyls')
    augroup lsp_pyls_enable
        autocmd!
        autocmd User lsp_setup call lsp#register_server({
                    \ 'name': 'pyls',
                    \ 'cmd': {server_info->['pyls']},
                    \ 'whitelist': ['python'],
                    \ })
        autocmd FileType python call s:configure_lsp()
        autocmd FileType python imap <expr> . ".\<C-X>\<C-O>"
    augroup end
endif

pythonのインストール

  • pythonの公式サイトから使いたいバージョンのpythonインストーラをダウンロード
  • 今回はpython3.7.5をダウンロードします。
    image.png

  • ダウンロードしたインストーラを実行する。

image.png
image.png

Visual C++ 再配布パッケージのインストール

  • コード補完で使うpython-language-serverのインストールに必要
  • Visual C++ ダウンロードページからインストーラをダウンロード image.png
  • ダウンロードしたインストーラを実行する

image.png

poetryのインストール

  • powershellを起動し下記コマンドを実行する
pip install --user --upgrade pip
pip install poetry

Poetryプロジェクトの作成

  • プロジェクトを作成したいディレクトリで下記コマンドを実行
poetry new testproject
cd testproject
poetry add --dev python-language-server
poetry add --dev flake8

gVim起動

  • gVimを起動するときはpoetry shellに入ってから起動する
cd testproject
poetry shell
gvim main.py
  • ファイルを保存したときにpep8に準拠していなければエラー箇所が表示されます
  • 補完は<C-x><C-o>でできますが、.(ドット)を入力後は自動で補完がでます
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1