18

私はコーディング、特に python コーディングに vim を使用しています。多くの場合、現在のバッファーを Python インタープリターで実行したいと考えています。(たとえば、単体テストを実行するため)、通常、これを行います:!python % <Enter>

このシナリオは、グローバル python で正常に動作しますが、代わりに virtualenv python を実行したいと考えています。vim内でvirtualenvを有効にするにはどうすればよいですか? ランタイムで virtualenv を切り替えることは可能ですか?

私はmacvimを使用しています

4

6 に答える 6

19

これが私が使用するものです(強調表示が面倒です)。

" Function to activate a virtualenv in the embedded interpreter for
" omnicomplete and other things like that.
function LoadVirtualEnv(path)
    let activate_this = a:path . '/bin/activate_this.py'
    if getftype(a:path) == "dir" && filereadable(activate_this)
        python << EOF
import vim
activate_this = vim.eval('l:activate_this')
execfile(activate_this, dict(__file__=activate_this))
EOF
    endif
endfunction

" Load up a 'stable' virtualenv if one exists in ~/.virtualenv
let defaultvirtualenv = $HOME . "/.virtualenvs/stable"

" Only attempt to load this virtualenv if the defaultvirtualenv
" actually exists, and we aren't running with a virtualenv active.
if has("python")
    if empty($VIRTUAL_ENV) && getftype(defaultvirtualenv) == "dir"
        call LoadVirtualEnv(defaultvirtualenv)
    endif
endif

virtualenv に使用している Python に対して MacVim をコンパイルする必要があることに注意してください。たとえば、Python.org から Python 2.7 をダウンロードした場合は--with-python-config-dir=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config、 への引数としてMacVim を使用して再コンパイルする必要があります./configure

それが役立つことを願っています!

編集:帰属についての 1 つのメモ: この小さな記事を書くために行った調査作業の多くは、このブロガーによって行われました。

于 2010-10-25T17:22:44.730 に答える
18

vim を起動する前に、virtualenv を有効にします。対応するインタープリター インスタンスを自動的に取得します。

于 2010-10-07T12:23:27.023 に答える
2

github には vim プラグインもあります。

https://github.com/jmcantrell/vim-virtualenv

試したことはありませんが、質問も解決するようです。

于 2013-05-11T13:39:13.730 に答える