Pyenv to the rescue

1 minute read


The problem

I recently switched to Debian testing again so as to be able to use Python 3.6 among other things. I started working with this version, creating my Lambda Layers and everything was fine until Debian testing switched to Python 3.7. Well, I told myself, there is no drama, using alternative I can switch versions of Python smoothly or set virtualenv to use one interpreter or another.

:~# update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
  0            /usr/bin/python3.6   3         auto mode
  1            /usr/bin/python2     1         manual mode
  2            /usr/bin/python3     2         manual mode
* 3            /usr/bin/python3.6   3         manual mode

Press <enter> to keep the current choice[*], or type selection number: 3

But even using alternative or forcing the interpreter to use Python 3.6 it seems there is always something pointing to Python 3.7:

$ virtualenv -p /usr/bin/python3.6 test
Running virtualenv with interpreter /usr/bin/python3.6
Traceback (most recent call last):
  File "/home/lgallard/.local/lib/python3.7/site-packages/", line 22, in <module>
    import distutils.spawn
ModuleNotFoundError: No module named 'distutils.spawn'

pyenv to the rescue

There is a version manager of Python called pyenv which allows you to configure the environment to use both locally and globally.

For example, to install Python version 3.6.8 just do

pyenv install 3.6.8

We can verify the versions as follows:

$ pyenv versions
* 3.6.8 (set by /home/lgallard/.pyenv/version)

Then to use the version installed with pyenv, just pass the path:

$ virtualenv -p ~/.pyenv/versions/3.6.8/bin/python test
Running virtualenv with interpreter /home/lgallard/.pyenv/versions/3.6.8/bin/python
Using base prefix '/home/lgallard/.pyenv/versions/3.6.8'
New python executable in /home/lgallard/test/bin/python
Installing setuptools, pip, wheel...

./test/bin/python --version
Python 3.6.8

This way I was able to keep using the specific Python version I needed.


Leave a Comment