在vim中设置python virtualenv [英] Set python virtualenv in vim

查看:228
本文介绍了在vim中设置python virtualenv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用vim用于编码和用于Python特别编码.我常常想执行与Python解释当前的缓冲区. (例如,运行单元测试),通常我使用:!python % <Enter>

I use vim for coding and for python coding in particular. Often I want to execute the current buffer with python interpreter. (for example to run unittests), usually I do this with :!python % <Enter>

这scenatio将工作正常工作与全球蟒蛇,但我想,而不是运行的virtualenv蟒蛇.如何VIM内启用的virtualenv?是否有可能进行切换的virtualenv上运行时?

This scenatio will work works fine with global python, but I want to run virtualenv python instead. How do I enable virtualenv within vim? Is it possible to switch virtualenv on the runtime?

我正在使用macvim

I'm using 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

注意,你必须的MacVim编译针对的Python您使用的的virtualenv,例如如果您是从Python.org下载的Python 2.7,则应使用--with-python-config-dir=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config作为./configure的参数来重新编译MacVim.

Note that you need to have MacVim compiled against the Python you are using for the virtualenv, e.g. 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.

希望有帮助!

只是一个音符归属:很多是走进了写这个小玩意通过的此博客,他应该得到一些信贷.

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.

这篇关于在vim中设置python virtualenv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆