BlockLune's Blog

Home Tags About

Managing Multiple Python Versions with pyenv and conda

As a CS student, I often find myself working on projects that require different Python versions, each with its own set of dependencies and libraries. In such scenarios, having a reliable and efficient method for managing these versions becomes crucial. Inspired by pyenv 与 conda 双轨制:管理 Python 版本和环境, this blog post aims to document my experience with managing multiple versions of Python using pyenv and conda.

In this workflow, we use pythons managed by pyenv by default. We’ll use conda activate to use pythons managed by conda when needed.

Note

The installation process provided here is only for macOS (and Z Shell), on other platforms such as Windows you may need to make some changes.

Install pyenv

Use Homebrew to install pyenv:

brew install pyenv

echo '# >>> pyenv initialize >>>' >> ~/.zshrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
echo '# <<< pyenv initialize <<<' >> ~/.zshrc

Restart your zsh for the changes to take effect.

Use pyenv

pyenv install --list # shows all versions available
pyenv install 3.12.2 # installs version 3.12.2
pyenv global 3.12.2 # set version 3.12.2 to be used globally in the current machine
pyenv local 3.12.2 # set version 3.12.2 to be used in the current directory and all directories below it

Install conda

Use Homebrew to install anaconda:

brew install anaconda
/opt/homebrew/anaconda3/bin/conda init zsh

Restart your zsh for the changes to take effect.

Run the following command to disable conda’s ability to automatically activate the base environment,if you need:

conda config --set auto_activate_base false