I have multiple vim plugins (ropevim, taskwarrior etc.) which depend on python libraries. These libraries are installed in system python3

When I am working on a python project, the virtual environment for that project does not have these libraries. Resulting in these plugins not working and dumping a ton of error messages on the console.

Is it possible to ensure that vim always uses the same virtual environment (or system python env) for its plugins irrespective of what the shell virtualenv is?

Note I had asked this Q. on stackoverflow. someone recommended asking in this sub group. I will close the Q. there

  • 1
    I think virtualenv is based on a bunch of environment variables? You can set those in Vim with let $VARNAME = 'value'. I'm not really familiar with virtualenv, but you can probably do a bit of muckery with that to ensure it's set up correctly, for example by prefixing $PATH with the desired virtualenv (and maybe also some others, like some $PYTHON* ones). Check env | grep -i python to see what it sets. Commented Oct 23, 2022 at 4:03
  • You are correct. The activate script backs-up the system PATH and PYTHON_HOME to _OLD_VIRTUAL_PATH and _OLD_VIRTUAL_PYTHONHOME. So checking for and restoring them before the call to vundle#begin() should do the trick. Commented Oct 24, 2022 at 7:22
  • Hmm... I spoke too soon, these variables are not exported hence are not visible in vimscript... VIRTUAL_ENV is exported, I will have to check for that and use regex to clean up the path. Commented Oct 24, 2022 at 7:43

1 Answer 1


Adding the following snippet at the start of .vimrc restores the default python environment.

if has_key(environ(),"VIRTUAL_ENV")
        let $PATH=substitute($PATH,join([$VIRTUAL_ENV,"bin:"],"/"),"","g")
  • 3
    A slightly nicer way to write that might be something like let $PATH = $PATH->split(':')->filter({_, v -> v !~ '^' .. $VIRTUAL_ENV})->join(':'); this splits $PATH to a list, and it will still work if $VIRTUAL_ENV/bin happens to be the last entry in $PATH. Commented Oct 24, 2022 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.