What You’ll Learn

What You’ll Need

Python is a powerful and helpful programming language, but one major downside is that after a certain point, you will need to start managing dependencies. Whether you are trying to use a new library or program that requires a specific version of Python or boot an old project that was built for a legacy version of Python, there are a lot of situations where being able to set your Python version is helpful. One other benefit to keep in mind is that you don’t want to be developing Python projects with your built-in system Python (macOS/Linux users), and pyenv gives you a layer of abstraction to help manage that complexity.

Pyenv tames the chaos of dealing with multiple versions of Python in your environment. Gone are the days of manually manipulating the $PATH variable of your shell or having to type “python3” or “python3.9.” Furthermore, pyenv takes care of pip for you too. There are no more worries about whether to use “pip” or “pip3,” or if this version of pip was installed in the right Site_Packages directory for the version of Python that is being used. Finally, pyenv has some great third-party plug-ins you can use to make your experience even easier. The pyenv-virtualenv and the pyenv virtualenvwrapper integrate with pyenv and make managing multiple versions of Python with virtual environments simple.

I’m more of a “show me” type of guy, so let’s jump into a simple demo of how pyenv works.

First, pyenv works for macOS- and Linux-based systems and can be installed by pulling the repository from GitHub or by using Homebrew if you are using macOS. I am not going to dive into how pyenv is installed because the developers of pyenv already do excellent documentation, which can be found here. For Windows users, there is a forked version of pyenv for Windows simply called “pyenv for Windows” (https://github.com/pyenv-win/pyenv-win).

Once you have pyenv set up and ready to roll, you can start downloading Python. Which versions are available to download? A ton!

Use the command pyenv install --list to display what is available.

I would recommend piping the command through the less command or using grep to whittle down your search for something more specific; when I did a pyenv install --list | wc -l, I ended up with 532 lines!

Here, you can see I used grep to find all available versions for Python 3.6:

pyenv-versions

Installing a Specific Version

Let’s say you wanted to install Python version 3.6.5; we can use the pyenv install command and add the version number of Python you want to install.

In this case, we will use the command pyenv install 3.6.5. (Be aware that if you run this command without the verbose argument -v, you will see the output below until pyenv is done installing Python.)

pyenv-install

I suggest getting some coffee or watching some YouTube videos while you wait, because it will take some time. When the download is complete, you can verify that the version has been successfully installed by running the pyenv versions command, which lists all versions of Python that pyenv installed.

Setting Your Global Version

Now that you have Python 3.6.5 installed on your host, how do you set it to the version you want to use?

Pyenv allows you to set a global version that is used regardless of which directory you are issuing the Python command from. As you can see in the example below, we are able to set the global version of Python—the version that will be set by default—by issuing the command pyenv global 3.6.5. If you want a different version, you can issue the same command and change 3.6.5 to whichever version you have downloaded on your host. You are now done, and it was that easy!

pyenv-global

Setting Your Local Version

Pyenv comes with some other neat tricks too. For example, let’s say you created a directory called ~/foobar and wanted to run version 3.9.4 in this directory only. This is not a problem and can be done by issuing pyenv local 3.9.4 in the ~/foobar directory. When you use the pyenv local command with your desired local version, any time you run a Python command in that directory, it will use the local version instead of the global version.

pyenv-local

Wrap-Up

I have just scratched the surface of some cool things that pyenv can do for you, and I recommend visiting their repository at https://github.com/pyenv/pyenv to learn more about this helpful Python version management application. This application has saved me many headaches and has made my Python learning journey easier and more enjoyable. Gone are the days of having to figure out if I need to type “python3” or “python3.6” to make something work. It’s these little quality-of-life things that make the difference in successfully learning a new programming language.

Learn More