Manage different Python versions with pyenv and Homebrew
Photo by Juanjo Jaramillo

Manage different Python versions with pyenv and Homebrew

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.

Homebrew and pyenv allow you to avoid this. Just follow the five steps below:

  1. Install pyenv

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 !

要查看或添加评论,请登录

社区洞察

其他会员也浏览了