Friday, October 6, 2017

Virtualenv in python

A decent python code needs multiple libraries to run. The usual approach is to install those libs and dependencies using pip install.  However when you ship the code to someone else, the environment details need to be provided including the dependencies with the version numbers. It becomes very cumbersome in that way to manage it.

A better way is to create a virtual environment. The virtual environment contains all the required dependencies in a local folder and the whole environment can be shipped. It can now run in any foreign environment without worrying about what is available or not. However this does makes the size of the artifacts quite large.

To create a virtual environment, follow the following command. (You can wrap it in a script)


# Create virtualenv and you can tell which version of
# python you want to use. If you notice, it will create
# an env directory in your local working directory.
# The env contains the whole python environment.
# You can give any other name in place of env.
# Also make sure you have virtualenv installed otherwise
# do a pip install
virtualenv --python=/usr/bin/python2.7 env

# The activate script adds to your environment the required 
# path so that python and their libraries can be invoked 
# from the local env
source env/bin/activate

# requiments.txt is a plain text file which contains all
# the libraries dependencies. See below for a sample of 
# requirements.txt. Whatever libraries are mentioned will
# get installed in the local env folder
pip install -r requirements.txt

# The relocatable is supposed to make the path relative.
# However I did not have much success with it. Putting it
# still here if it works for you. I also have read at some 
# place that the relocatable is not stable.
virtualenv --relocatable env

A sample requirements.txt

pandas==0.20.3
pymysql==0.7.11
sqlalchemy==1.1.14
log==0.0.2


Now you can put all your python artifacts parallel to env folder and make a zip and ship it. Let's assume your python code entry point is main.py and is parallel to env folder. In the other environment, unzip it and do the following

# This is a hack as the relocatable did not work properly. 
sed -i -e 's/.*VIRTUAL_ENV=.*/VIRTUAL_ENV="$ENV_DIR"/' env/bin/activate

# Activate the local python environment

source env/bin/activate

# Run the python code
python main.py

Also when do the following

which python

You will notice that the python is picked from local env.

Also see the details at http://docs.python-guide.org/en/latest/dev/virtualenvs/

No comments:

Post a Comment