Setting up python virtual environment

 

Python virtual environments allow you to have independent python installations with own set of packages. It is the recommended way of using python at CAMK. Examples below show how to prepare python virtual environment with matplotlib for python 2.x and python 3.x (tested on psk).


 

Python 2.7.9 example

 

cd
mkdir -p .pythons/2.7
cd .pythons
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar zxvf Python-2.7.9.tgz
cd Python-2.7.9
make clean
./configure --prefix=$HOME/.pythons/2.7
make -j4
make install
cd ..
rm -rf Python-2.7.9*


At this point core python 2.7 installation is in $HOME/.pythons/2.7. Now we will create virtual environment based on this python version.

cd $HOME
mkdir python-virtualenvs
cd python-virtualenvs
virtualenv myenv2.7 --python=~/.pythons/2.7/bin/python


To work in specific virtual environment issue command
     source ~/python-virtualenvs/myenv2.7/bin/activate
(to leave type deactivate )

To install specific packages use pip in active virtual environment e.g.:

pip install nympy
pip install scipy
pip install matplotlib
pip install pandas


To list installed packages type:

pip freeze


Python 3.4.5 example

 

The procedure for python 3 is almost the same. The only difference is that python 3 comes with virtual environment tool built-in (pyvenv).

cd
mkdir -p .pythons/3.4
cd .pythons
wget https://www.python.org/ftp/python/3.4.5/Python-3.4.5.tgz
tar zxvf Python-3.4.5.tgz
cd Python-3.4.5
make clean
./configure --prefix=$HOME/.pythons/3.4
make -j4
make install
cd ..
rm -rf Python-3.4.5*


Now let's create virtual environment.

cd $HOME
mkdir python-virtualenvs
cd python-virtualenvs
~/.pythons/3.4/bin/pyvenv myenv3.4


To work in specific virtual environment issue command
     source ~/python-virtualenvs/myenv3.4/bin/activate
(to leave type deactivate )

To install specific packages use pip in active virtual environment e.g.:

pip install nympy
pip install scipy
pip install matplotlib
pip install pandas


To list installed packages type:

pip freeze