Python VirtualEnv Tips

This post is about installing, activate & deactivate a virtual environment in python. A virtual environment is like a virtual machine for python/like a jail in BSD/ like a docker. This is to create a python environment that is ideal for development/testing/manage inter-package dependencies.

Create a python virtual environment

########## Installation

# to install virtualenv
pip install virtualenv

# post installation virtualenv will be available as commandline tool
# you run `which virtualenv` to check if its installed
# you can also run `pip show virtualenv` for more details

########## Utilisation of virtualenv

# create a virtual envirnoment
$ virtualenv env

# create a virtual environment folder named 'env'
$ virtualenv env

# create a virtual environment folder named 'env'(with python2.7)
$ virtualenv -p python2.7 env

# create a virtual environment folder named 'env' with python3.5
$ virtualenv -p python3.5 env

Quick way install all system packages in virtual environment

# To install(copy*) all OS packages into your env
$ virtualenv --sytem-site-packages env


# using PIP to install packages from a requirements.txt
# pip install -r requirement.txt

Activate & Deactivate Virtual Env

# To install all OS packages into your virtual environment
$ virtualenv --sytem-site-packages env

# Now you will see a env folder
$ ls
env

# activate virtual env -- note how your prompt changes
$ source env/bin/activate
(env)$ 

# to deactivate
(env)$ deactivate
$

Clone a python virtual environment

############ Installation
# pip install virtualenv-clone

# copy a virtual envirnoment 'env' to 'env2'
$ virtualenv-clone env env2

To Make virtual environment inherit specific packages from your global site-packages?

Create the environment with virtualenv --system-site-packages . Then, activate the virtual environment and when you install packages, use pip install --ignore-installed or pip install -I . That way pip will install what you’ve requested locally even though a system-wide version exists. Your python interpreter will look first in the virtual environment’s package directory, so those packages should shadow the global ones.

Anaconda/Conda/Miniconda

Conda is a tool for managing and deploying applications, environments, and packages. If you are using Conda/Mini-Conda/Anaconda, then you would have use following way to create virtual environments. This is because, Conda as a package manager want to have version control, and keep records of all virtual environments.

Check conda install & version

SDS-bash3.2$ conda -V
conda 4.3.17

Check Existing Environments

SDS-bash3.2$ conda info --env
# conda environments:
#
python2                  /Users/sampathm/miniconda3/envs/python2
tensorflow               /Users/sampathm/miniconda3/envs/tensorflow
root                  *  /Users/sampathm/miniconda3

Create a virtual environment

$ conda create -n python27 python=2.7

Start a virtual environment

$ source activate python27

Create a virtual environment

$ deactivate

Remove a virtual environment

$ conda info --env
# conda environments:
#
python27                  /Users/sampathm/miniconda3/envs/python27
python2                  /Users/sampathm/miniconda3/envs/python2
tensorflow               /Users/sampathm/miniconda3/envs/tensorflow
root                  *  /Users/sampathm/miniconda3

SDS-bash3.2$ conda remove -n python27 --all

Source:
* https://conda.io/docs/using/envs.html
* http://stackoverflow.com/questions/14571454/virtualenv-specifing-which-packages-to-use-system-wide-vs-local

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s