在vim中设置python virtualenv

发布于 2024-09-26 08:03:19 字数 240 浏览 8 评论 0原文

我使用 vim 进行编码,特别是 python 编码。我经常想用 python 解释器执行当前缓冲区。 (例如运行单元测试),通常我使用 :!python %

这个场景可以在全局 python 中正常工作,但我想改为运行 virtualenv python。如何在 vim 中启用 virtualenv?是否可以在运行时切换 virtualenv ?

我正在使用 macvim

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>

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?

I'm using macvim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

有木有妳兜一样 2024-10-03 08:03:19

在启动 vim 之前激活你的 virtualenv。您将自动获取相应的解释器实例。

Activate your virtualenv before starting vim. You will automatically get the corresponding interpreter instance.

网名女生简单气质 2024-10-03 08:03: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 重新编译 MacVim /Python.framework/Versions/2.7/lib/python2.7/config 作为 ./configure 的参数。

希望有帮助!

编辑:只有一个归属说明:写这首小曲的许多侦探工作都是由 这位博主,他值得一些功劳。

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, 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.

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.

可爱咩 2024-10-03 08:03:19

github上还有一个vim插件:

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

我没有尝试过,但似乎也解决了这个问题。

There is also a vim plugin on github:

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

I have not tried it, but it seems to solve the question as well.

尸血腥色 2024-10-03 08:03:19

您可以使用 vim 的别名创建一个函数,以自动加载/卸载 virtualenv(如果它存在于您启动它的位置)。

在此示例中,它检查 .venv/bin/activate 中的 virtualenv。

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

您可以将其添加到 .bashrc.bash_profile 中。

小警告:如果 virtualenv 已经加载,它将被新的覆盖。

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.

In this example, it checks for the virtualenv in .venv/bin/activate.

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.

Small caveat: If a virtualenv is already loaded, it will be overwritten with the new one.

俏︾媚 2024-10-03 08:03:19

这个问题其实困扰了我很长一段时间,直到我使用了 vim-conda 的插件。只需在 ~/.vimrc 中添加插件“cjrh/vim-conda”即可。还可以查看详细说明https://github.com/cjrh/vim-conda

this issue actually bothered me for a long time until I use the plugin of vim-conda. Just add Plugin 'cjrh/vim-conda' in your ~/.vimrc and it will work. You can also see the detailed instruction https://github.com/cjrh/vim-conda.

隱形的亼 2024-10-03 08:03:19

如果由于某些原因您不想在 python 虚拟环境中运行 vim,那么您可以不使用 venv/bin/activate,

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

也可以使用以下方式 :环境,但它在某种程度上使其与 vim 运行的环境分开。

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文