asdf, direnv and the upgrade to python 3.10

06 Oct 2021 07:16 UTC-07:00

Recently Python Bytes a post mentioned using asdf and direnv to manage Python versions.

I've always looked for a good way to have auto-setting environments and while virtualenvwrapper did a lot for that. I feel like using pyenv via Homebrew was a step backward, having to initialize pyenv-virtualenvwrapper every time I opened the terminal felt like I was doing something wrong. I probably was.

That said asdf gives you pyenv-like1 simplicity for running multiple versions of Python (and nvm, and ruby, and other things).

My Installation


Install Homebrew. We aren't removing it from our setup, just liberating it from programming language version management responsibilities.

Install direnv. Using the layout command is how we'll create that auto-environment control. layout will create a shim of our requested Python version in the .direnv directory of our project2.

[Optional] - This blog post is using zsh and ohmyzsh as the environment. It's not required but this post will explain the setup using that.

Install asdf and Python

Run brew install asdf. If running ohmyzsh, you can add asdf to the list of plugins, otherwise be sure to follow any setup instructions.

zsh plugins.

You'll also need the asdf-python plugin with asdf plugin-add python

Next you'll need to install Python through asdf. I recommend completely uninstalling Python via Homebrew. Once that is done you can re-install Python using the command asdf install python <PYTHON VERSION> you can also use latest for the Python version if you aren't sure with version is the latest one.

You can verify the python versions installed using asdf list python. You can verify that asdf installed version of python is default by running which python.

If you have multiple versions installed, you can select the version similar to pyenv using asdf global python <PYTHON VERSION>

You can tell which one is set using python --version.

asdf python versions

Register a Python Environment for Your Project

When you want to create a new project make the directory for that project. Then create a .envrc file and add layout python <PYTHON VERSION>

I had a recent issue with the part of this3. If you drop this then it will use the latest version by default.

Then you will need to activate direnv using direnv allow.

  1. If I remember correctly it's actually using pyenv to manage the version. I'm too lazy to check. 

  2. Also thanks to Jeff Triplett who got me into direnv for directory based environment variables a while ago. 

  3. To be fair, at the time of writing the version I was trying to use was less than 24 hours old so things may not have been updated yet. I just tried again before releasing this and the problem has been fixed.