20

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

ak.
  • 3,329
  • 3
  • 38
  • 50

6 Answers6

21

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

sykora
  • 96,888
  • 11
  • 64
  • 71
  • I'm using MacVim, and I start it from the Dock, so it's not really a good option... As far as I understand activating the virtualenv is all about modifying the PATH, PYTHONHOME and PYTHONPATH env vars, maybe some other too. I dont mind to port the virtualenv's activate `script` to vim, I was just wondering if there is an existing solution. – ak. Oct 07 '10 at 12:35
  • 4
    I'm finding this doesn't work if the virtualenv python is a different version from the vim-compiled one (eg 2 vs 3) – hwjp Jun 21 '16 at 13:51
20

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.

dwf
  • 3,503
  • 1
  • 20
  • 25
  • wow, cool, this is what I was looking for, thanks a lot! I didn't know virtualenv creates this activate_this.py – ak. Oct 25 '10 at 19:27
  • Hi dwf, could you take a look at my question here to see why my output is weird? http://stackoverflow.com/questions/17288843/macvim-uses-wrong-python-virtualenv – Lionel Jun 25 '13 at 03:49
  • In Python 3: `exec(open(activate_this).read(), { "__file__": activate_this })` – Zoe Jun 03 '20 at 17:08
3

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.

Nico
  • 197
  • 2
  • 6
  • 2
    I've tried this, but it doesn't seem to work on Mac OSX 10.8 (Mountain Lion) -- some other underlying issue perhaps exists – Lionel Jun 25 '13 at 02:53
  • 1
    @Chris: This is most likely because of [this issue](http://stackoverflow.com/questions/9853584/how-to-use-correct-ruby-in-vim-how-to-modify-path-in-vim/12146694#12146694). – Nolsto Mar 06 '15 at 04:09
  • Worked perfectly! But you may have to set `g:virtualenv_directory'.` E.g. for me: `let g:virtualenv_directory = '~/.local/share/virtualenvs'` ---- For more info check from inside vim/nvim: `:h virtualenv` after the installation. – Karioki Dec 27 '22 at 07:39
1

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.

Finch_Powers
  • 2,938
  • 1
  • 24
  • 34
0

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.

Jingtao Yao
  • 91
  • 1
  • 1
0

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.

shycha
  • 440
  • 5
  • 13