Python Virtual Environment

Last Updated: Feb 5, 2025

The default way of creating a Python virtual environment is through something like the following command:

python3 -m venv .virtual-env

where you cannot effectively specifcy which version of Python for the specific virtual environment. Conventionally, you can specify it by a README file, but this takes extra steps.

pyenv is a tool which exactly solves this problem. Nevertheless, it is generally a great tool for handling multiple versions of Python.

For example if I want to create a virtualenv with a specific Python version, I can achieve this by doing:

pyenv virtualenv 3.12.8 virtualenv-test

Then, activate it whatever directory I am at in the shell:

pyenv activate virtualenv-test

Read more about virtualenv here.