How to work with Python in 2020

5 min. read

Four years ago I had to use Python at work, so I had to figure out the best way to work with it. I've recently recovered interest again on Python as I am playing with Machine Learning stuff, but my setup now is different.

rbenv ➡️ pyenv: Installing several versions of Python in parallel

Is there an equivalent to rbenv in Ruby? Yes, it's called pyenv and it's basically a translation of rbenv to Python. How cool is that. The philosophy, installation and use is the same. As with rbenv, I cloned the repo and configured my .bashrc file.

git clone ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
exec $SHELL -l

Now all Python versions will be installed under .pyenv/versions and everytime you use pip (the equivalent of gem), it will install it inside of the relevant Python version.

Before installing a Python version you may have to install the relevant build dependencies for Python. For example, in Ubuntu:

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

and then:

pyenv install --list
# Find your version
pyenv install <VERSION>

Now go to your project folder and do:

pyenv local <VERSION>

Just like Ruby! Awesome. And it will also generate a local file with the Python version you are using.


In my previous post four years ago, I had praised virtualenvwrapper. You can not really make it work with pyenv, but the author of pyenv has made pyenv-virtualenvwrapper so that you can still enjoy using it. You can also git clone it and configure your .bashrc file.

(I set my environments in the opt folder, but the usual is to put them in your home directory)

# Make sure you have configured a global Python with:
#   pyenv global <VERSION>

git clone $(pyenv root)/plugins/pyenv-virtualenvwrapper
echo 'export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV="true"' >> ~/.bashrc
echo 'export VIRTUALENVWRAPPER_PYTHON=/home/USERNAME/.pyenv/shims/python' >> ~/.bashrc
echo 'export WORKON_HOME=/opt/virtualenvwrapper' >> ~/.bashrc
echo 'pyenv virtualenvwrapper_lazy' >> ~/.bashrc

The docs tell you that when you install a new version of Python, you have to reinstall virtualenvwrapper for that version with pyenv virtualenvwrapper_lazy.

Now you can go to your project and create a virtual environment like you did before:

:) mkvirtualenv -a . PROJECT_NAME

Now you are in your environment!

Check other commands in the home page

bundler ➡️ pipenv Package version managing

After this the best you can do is to install pipenv to manage your dependencies like bundler. It will create a Pipfile (Gemfile) and a Pipfile.lock (Gemfile.lock)

(PROJECT-NAME) :) pip install pipenv
(PROJECT-NAME) :) pipenv install requests
Creating a Pipfile for this project…
Installing requests…
Adding requests to Pipfile's [packages]…
✔ Installation Succeeded
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success!
Updated Pipfile.lock (85b5da)!
Installing dependencies from Pipfile.lock (85b5da)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 46/46 — 00:00:11