在vim中设置python virtualenv
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在启动 vim 之前激活你的 virtualenv。您将自动获取相应的解释器实例。
Activate your virtualenv before starting vim. You will automatically get the corresponding interpreter instance.
这是我使用的(抱歉突出显示很奇怪)。
请注意,您需要针对用于 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).
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.
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.
您可以使用 vim 的别名创建一个函数,以自动加载/卸载 virtualenv(如果它存在于您启动它的位置)。
在此示例中,它检查
.venv/bin/activate
中的 virtualenv。您可以将其添加到
.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
.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.
这个问题其实困扰了我很长一段时间,直到我使用了 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.
如果由于某些原因您不想在 python 虚拟环境中运行 vim,那么您可以不使用 venv/bin/activate,
也可以使用以下方式 :环境,但它在某种程度上使其与
vim
运行的环境分开。If for some reasons you do not want to run
vim
inside a python virtual environment, then instead of sourcingvenv/bin/activate
, you can:which also kinda source the virtual environment, but it keeps it somewhat separate from the environment in which
vim
is run.