How to Hande Multiple Python Environments Using Pyenv.

Abdullah zulfiqar
Django Unleashed
Published in
3 min readJan 28, 2024

Throughout my journey as a Python ninja, I’ve encountered the challenge of working with multiple Python versions across various projects. Initially, my approach was to manually install each version and tediously manage them via system paths. However, this method quickly became cumbersome. My game-changer moment was discovering Pyenv — a phenomenal tool that streamlined the management of multiple Python environments.

Pyenv is especially useful, even if you already have Python installed on your system. It allows you to install several versions of Python and switch between them effortlessly. With Pyenv, you can readily embrace the features of newer Python releases without the hassle of uninstalling previous versions or getting entangled in the complexities of path management for new installations. Essentially, Pyenv provides a seamless and efficient way to juggle various Python versions, ensuring that you can focus on your development work without worrying about environmental conflicts.

Consider Pyenv for Python management. Here’s why it’s great:

  • Pyenv simplifies Python installation on your system.
  • It enables you to have multiple Python versions installed simultaneously.
  • Easily pick and install any Python version quickly.
  • With Pyenv, switching between different Python versions is a breeze.

When you use Pyenv to install a specific version of Python, it does not just download a pre-compiled, ready to use version of Python (as you get from Python’s official website or a package manager like apt or yum). Instead, it downloads the source code of Python and compiles it on your machine. This process of compiling turns the source code, which is human-readable code, into an executable program that your computer can run.

However, compiling from source requires certain tools and libraries, which are referred to as build dependencies, which are not part of Python itself but are needed to process the Python source code and create an executable program.

To install the build dependencies on Ubuntu/Debian systems, you could use the following:

sudo apt-get 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 python-openssl

To install the build dependencies on MacOs, you could use the following:

brew install openssl readline sqlite3 xz zlib

As you have installed the dependencies now you are ready to install the pyenv. I would suggest installing pyenv using pyenv-installer.

curl https://pyenv.run | bash

This will install some useful plugins to enhance Pyenv functionality:

  • pyenv.
  • pyenv-virtualenv: virtualenv Python discovery plugin for pyenv-installed interpreters.
  • pyenv-update: Plugin for updating pyenv.
  • pyenv-doctor: Plugin to verify that pyenv and build dependencies are installed.
  • pyenv-which-ext: Plugin to automatically lookup system commands.

After installation you may see this in the shell:

WARNING: seems you still have not added 'pyenv' to the load path.

Load pyenv automatically by adding
the following to ~/.bashrc:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Follow above steps to integrate Pyenv into your system path and activate Pyenv/Pyenv-virtualenv auto-completion.

After that restart your shell or simply run the command below:

exec "$SHELL"

As pyenv installed so use it to install different python versions.

To list all python version use the command:

pyenv install --list

To list all jython version use the command:

pyenv install --list | grep "jython"

To check already installed versions use command:

pyenv versions
system
3.10.11
* 3.11.2 (set by /Users/abdullah/.pyenv/version)
3.11.3

running pyenv versions will list all installed pyenv version and * means you have selected this version. To install specific version such as 3.11.7 use the comand:

pyenv install 3.11.7

to check where pyenv has installed all python version use the command below:

ls ~/.pyenv/versions/

To set a specific Python version as the default for your entire system, first select the desired version, then apply the following command:

pyenv global 3.11.7

After setting a global Python version, you can still specify a different version for a particular terminal session. This version will only be active during the session. Use the command below to set a local version

pyenv local 3.11.2

Conclusion:

With Pyenv, contributing to projects supporting various environments becomes simpler. You can try new Python versions without affecting your development setup.

--

--

Abdullah zulfiqar
Django Unleashed

Software Engineer Python | Full Stack Architect | Python Engineer | Django | Flask | FastAPI | AI | Freelance