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

Object Partners profile.

One thought on “Managing Python environments with direnv

Leave a Reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]