157 words
1 minute
Quickly testing code in a different Python version using pyenv
Anubhav Gain
2024-02-20

Quickly testing code in a different Python version using pyenv#

I had a bug that was only showing up in CI against Python 3.8.

I used the following pattern with pyenv to quickly run the tests against that specific version.

(I had previously installed pyenv using brew install pyenv.)

Seeing what versions I had already#

Terminal window
pyenv versions

This outputs (on my machine):

system
3.7.16
3.8.17

To see all possible versions:

Terminal window
pyenv install --list

That’s a long list! I grepped it for 3.8:

Terminal window
pyenv install --list | grep '3.8'
3.8.0
3.8-dev
3.8.1
3.8.2
...
3.8.14
3.8.15
3.8.16
3.8.17
...

Installing a specific version#

I installed 3.8.17 like this:

Terminal window
pyenv install 3.8.17

This took a long time, because it compiled it from scratch.

Using that version via a virtual environment#

I decided to use that version of Python directly. The binary was installed here:

Terminal window
~/.pyenv/versions/3.8.17/bin/python

I created a temporary virtual environment in /tmp like this:

Terminal window
~/.pyenv/versions/3.8.17/bin/python -m venv /tmp/py38env

Then installed my current project into that environment like so:

Terminal window
/tmp/py38env/bin/pip install -e '.[test]'

Now I can run the tests like this:

Terminal window
/tmp/py38env/bin/pytest
Quickly testing code in a different Python version using pyenv
https://mranv.pages.dev/posts/quickly-testing-code-in-a-different-python-version-using-pyenv/
Author
Anubhav Gain
Published at
2024-02-20
License
CC BY-NC-SA 4.0