Have you ever had a project that required an older version of Python while your machine is running a newer one? You’re not alone – managing multiple Python versions can be a headache for developers. Luckily, pyenv makes it easy to switch between Python versions without conflicts. In this beginner-friendly tutorial, we’ll show you step-by-step how to install and use this Python version manager on any OS, so you can seamlessly work with different Python versions.
Python is constantly evolving. Every few months, a new version drops with shiny features (and sometimes breaking changes). That’s great for innovation, but it’s a nightmare when you’re juggling projects with different version requirements. One project needs Python 3.7, another wants Python 3.10, and that random script you downloaded? Yeah, it only works with Python 2.7.
Without a tool like PyEnv, your system Python setup can become a chaotic mess. Worse, you might break critical tools or apps. Managing Python versions isn’t just a convenience—it’s survival.
Here’s what good version management lets you do:
Project Isolation: Keeps your projects separate and prevents dependency conflicts. This is huge.
Testing: Allows you to test your code against multiple Python versions to ensure compatibility.
Flexibility: Gives you the freedom to use the right Python version for the job.
Pro Tip 💡: Learn to set up your python local environment the right way.
Now, there are several tools for Python version management, but my personal favourite is pyenv, and the one we’ll be focusing on this today. It’s lightweight, easy to use, and works like a charm on macOS and Linux. (Windows users might consider pyenv-win, which is a port).
Think of pyenv as your Python version remote control. Here’s a simple to understand flow diagram how a python version manager like pyenv roughly works:
Ready to get your hands dirty? Let’s get pyenv set up on your machine.
First, we need to install PyEnv. The method varies depending on your OS, but I’ve got you covered.
On macOS or Linux:
Run the following commands in your terminal:
curl https://pyenv.run | bash BashThis downloads and sets up PyEnv. Simple, right?
If you are a macOS homebrew user, you can use:
brew update
brew install pyenvBashOn Windows:
For Windows folks, grab PyEnv-win. Install it using this command on PowerShell:
Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"BashNext, update your shell configuration to recognize PyEnv. For macOS/Linux, add these lines to your .bashrc or .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 BashNow, restart your terminal, and you’re good to go.
Now, the fun part is installing Python! Let’s say you need Python 3.9. Just run:
pyenv install 3.9.16BashNeed Python 2.7 for legacy reasons? No problem:
pyenv install 2.7.18BashYou can see all available versions with pyenv install --list.
To set a default version system-wide, use:
pyenv global 3.9.16 BashBoom. Now, whenever you type python in your terminal, it’ll use Python 3.9.16.
Here’s the magic: you can set a specific version with the Python version manager just for a specific project. Navigate to your project directory and run:
pyenv local 3.8.10 BashThis creates a .python-version File in the project folder. PyEnv will automatically switch to Python 3.8.10 whenever you’re in that directory and trying to use the python command. Check the current version: python --version. You should see Python 3.8.10
Check which Python version is active with:
pyenv version BashAnd list all installed versions:
pyenv versions BashIf everything looks good, you’re all set!
python -m venv venv
source venv/bin/activateBashPro Tip: Learn more in-depth about working with Python virtual environments.
Let’s face it—things don’t always go smoothly. Here are a few hiccups you might hit and how to fix them.
pyenv Command not found.pyenv to your PATH as per step 1 above? Make sure you sourced your .bashrc or .zshrc file as well.sudo apt update && sudo apt install -y make build-essential libssl-dev zlib1g-devPyEnv is awesome, but it’s not perfect. Here are some quirks:
pyenv install -l the command.But once you get the hang of it, these downsides are small potatoes compared to the benefits.
pyenv saved my sanity, and I hope it does the same for you. Managing Python versions used to feel like wrangling cats. Now? It’s a two-second job.
Start small. Install pyenv, Try switching between versions, and use it for your next project. Before you know it, you’ll wonder how you ever coded without it.
So, what are you waiting for? Go install PyEnv and take control of your Python versions today. Trust me, your future self will thank you.
Got questions? Drop them in the comments below, or check out the official pyenv GitHub readme.
Using a version manager like pyenv is generally best due to minimized hassles, no conflict and ability to easy switching between different versions.
As recommended in the article itself, pyenv-win is the best way to install pyenv on windows.
pyenv manages Python installations, while virtualenv/venv manages environments/packages. They serve different purposes and can be used together seamlessly.
Yes, pyenv can install legacy versions like 2.7.* . Note that, python 2.x already reached their End of Life(EOL) status on January 1, 2020 and no official support anymore.
To remove a specific version of python, use command pyenv uninstall <version>. Or if you are interested remoe pyenv entirely, depending on your installation approach, use commands such as brew uninstall pyenv to uninstall pyenv from your system
Tired of repetitive tasks eating up your time? Python can help you automate the boring stuff — from organizing files to scraping websites and sending…
Learn python file handling from scratch! This comprehensive guide walks you through reading, writing, and managing files in Python with real-world examples, troubleshooting tips, and…
You've conquered the service worker lifecycle, mastered caching strategies, and explored advanced features. Now it's time to lock down your implementation with battle-tested service worker…
This website uses cookies.