Skip to content

Install Python using Pyenv

Abstract

Your laptop comes in with a system level installed python. You do no want to mess that installation. Instead the steps described hereafter will make you install a safe and correct python3 setup.

You need python for several reasons:

  • to compile the punch documentation
  • to compile and run punch python applications
  • to use the punchbox or the standalone

Installing PyEnv and Python

Pyenv is our prefered tool to deal with python versions and environments. Checkout the the official pyenv documentation.

sudo apt update
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
git clone https://github.com/pyenv/pyenv.git --depth 1 --branch v1.2.26 ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc
export PYTHON_CONFIGURE_OPTS="--enable-optimizations  -with-lto"
export CONFIGURE_OPTS="--enable-optimizations  -with-lto"
pyenv install 3.6.8
pyenv global 3.6.8
source ~/.bashrc
sudo yum update
sudo yum install @development zlib-devel bzip2 bzip2-devel readline-devel sqlite \
sqlite-devel openssl-devel xz xz-devel libffi-devel findutils git
git clone https://github.com/pyenv/pyenv.git --depth 1 --branch v1.2.26 ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc
pyenv install 3.6.8
pyenv global 3.6.8
source ~/.bashrc
xcode-select --install
brew install openssl readline sqlite3 xz zlib
# When running Mojave or higher (10.14+) you will also need to install the additional SDK headers(https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes#3035624):
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
git clone https://github.com/pyenv/pyenv.git --depth 1 --branch v1.2.26 ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
pyenv install 3.6.8
pyenv global 2.7.16 3.6.8
source ~/.bash_profile

# In case installation of Python is failing:
# sudo rm -rf /Library/Developer/CommandLineTools
# xcode-select --install

Now, you should get the following output when using these commands:

python --version
Python 3.6.8

python3 --version
Python 3.6.8

Installing PyEnv and Python

Next install virtualenv for pyenv :

git clone https://github.com/pyenv/pyenv-virtualenv.git $PYENV_ROOT/plugins/pyenv-virtualenv
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
# restart your shell
exec "$SHELL"
git clone https://github.com/pyenv/pyenv-virtualenv.git $PYENV_ROOT/plugins/pyenv-virtualenv
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
# restart your shell
exec "$SHELL"
git clone https://github.com/pyenv/pyenv-virtualenv.git $PYENV_ROOT/plugins/pyenv-virtualenv
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
# restart your shell
exec "$SHELL"

You can then create your working virtualenv. For example:

pyenv virtualenv 3.6.8 myworkingenv
pyenv activate myworkingenv
To check :
pyenv version

Updating Pip and Wheel

Make sure you are upto date with the latest important python packages: First list the outdated packages:

pip list --outdated

Package    Version Latest Type 
---------- ------- ------ -----
pip        18.1    20.0.2 wheel
setuptools 40.6.2  45.2.0 wheel
You are using pip version 18.1, however version 20.0.2 is available.

Make sur you update pip, setuptools and install wheel before continuing to install punch requirements.

pip install --upgrade pip
pip install --upgrade setuptools
pip install --upgrade wheel

It can happen you end up with outdated packages, to uninstall these:

pip list --outdated --format freeze | xargs pip uninstall -y