Python DevOps
pyenv, virtualenv, freeze
Aug 12, 2018     2 minutes read

1. What are pyenv, virtualenv and freeze and why would you use them?

2. Installation

Installation is trivial. All you have to do is to clone two repositories from github (just copy-paste the code):

pyenv

cd
git clone git://github.com/yyuu/pyenv.git .pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc

virtualenv

git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
source ~/.bashrc

Everything is described in “definitely the best tutorial”; link available in section 4.

3. A “Hello World” example

Say you want to create a new project with Python 3.6.0.

pyenv install 3.6.0

and you want to write an application which uses numpy and flask. First you have to decide how you’re going to call your app. And then create a virtualenv with that name.

pyenv virtualenv 3.6.0 awesome_app

In order to install packages for this specific Python version and use it’s interpreter, let’s activate our brand new environment:

pyenv activate awesome_app

As you can see, the prompt has changed. Now you can install any package you want with pip:

pip install --upgrade pip  # this may help at the beginning
pip install flask pytest

and check the Python’s version if it is actually 3.6.0:

python --version

Finally, you can list all the packges that are installed under this Python’s version:

pip freeze

It is a good practice to keep the list of you packages in a requirements.txt file

pip freeze > requirements.txt

so you can easily install them with

pip install -r requirements.txt

when you download your repo from a remote repository.

When the work is done, type

pyenv deactivate

4. Easily forgettable commands: