AI and VoIP Blog

VOIP | AI | Cloud | Kamailio | Open Source


Manage Multiple Python Versions with Pyenv


Managing multiple Python versions can be tricky, especially when working on different projects that require specific dependencies. pyenv allows you to install and switch between multiple Python versions easily, while pyenv-virtualenv helps you create isolated environments for different projects. This setup prevents conflicts between dependencies and keeps your development environment clean.


Step 1: Install Homebrew

Before installing pyenv, install Homebrew:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 2: Install Pyenv and Pyenv-Virtualenv

Once Homebrew is installed, use it to install pyenv and pyenv-virtualenv on both macOS and Linux:

brew install pyenv pyenv-virtualenv

For Windows, check the installation steps on the GitHub repositories:


Step 3: Configure Your Shell

For Fish Shell

Run these two commands manually for fish version >= 3.2.0

set -Ux PYENV_ROOT $HOME/.pyenv
fish_add_path $PYENV_ROOT/bin

For older versions run these two commands instead

set -Ux PYENV_ROOT $HOME/.pyenv
set -U fish_user_paths $PYENV_ROOT/bin $fish_user_paths

Now add this line to your fish config ~/.config/fish/config.fish

pyenv init - fish | source

Apply changes:

source ~/.config/fish/config.fish

For Zsh Shell

Modify your ~/.zshrc file:

nano ~/.zshrc
eval "$(pyenv init -)"
if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi

Apply changes:

source ~/.zshrc

Step 4: Install Python Versions

List Available Python Versions

pyenv install -l

Install a Specific Python Version

pyenv install 3.12.0


Step 5: Create and Manage Virtual Environments

Create a Virtual Environment inside your project or repository

pyenv virtualenv 3.12.0 myproject

Activate the Virtual Environment

pyenv local myproject

Check Python version:

python --version
# Output: Python 3.12.0

Summery of some very useful commands is given below in the table

Command Description
pyenv versions Lists all installed Python versions.
pyenv global <version> Sets the global Python version. (e.g., pyenv global 3.10.9)
pyenv local <version> Sets the local (project) Python version.
pyenv shell <version> Sets the Python version for the current shell.
pyenv uninstall <version> Uninstalls a Python version. (e.g., pyenv uninstall 3.8.12)
pyenv which python Shows the path to the active Python executable.
pyenv commands Lists all available pyenv commands.
pyenv virtualenvs Lists all created virtual environments.
pyenv uninstall <virtualenv_name> Deletes a virtual environment. (e.g., pyenv uninstall myproject)
pyenv deactivate Deactivates the current virtual environment.
pyenv virtualenv <python_version> <virtualenv_name> Creates a virtual environment with a specific Python version.
pyenv virtualenv <python_version> <virtualenv_path> Creates a virtual environment in a specific path.
pyenv version-name Shows the currently active virtual environment.

Conclusion

That’s it! With pyenv and pyenv-virtualenv, managing Python versions and virtual environments becomes much easier. Do you have any questions or run into any issues while setting this up? Let me know in the comments below—I’d love to help!

Additional Links
Install Pyenv
Install Pyenv-Virtualenv
Troubleshooting and FAQ for Pyenv

Let me know if you have any questions in the comments below!

Join 752 other subscribers

Leave a comment

Akash Gupta
Senior VoIP Engineer and AI Enthusiast



Discover more from AI and VoIP Blog

Subscribe to get the latest posts sent to your email.



Leave a comment