I was doing some work with Django 3.2.9 and Python 3.10 recently, and I came across a strange issue. The root cause of this was that the pyenv shims had disappeared from my
PATH and so the system Python version was being used. Here’s how to fix it.
I was upgrading a project to the latest versions of Django and Python when I hit this issue. Hopefully this post will save others some time debugging it.
I use pyenv to manage my Python versions on Linux so I updated it, installed the latest version of Python with it and set that as the version to use. However, when I tried to create a virtual environment (a venv is usually a good idea) I hit the following error:
No module named venv
The Python version being used was picked up from my
/usr/bin/ directory (shown using the
which command) rather than the version under
.pyenv. Looking into the root cause, the pyenv binary was still in my
PATH but the shims were missing.
It would appear that something has changed with pyenv since I last updated it a couple of Python patches ago. I originally installed it with pyenv-installer and it seems the method of initialising has changed.
I came across this issue from the most recent commit to the instructions. It looks like the syntax changed from
pyenv init - to
pyenv init --path. Making this change and restarting my shell (
exec $SHELL) fixed the shims issue but broke auto-completion. Putting both versions in fixes the problem.
.bashrc file now ends like this:
eval "$(pyenv init --path)" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
Everything looks like it is working again now and the shims appear at the start of my
PATH (shown with