简体   繁体   English

在vim中设置python virtualenv

[英]Set python virtualenv in vim

I use vim for coding and for python coding in particular.我使用 vim 进行编码,尤其是 Python 编码。 Often I want to execute the current buffer with python interpreter.我经常想用 python 解释器执行当前缓冲区。 (for example to run unittests), usually I do this with :!python % <Enter> (例如运行单元测试),通常我用:!python % <Enter>

This scenatio will work works fine with global python, but I want to run virtualenv python instead.这个场景适用于全局 python,但我想运行 virtualenv python。 How do I enable virtualenv within vim?如何在 vim 中启用 virtualenv? Is it possible to switch virtualenv on the runtime?是否可以在运行时切换 virtualenv?

I'm using macvim我正在使用 macvim

Here's what I use (sorry the highlighting is screwy).这是我使用的(对不起,突出显示是乱七八糟的)。

" 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

Note that you need to have MacVim compiled against the Python you are using for the virtualenv, eg if you downloaded Python 2.7 from Python.org you should recompile MacVim using --with-python-config-dir=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config as an argument to ./configure .请注意,您需要针对用于 virtualenv 的 Python 编译 MacVim,例如,如果您从 Python.org 下载了 Python 2.7,您应该使用--with-python-config-dir=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config重新编译 MacVim --with-python-config-dir=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config作为./configure的参数。

Hope that helps!希望有帮助!

EDIT: Just one note of attribution: A lot of the detective work that went into writing this little ditty was done by this blogger , and he deserves some of the credit.编辑:只是一个说明:写这篇小曲的很多侦探工作都是由这位博主完成的,他应该得到一些荣誉。

Activate your virtualenv before starting vim.在启动 vim 之前激活您的 virtualenv。 You will automatically get the corresponding interpreter instance.您将自动获得相应的解释器实例。

There is also a vim plugin on github: github上还有一个vim插件:

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

I have not tried it, but it seems to solve the question as well.我还没有尝试过,但它似乎也解决了这个问题。

this issue actually bothered me for a long time until I use the plugin of vim-conda.这个问题实际上困扰了我很长时间,直到我使用 vim-conda 的插件。 Just add Plugin 'cjrh/vim-conda' in your ~/.vimrc and it will work.只需在您的 ~/.vimrc 中添加插件 'cjrh/vim-conda' 就可以了。 You can also see the detailed instruction https://github.com/cjrh/vim-conda .您还可以查看详细说明https://github.com/cjrh/vim-conda

If for some reasons you do not want to run vim inside a python virtual environment, then instead of sourcing venv/bin/activate , you can:如果由于某种原因,你不希望运行vim一个python虚拟环境中,然后代替采购venv/bin/activate ,您可以:

PYTHONPATH="$(source venv/bin/activate; python3 -c "import sys; print(':'.join(sys.path))"; deactivate)" vim

which also kinda source the virtual environment, but it keeps it somewhat separate from the environment in which vim is run.这也是虚拟环境的来源,但它使其与运行vim的环境有些分离。

You can create a function with an alias for vim to auto load/unload the virtualenv if it exists at the location from which you start it.您可以创建一个带有 vim 别名的函数,以自动加载/卸载 virtualenv(如果它存在于您启动它的位置)。

In this example, it checks for the virtualenv in .venv/bin/activate .在这个例子中,它检查.venv/bin/activate的 virtualenv。

vimVenAutoload() {
    if [ -e .venv/bin/activate ]; then
        . .venv/bin/activate;
        vim $*;
        deactivate;
    else
        vim $*;
    fi;
}
alias vim="vimVenAutoload"

You can add this to your .bashrc or .bash_profile .您可以将其添加到您的.bashrc.bash_profile

Small caveat: If a virtualenv is already loaded, it will be overwritten with the new one.小警告:如果一个 virtualenv 已经加载,它将被新的覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM