Sunday, June 9, 2019

How to automatically setup a python3 virtualenv on CentOS 7

CentOS7 comes with Python 2 by default, but pip is going to stop supporting Python 2 from 2019.
This document shows you how to install python3 on CentOS 7 and also use a simple shell script to setup python3 virtualenv automatically with a requirements.txt file such that you do not need to worry about pip module changes.

(1) Install python3 on CentOS 7

sudo yum -y install python36


(2) A simple shell script to setup python3 virtualenv automatically

It is convenient and easier to maintain all the pip dependencies (all the pip installs to run your main python script) in a requirements.txt file. As a result, other developers know the dependencies exactly (including pip module names and versions).

The following shell script would do that for you:

$ cat setup_env.sh
#!/bin/bash

# setup env variables
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# this reset for PYTHONPATH is important, because if you have any python2 paths in your existing PYTHONPATH, virtualenv will get confused
export PYTHONPATH=''

# check whether pip, virtualenv and python36 have been installed
declare -A DEP2CMD=( \
["pip"]="sudo yum install -y python-pip && sudo pip install --upgrade pip" \
["virtualenv"]="sudo pip install --upgrade virtualenv" \
["python36"]="sudo yum -y install python36" \
)
for DEP in "${!DEP2CMD[@]}"; do
    CMD=${DEP2CMD[$DEP]}
    if ! [ -x "$(command -v $DEP)" ]; then
        echo "ERROR: $DEP is not installed. Please install it by running: $CMD" >&2
        return
    fi
done

VENV=$SCRIPT_DIR/venv
# validate the virtualenv
# if the $VENV/bin/pip and $VENV/bin/activate exist, we do not create a new virtualenv
if [ ! -f $VENV/bin/pip ] || [ ! -f $VENV/bin/activate ]; then
    rm -rf $VENV
    virtualenv -p python36 $VENV
fi

source $VENV/bin/activate
# check whether all the dependencies in requirements.txt have been installed
pip install -r $SCRIPT_DIR/requirements.txt

$ source setup_env.sh
Requirement already satisfied: PyYAML==3.13 in ./venv/lib/python3.6/site-packages (from -r /home/corpmz.com/pwang/projects/ai_common/ai_redis/requirements.txt (line 1))
You are using pip version 9.0.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

$ python --version
Python 3.6.8

$ deactivate

$ python --version
Python 2.7.5

No comments: