Managing Python environments with direnv

A few months ago I discovered direnv and was surprised how I got along so well with out it. From the website, direnv ” allows project-specific environment variables without cluttering the ~/.profile file.” I used it to make aliases that only exist inside a project, as well as a path to project-specific scripts and environment variables. Then I discovered that you can manage Python environments with it and my eyes opened up to all the power you could do. In reality, it makes this popular post unnecessary since direnv does all the work for you.

The direnv project has a good wiki doc here but it’s long and has a lot of scenarios – I’ll put in here what worked for me. This is all very specific to Python and virtualenv, but it gives a good indication of what all you can do with direnv, regardless of what language you are using.

First you have to install direnv with your favorite package manager. Then enabling it is easy – for me, it was adding the following line to the very end~/.zshrc:

 eval "$(direnv hook zsh)"

 

There are hooks for bash and other shells as well.

Then I had a one-time setup in the main direnv file. On my Mac, it is in ~/.config/direnv/direnvrc

use_python() {
    if [ -n "$(which pyenv)" ]; then
        local pyversion=$1
        pyenv local ${pyversion}
    fi
}

layout_virtualenv() {
    local pyversion=$1
    local pvenv=$2
    if [ -n "$(which pyenv virtualenv)" ]; then
        pyenv virtualenv --force --quiet ${pyversion} ${pvenv}-${pyversion}
    fi
    pyenv local --unset
}

layout_activate() {
    if [ -n "$(which pyenv)" ]; then
        source $(pyenv root)/versions/$1/bin/activate
    fi
}

And then in each project directory, I make a .envrc file and add the following

show_virtual_env() {
  if [[ -n "$VIRTUAL_ENV" && -n "$DIRENV_DIR" ]]; then
    echo "($(basename $VIRTUAL_ENV))"
  fi
}
PS1='$(show_virtual_env)'$PS1

pyversion=3.7.2 # your python version
pvenv=my-nifty-project # you project name or what you want to call you virtual env

use python ${pyversion}
# Create the virtualenv if not yet done
layout virtualenv ${pyversion} ${pvenv}
# activate it
layout activate ${pvenv}-${pyversion}

And then, in your terminal, go to you project directory. If you setup direnv correctly (and it’s a one-liner), then you will see a message telling you to allow the changes in your .envrc. Just type direnv allow and, well, that’s it. direnv will see that you don’t have your virutalenv setup in that folder and will automatically set it up. After the initial time, it will automatically register and be enabled for you.

Again, this is just the tip of the iceberg of what you can do with direnv. It’s saved me a lot of headaches. What kind of creative uses can you implement into your workflow?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*