Skip to content

Instantly share code, notes, and snippets.

@jcausey-astate
Last active February 3, 2022 03:51
Show Gist options
  • Save jcausey-astate/66cfc5e94d23467474d16aaff32e74b5 to your computer and use it in GitHub Desktop.
Save jcausey-astate/66cfc5e94d23467474d16aaff32e74b5 to your computer and use it in GitHub Desktop.
Simple way to automatically select virtual environments based on direcotry in Linux.

Tricks for working with many virtualenv environments on a Linux server.

Much of this was adapted from Justin Abrahms. I've just collected the bits related to setting up the environment in bash here.

Assumptions

This page assumes that you have access to pip, but that you are not the administrator (no root or sudo access).

Get virtualenv if it isn't installed

If you don't have virtualenv installed, you can do:

pip install --user virtualenv

Get virtualenvwrapper if it isn't installed

To install virtualenvwrapper, pip will try to modify files wherever the system virtualenv package is installed. If you installed virtualenv yourself using the command above, then there is no need to worry (it is already in your user directory) and you can probably drop the --user flag. But I've included it here because I've often interacted with systems that have virtualenv but not virtualenvwrapper:

pip install --user virtualenvwrapper

Now you need to add some lines to your ~/.profile or ~/.bashrc file. (I usually add at the bottom of .profile.) These lines provide the command line "magic" that makes virtualenvwrappers workon command and completions work:

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source "$HOME/.local/bin/virtualenvwrapper.sh"

NOTE: You need to look closely at the last line. If your local pip installation directory is different from ~/.local/ then you will need to change it to match wherever the virtualenvwrapper.sh script was actually installed. Here's how you can find out:

cd ~
find . -name virtualenvwrapper.sh

Now whatever path is displayed by find is what you should use after the word source in the third line of the additions to your .profile or .bashrc shown above.

Learn to use virtualenvwrapper

Well, read the docs here!

The most important commands are:

  • workon --- used to specify a virtual environment to activate. Type the environment's name after the workon command.
  • deactivate --- used to shut down a virtual environment when you are finished.
  • mkvirtualenv --- used to create a new virtual environment. Type a name for the new virtual environment after the mkvirtualenv command. You can specify the Python version with the --python flag.

Automatically activate virtual environments based on directory

This is a great trick I found on Justin Abrahms's blog. You can hack your .profile (or .bashrc) just a little bit more to make some magic happen.

It goes like this. In each project that needs a specific virtual environment, create a (plain-text) file named .venv in the top-level directory of the project. The contents of the file should be the name of the virtual environment that you need to activate when working on that project. Let's say I have a project directory foo_source that needs a virtual environment named foo. I create a file foo_source/.venv that contains the word foo and nothing else. With the lines shown below added to your .profile or .bashrc, every time you use cd to change directories, it will look for the file .venv. If the file is found, the workon command is automatically called with the name provided in the .venv file. So if I cd foo_source, the command workon foo will automatically be executed and I'm in the correct environment for my project!

Here are the lines to add to your .profile or .bashrc:

# The following will check for a ".venv" file in every directory you
# 'cd' into, then if it exists it will use the contents to start the
# appropriate virtualenv with `workon`
# Hack from https://justin.abrah.ms/dotfiles/zsh.html#sec-2-7
# Support for bash
PROMPT_COMMAND='prompt'

function prompt()
{
    if [ "$PWD" != "$MYOLDPWD" ]; then
        MYOLDPWD="$PWD"
        test -e .venv && workon `cat .venv`
    fi
}

Be sure to source ~/.profile (or .bashrc) before trying it out.

Enjoy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment