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?

About the Author

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.

One thought on “Managing Python environments with direnv

Leave a Reply

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

Related Blog Posts
Rockstar Development
This originally appeared on Marty Henderson’s personal blog Or, how to use Gitpod and GitLab so that no one else has to care about your questionable coding language choices. A true rockstar has a good […]
Testing a Quarkus Kafka Application
Quarkus, a “Kubernetes Native Java stack,” enables lighter Java applications with faster startup times. In a recent post, I talked about scaling Kafka consumers in Kubernetes. Quarkus applications fit right into this picture because they […]
Gitpod and Hringvegurinn
Iceland Ever seen an advertisement for visiting Iceland? Have you noticed that they all mention Hringvegurinn or the Ring Road, as a good tour? (If you haven’t seen a tour ad for Iceland, Steindi Jr […]
Tacos and Steak, an Istio story
This post originally appear on Marty Henderson’s personal blog A brief history In the before times of 2017, Varun Talwar and Louis Ryan sat under a forgotten waterfall at the edge of the world and […]