Image for post
Image for post

Installing Pythons with PyEnv

Using pyenv to manage Python 2 and Python 3 environments

Python is extremely popular these days for a variety domains. Python is popular for web programming, with famous web frameworks like Flask, Tornado, Django.

The Problem and Solution

Image for post
Image for post

The Installation

For pyenv you need a *nix environment, such as Linux and macOS. Unfortunately, Windows is not supported. Before installing PyEnv, you’ll need to install compilers, libraries, and headers, as PyEnv will download and compile Python from source.

macOS installation

You can easily install PyEnv along with some required libraries using Homebrew:

# install pyenv
brew install readline xz pyenv pyenv-virtualenv
# setup
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Linux Libraries Prereq: Ubuntu and Debian

One you have the build tools installed, you need some libraries to install python:

sudo apt-get install -y \
libbz2-dev \
libsqlite3-dev \
llvm \
libncurses5-dev \
libncursesw5-dev \
tk-dev \
liblzma-dev

Linux Libraries Prereq: CentOS (RHEL) and Fedora

Once have the build tools installed, you need some libraries to install python:

sudo yum install \
zlib-devel \
bzip2 \
bzip2-devel \
readline-devel \
sqlite \
sqlite-devel \
openssl-devel \
xz \
xz-devel \
libffi-devel

Linux installation

After installing build tools and other required libraries, you can install PyEnv using the pyenv-installer:

# install pyenv
PROJ
=pyenv-installer
SCRIPT_URL=https://github.com/pyenv/$PROJ/raw/master/bin/$PROJ
curl -L $SCRIPT_URL | bash
# setup
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

The Usage

PyEnv is install per user, where you can use these different ways:

  • shell: updates the current session
  • local: use the local project version specified in a .python-version file. Python is set at the current directory level, useful for managing python per project.

Install and Default Python Versions

As an example, this installs current latest stable versions of Python 3 and Python 2, and then has defaults for Python 2 platform for python command.

pyenv install 3.7.1
pyenv install 2.7.15
pyenv global 2.7.15 3.7.1

Update Pip and Setuptools

It is good practice to update both Pip and Setuptools. For those unfamiliar with this, these are package installers, where pip is the most popular.

pip install --upgrade pip setuptools

Installing Python packages

Now that you have the desired Python version install, we can try out some packages. Packages that have command line tools can be made available to your current python version by running pyenv rehash.

# install some tools
pip install fabric
pip install flask
# make the tools available
pyenv rehash

VirtualEnv Integration

PyEnv includes a plug-in pyenv-virtualenv that includes integration with virtualenv. With this you can initialize and automatically switch into both your desired python version as well as a segregated environment for your python packages installed through pip.

Alternative for Windows Users

The tool pyenv is not an option for Windows users unfortunately. There are alternatives, one is the conda tool.

Conda

I experimented with MiniConda, it installs Python 2, and you can use this to install other versions of Python. It only has startup scripts for command shell with a supplied activate.bat file. So you would have to create a PowerShell equivalent for use with PowerShell.

choco install miniconda
cd C:\ProgramData\Miniconda2\Scripts\
activate.bat
C:\ProgramData\Miniconda2

VirtualEnv Integration

PyEnv includes a plug-in pyenv-virtualenv that includes integration with virtualenv. With this you can initialize and automatically switch into both your desired python version as well as a segregated environment for your python packages installed through pip.

Testing Simple Flask Server

I created a small hello Flask server in experimentation with various web micro-frameworks. You can use this to test out your Python environment. (Note these instructions are for *nix systems running GNU bash)

mkdir -p hello_flask && cd hello_flaskcat <<-PKG_MANIFEST > requirements.txt
Flask >= 0.10.1
itsdangerous >= 0.24
Jinja2 >= 2.8
MarkupSafe >= 0.23
Werkzeug >= 0.11.4
PKG_MANIFEST
cat <<-PYTHON_VERSION > .python-version
2.7.11
PYTHON_VERSION
cat <<-SOURCE_CODE > app.py
#!/usr/bin/env python
from flask import Flask
app = Flask(__name__)

@app.route('/')
@app.route('/hello') # this route is not working
@app.route('/hello/')
def hello_world():
return 'Hello World!\n'

@app.route('/hello/<username>') # dynamic route
def hello_user(username):
# show the user profile for that user
return 'Why Hello %s!\n' % username

if __name__ == '__main__':
app.run(host='0.0.0.0') # open for everyone
SOURCE_CODE
# install project python version
pyenv
install $(cat .python-version)
pyenv local
# create virtualenv
pyenv
virtualenv hello-flask
# activate virtualenv
pyenv
local hello-flask
pip install -r requirements.txt
# run the server
./app.py

Wrapping Up

There you have it, a way to consistently manage Python versions per user and project (local directory) level, and a small Python demonstration on how to create a small web service with Flask.

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store