On UNIX-like operating systems you can have the Python equivalent of
node_modules today, for every Python version, without changing your workflows.
node_modules directory instead of managing virtualenvs.
All it takes is the following line in the
.envrc in the project’s directory:
layout python python3.9
Now whenever you enter that directory, direnv will look into the local
.direnv directory and either activate or create a venv for the exact Python version. As of writing this it would be
If something breaks, just delete it. The only thing to keep in mind is that it’s usually a good idea to update pip and setuptools whenever direnv creates a new venv for you (aka whenever entering the directory takes unusually long). To clean up stale venvs I just use
$ find . -type d -name "python-OLD-VERSION" -delete
layout pyenv 3.9.6
My thanks go to Chris for telling me about this a few months ago.
I personally prefer using asdf nowadays, because I can use it for managing all kinds of compilers and runtimes. It uses a well-known per-directory file called
.tool-versions. Don’t use Homebrew’s Python. ↩︎