DEV Community

Cover image for Change the Python Version as You Like: Become the Python Wizard with pyenv
Oktapian Candra
Oktapian Candra

Posted on

Change the Python Version as You Like: Become the Python Wizard with pyenv

Ah, Python developers—always on the cutting edge of innovation, until the day you clone a repository and realize it only works with Python 3.8. You’re running Python 3.10 or, worse, Python 3.11, and now all your hopes and dreams are dashed by an infuriating error message that might as well say, “Sorry, pal. Wrong Python.”

But fear not, for today we’re diving into pyenv, the magical tool that lets you change Python versions the way you’d change socks (though hopefully, you do that daily). By the end of this article, you’ll be switching Python versions like a pro, showing off to your colleagues, and maybe even naming yourself the “Keeper of the Snakes.”


Why Does Python Versioning Even Matter?

Let’s face it: Python developers have trust issues with versioning. It all started with the split between Python 2 and Python 3 (RIP Python 2, we’ll always remember your print statements without parentheses). Fast forward to today, and Python releases new versions faster than you can debug your latest TypeError.

Here’s the thing: not all Python projects are created equal. Some depend on older versions for compatibility reasons, while others thrive on the bleeding edge. And let’s not forget the chaos when you’re juggling multiple projects, each screaming for a different Python version.

This is where pyenv swoops in to save the day, like a superhero with a cape made of Python syntax.


What is pyenv?

In simple terms, pyenv is a Python version management tool that allows you to install, switch, and manage multiple Python versions on the same machine. It’s like a magical wardrobe that lets you choose the Python version you need for the job at hand.

Key Features of pyenv:

  • Install Multiple Versions: Easily install any version of Python, from the ancient days of 2.7 to the shiny new releases of 3.x.
  • Version Switching: Switch between global, local (project-specific), or shell-specific Python versions effortlessly.
  • No More Conflicts: No more fighting with your system Python installation or accidentally breaking things your operating system depends on.

Think of pyenv as your personal Python librarian. You ask for Python 3.8.10? It hands it to you on a silver platter. Need Python 3.11 for another project? No problem—it’s like having every Python version in your back pocket.


Step 1: Installing pyenv (Let’s Get This Party Started)

Before we start swapping Python versions like Pokémon cards, we need to install pyenv. Here’s how to do it:

1. Install Required Dependencies

First, let’s make sure we have all the necessary tools for building Python versions. Run the following commands based on your operating system:

sudo apt update  
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \  
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \  
xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git
Enter fullscreen mode Exit fullscreen mode

2. Install pyenv

Now, let’s install pyenv itself. Run the following command:

curl -fsSL https://pyenv.run | bash
Enter fullscreen mode Exit fullscreen mode

3. Update Your Shell Configuration

Next, we need to add pyenv to your shell startup file so it runs automatically when you open a terminal. If you’re using bash, add the following lines to your ~/.bashrc or ~/.bash_profile:

export PYENV_ROOT="$HOME/.pyenv"  
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"
Enter fullscreen mode Exit fullscreen mode

For zsh, add these lines to your ~/.zshrc instead.

Finally, reload your shell:

exec "$SHELL"
Enter fullscreen mode Exit fullscreen mode

4. Verify Installation

Check if pyenv is installed correctly by running:

pyenv --version
Enter fullscreen mode Exit fullscreen mode

If you see something like pyenv 2.3.0, you’re good to go. If not, double-check the steps above and try not to yell at your computer (it’s sensitive, you know).


Step 2: Installing and Switching Python Versions

Now comes the fun part: installing and switching between Python versions like a true Python wizard.

Installing a Python Version

Let’s say you need Python 3.8.10 for a project. Simply run:

pyenv install 3.8.10
Enter fullscreen mode Exit fullscreen mode

Want Python 3.10.9, the cool new kid on the block? No problem:

pyenv install 3.10.9
Enter fullscreen mode Exit fullscreen mode

You can see a full list of available versions with:

pyenv install --list
Enter fullscreen mode Exit fullscreen mode

Setting a Global Python Version

To set a global (default) Python version for your entire system, use:

pyenv global 3.10.9
Enter fullscreen mode Exit fullscreen mode

Now, every time you open a terminal, Python 3.10.9 will be your default.

Setting a Local Python Version

Want to use a specific Python version for a particular project? Navigate to your project directory and run:

pyenv local 3.8.10
Enter fullscreen mode Exit fullscreen mode

This creates a .python-version file in the directory, telling pyenv to use Python 3.8.10 whenever you’re working in this project.

Temporarily Changing Python Version

If you just want to switch Python versions temporarily (e.g., for a quick test), use:

pyenv shell 3.8.10
Enter fullscreen mode Exit fullscreen mode

This sets the Python version for your current shell session only. Once you close the terminal, it’s back to the default.


Step 3: Living the Dream

Congratulations! You’re now the proud owner of a flexible Python environment. No more arguing with your system Python installation or worrying about breaking dependencies. With pyenv, you can:

  • Seamlessly switch between Python versions.
  • Develop multiple projects with different Python requirements on the same machine.
  • Impress your developer friends with your newfound Python wizardry.

Bonus: pyenv + Virtual Environments = True Happiness

If you want to take things to the next level, combine pyenv with virtual environments using pyenv-virtualenv. This allows you to create isolated environments for each project, each with its own Python version and dependencies.

Install it with:

pyenv install pyenv-virtualenv
Enter fullscreen mode Exit fullscreen mode

And then create a virtual environment:

pyenv virtualenv 3.8.10 my-awesome-env
pyenv activate my-awesome-env
Enter fullscreen mode Exit fullscreen mode

Now you’re not just a Python wizard—you’re a Python ninja.


Wrapping Up

With pyenv, you’ve unlocked the power to switch Python versions as easily as you change your Spotify playlist. No more version conflicts, no more dependency nightmares—just smooth, efficient Python development.

So go forth, Keeper of the Snakes, and wield your Python versions wisely. And remember: with great power comes great responsibility… and probably a lot of late-night coding.

Happy coding, and may your pip install commands always succeed on the first try!

Top comments (0)