Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3287174
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:27:48+00:00 2026-05-17T20:27:48+00:00

I use vim for coding and for python coding in particular. Often I want

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:27:48+00:00Added an answer on May 17, 2026 at 8:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Vim and Vim plugins for Visual Studio when writing C++. Often, I
I am using VIM in Windows. The problem is that I want to use
I use vim (7.1) on OpenVMS V7.3-2. I connect to VMS trough a telnet
:vimgrep looks like a really useful thing. Here's how to use it: :vim[grep][!] /{pattern}/[g][j]
How do I make vi - Vim never use tabs (converting spaces to tabs,
I use _vimrc to configure my vim 7.2 (windows) default settings. One setting set
When I use `gqap' command to reflow a paragraph in vim, vim seems to
For those of us that like to use the graphical version of Vim or
When I'm opening a new file in Vim and I use tab completion, it
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.