Manage different Python versions with pyenv and Homebrew
David Chapuis ??
Software engineer (Back-end | Python | Automation) | Mentor | Tech writer
Working on a project that requires a different Python version that the one you have on your machine can quickly become a lonesome and painful journey.
brew install pyenv
2. Add pyenv to your shell
Bash:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
Zhsrc:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
source ~/.zshrc
If you are on Linux make sure to restart your shell to apply the changes.
3. Install the Python version you need for your project with pyenv
pyenv install 3.8.7
4. Set your global Python version
pyenv global 3.11.8
5. Set the local Python version for your project
cd path/to/your/project
pyenv local 3.8.7
All done !
Hope you enjoyed this quick guide and let me know your thoughts in the comments section !