Python Virtual Environment in Sandbox

Python Virtual Environment is a Python tool for project isolation. It enables the installation of any Python module you like independently from other python modules in Sandbox.

Python Virtual Environment is basically a folder within your IVM that contains all necessary components: site-packages, symlinks, and scripts to execute Python programs using those Python interpreter and site packages installed in Python Virtual Environment.

In addition, the virtual environment enables installation directly to your home directory and automatic environment configuration so that your packages are found from that environment.

Tutorial

See an example of how to install python packages to Sandbox from Users' meeting recording (at 23min 23sec).

Python Virtual Environment vs Docker Images

Compared to Docker images Python Virtual Environment use the same system as IVM and images cannot be copied from repositories. With Virtual Environments Sandbox users can install packages through /home/ivm thus needing no request to the service desk (finngen-servicedesk@helsinki.fi). In addition, Virtual Environments are users' personal environments, unlike Docker images which are shared with all Sandbox users.

Example

In this tutorial we will make a Python Virtual Environment for a package we install to python3 (instead of python2, see installation to python2 here). In our example, we use the yhaplo package.

Step 1:

In Sandbox open Terminal Emulator and go to your home directory

cd /home/ivm/

Step 2:

Make a virtual environment named yhaplo_env

python3 -m venv yhaplo_env

Step 3:

Activate virtual environment

source yhaplo_env/bin/activate

Step 4:

The name of your virtual environment will appear indicating you are inside a virtual environment

(yhaplo_env) ivm:~$

Step 5:

Copy dependencies from green bucket. (If the dependencies are not yet in green bucket see instructions How to upload to your own IVM via /finngen/green)

cp /finngen/green/six-master.zip .
cp /finngen/green/yhaplo-master_stg.zip .

Step 6:

Unzip dependencies

unzip six-master.zip
unzip yhaplo-master_stg.zip

Step 7:

Move to unziped folders and install six and yhaplo to virtual environment. Note that the code must be run in the package's folder where setup.py exists.

cd six-master
pip3 install --editable .
cd ..
cd yhaplo-master
pip3 install --editable .

Step 8:

Check the yhaplo

which yhaplo

will show

/home/ivm/yhaplo_env/bin/yhaplo

Step 9:

Deativate virtual environment

deactivate yhaplo_env

Step 10:

Check yhaplo again just to compare

which yhaplo

will show

/usr/local/bin/yhaplo

The input data is now editable in /home/ivm/yhaplo-master/input/ as required by the package.

Last updated