Install Python 3.10 using pyenv and virtualenv on a Mac
Courtesy of ChatGPT. Confirmed by a human. Lightly edited for brevity.
First, you’ll need to make sure you have Homebrew installed. If you don’t already have it installed, you can install it by running the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Next, you can use Homebrew to install pyenv. Run the following:
brew install pyenv
After installing pyenv, you need to add it to your shell’s configuration file. Depending on your shell, the file you need to modify may be different. For example, if you’re using Bash, you can add the following to your .bashrc
or .bash_profile
file:
HUMAN NOTE: If you are using zsh shell (default for macOS Catalina and/or Big Sur) you have to use ~/.zprofile
file rather than ~/.bash_profile
from StackOverflow
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
HUMAN NOTE: The `eval “$(pyenv virtualenv-init -)”` will be used later
Now that you have pyenv configured, you can use it to install Python 3.10:
pyenv install 3.10.0
Once the installation is complete, you can set the global version of Python to 3.10.0 by running the following:
pyenv global 3.10.0
This will set the global version of Python to 3.10.0, which means that any new terminal sessions you open will use this version of Python by default. Finally, you can verify it by running:
HUMAN NOTE: You need to first run source ~/.bash_profile
or create a new terminal window for the following to work
python --version
This should display Python 3.10.0
. That’s it! You’ve now set up Python 3.10.0 on your Mac!
First, make sure you have pyenv-virtualenv
installed. You can install it using Homebrew with the following command:
brew install pyenv-virtualenv
Once pyenv-virtualenv
is installed, you can create a new virtual environment by running the following:
pyenv virtualenv 3.10.0 myenv
This will create a new virtual environment called myenv
based on Python 3.10.0. You can replace myenv
with whatever name you want.
After creating the virtual environment, you can activate it with:
pyenv activate myenv
HUMAN NOTE:
If you see the error Failed to activate virtualenv
then ensure you set eval "$(pyenv virtualenv-init -)"
and then opened a new window or ran source ~/.bash_rc
This will activate the myenv
virtual environment, so any commands you run will use the version of Python installed in the virtual environment.
You can verify that the virtual environment is active by running:
which python
This should output a path that points to the myenv
virtual environment, rather than the system Python installation.
To deactivate the virtual environment and switch back to the system Python installation, you can run the following:
pyenv deactivate
That’s it! You’ve now created and activated a virtual environment using pyenv. You can use this virtual environment to install packages and dependencies specific to your project, without affecting other Python installations on your system.