Using pyenv and pyenv-virtualenv to install Python and create a virtual environment on MacOS

How to use pyenv and pyenv-virtualenv to install a specific version of Python and create a virtual environment with that version on MacOS

This is an update version of my previous tutorial Install Python with pyenv and pyenv-virtualenv create virtual environment with specific Python version. In this version I will show how to install Python 3.10.0 and create a virtual environment with that version.

To manage my Python versions I’m using using pyenv and pyenv-virtualenv and once they are correctly configured, it’s very easy to install a new Python version.

Make sure you have the required packages installed

First of all make sure you have all the latest packages installed (I’m using Homebrew for MacOS):

1
2
3
brew update
brew install pyenv
brew install pyenv-virtualenv

once installed you should see the latest version:

1
2
pyenv --version
pyenv 2.2.2

Configure pyenv and pyenv-virtualenv

To be able to use pyenv and pyenv-virtualenv you need to write this basic configuration in your ~/.bashrc (or ~/.zshrc if you are using zsh):

1
2
3
4
5
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Install Python 3.10.0

To install Python 3.10.0 you only need pyenv install 3.10.0:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
pyenv install 3.10.0
python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Downloading Python-3.10.0.tar.xz...
-> https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tar.xz
Installing Python-3.10.0...
patching file aclocal.m4
patching file configure
Hunk #5 succeeded at 10537 (offset -15 lines).
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.10.0 to /Users/andrea/.pyenv/versions/3.10.0

Set Python 3.10.0 as the local version

Now that the version you want has been installed, you need to tell pyenv you want to use it:

1
2
pyenv local 3.10.0
pyenv which python

which will show you this:

1
/Users/andrea/.pyenv/versions/3.10.0/bin/python

Create a virtual environment using Python from pyenv

To create a virtual environment named my-310-python using Python 3.10.0 version, you just need this command:

1
pyenv virtualenv 3.10.0 my-310-python

Verify that the virtual environment has been created:

1
2
pyenv virtualenvs
3.10.0/envs/my-310-python (created from /Users/andrea/.pyenv/versions/3.10.0)

Test and automatically activate the virtual environment

Create a test folder:

1
mkdir test-310

Create a .python-version file in the test folder:

1
echo my-310-python > test-310/.python-version

Now if you enter the folder (cd test-310), the virtual environment will be automatically activated:

1
2
cd test-310
(my-310-python) ➜  test-310

At this point you can verify that the virtual environment is working, by using python --version:

1
2
(my-310-python) ➜  test-310 python --version
Python 3.10.0
comments powered by Disqus
source code available on GitHub
Built with Hugo
Theme Stack designed by Jimmy