# 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](https://www.finngen.fi/en/members/recordings/finngen-users-meeting-14th-december-2021) (at 23min 23sec).

### Python Virtual Environment vs Docker Images

Compared to [Docker images](/working-in-the-sandbox/quirks-and-features/docker-images.md) 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](/working-in-the-sandbox/quirks-and-features/how-to-install-a-python-package-into-sandbox.md)). 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 --system-site-packages 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](/working-in-the-sandbox/quirks-and-features/how-to-upload-to-your-own-ivm-via-finngen-green.md))

```
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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.finngen.fi/working-in-the-sandbox/quirks-and-features/python-virtual-environment-in-sandbox.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
