PIP-Talk - Week 50

The topic for this week is: Pyenv

Do you need to evaluate or switch between different versions of Python ?

Then you will find this piptalk especially interesting.


Background

Licence: MIT

Venv or virtualenv will make life easy for you when you need to handle different versions or dependency’s of your modules/packages. But how do you handle different/multiple python versions in a simple way ? This is where pyenv comes into the picture.

pyenv is a simple python version management tool that lets you easily switch between multiple versions of Python.

Pyenv will download the actual source you want and then build and install that python version to your local machine. Python will be installed in a separate . pyenv folder in your home folder and will not interfere with your system installed python. In the background pyenv will manage the running versions by change the $Path variable.

Pyenv also playes very well with your current venvs or virtualenvs. But get the full power you should use the built-in: pyenv-virtualenv. pyenv-virtualenv can create your virtualenvs and assign then to a specific Python version. Super neat !


Why Not Use System Python ?

If you install a new version of Python and aren’t careful to install it into your user space, you could seriously damage your ability to use your OS. Also, you don’t really have much control over what version of Python comes installed on your OS. If you want to use the latest features in Python, and you’re on Ubuntu for example, you might just be out of luck.


Why not use your OS package manager ?

Package managers (for example: apt, yum, brew) tend to install their packages into the global system space instead of the user space. Again, these system level packages pollute your development environment and make it hard to share a workspace with others.


Why use pyenv ?

With pyenv you can:

  • Install Python in your user space
  • Install multiple versions of Python
  • Specify the exact Python version you want
  • Switch between the installed versions

pyenv lets you do all of these things and more.


Install pyenv

Installing pyenv can require several steps, depending on the platform or installation method. Sometimes the most simple way is to use your standard installation tool (apt-get, brew, yum, pacman etc)

MacOs

Example:

brew update
brew install pyenv

Arch linux

sudo pacman -S pyenv

Ubuntu

Install all required prerequisite dependencies:

sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Download and execute installation script:

curl https://pyenv.run | bash

Add the following entries into your ~/.bashrc file (or .zshrc ):

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

Restart your shell

exec $SHELL

Other platforms

https://github.com/pyenv/pyenv#installation


Basic commands

1. To install specific version use install flag:

$ pyenv install 3.8.0

2. To list all available installed versions of Python on your system:

$ pyenv versions

3. To set a specific version of Python global (system-wide) use global flag:

$ pyenv global 3.8.0

4. To set a specific version of Python local (project-based) use local flag:

$ pyenv local 3.8.0

5. Create a new virtualenv with a specifiq Python version (2.7.10)

$ pyenv virtualenv 2.7.10 my-virtual-env-2.7.10

6. List all virutalenvs

$ pyenv virtualenvs

7. Delete a virutalenv

$ pyenv virtualenv-delete my-virtual-env-2.7.10

Sources