2024-11-12 11:33:24 +01:00
# Python 3 `virtualenv` Setup
2020-11-13 10:42:31 +01:00
2020-11-17 11:50:24 +01:00
Multiple functional tests as well as the integration test toolchain in the µD3TN
2020-11-13 10:42:31 +01:00
project are using Python scripts to speed up development compared to pure C
2024-11-12 11:33:24 +01:00
test implementations. Some of the scripts are part of the [`python-ud3tn-utils` ](https://gitlab.com/d3tn/ud3tn/-/tree/master/python-ud3tn-utils ) package, while others are located in the [`tools/` ](https://gitlab.com/d3tn/ud3tn/-/tree/master/tools ) directory.
2020-11-13 10:42:31 +01:00
The integration test toolchain can be found in the
2024-11-12 11:33:24 +01:00
[`test/integration/` ](https://gitlab.com/d3tn/ud3tn/-/tree/master/test/integration ) directory. Both leverage a Python 3
2020-11-13 10:42:31 +01:00
implementation of several DTN protocols and convergence layers, provided in the
2024-11-12 11:33:24 +01:00
[`pyd3tn/` ](https://gitlab.com/d3tn/ud3tn/-/tree/master/pyd3tn ) directory.
2020-11-13 10:42:31 +01:00
A [venv ](https://docs.python.org/3/library/venv.html ) is used to isolate the
2020-11-17 11:50:24 +01:00
µD3TN Python environment from the system. The Makefile target `virtualenv`
2023-02-23 17:05:46 +01:00
creates a default Python virtualenv including all required Python packages.
2020-11-13 10:42:31 +01:00
```bash
make virtualenv
```
The location of the virtualenv directory can be controlled with the `VENV`
parameter. The default directory is `.venv/` .
2024-09-27 11:44:09 +02:00
```sh
2020-11-13 10:42:31 +01:00
make VENV=path/to/venv virtualenv
```
2024-11-12 11:33:24 +01:00
After the virtualenv is created, the `activate` script has to be sourced to add
2020-11-13 10:42:31 +01:00
the virtualenv Python interpreter to the `PATH` variable. Alternatively,
environment switcher like `direnv` (see below) or `virtualenvwrapper` can be
used.
2024-09-27 11:44:09 +02:00
```sh
2020-11-13 10:42:31 +01:00
source .venv/bin/activate
```
The `tools/` directory is added to the virtualenv site-packages allowing the
2020-11-19 12:38:39 +01:00
import of packages residing in `tools/` like the `pyd3tn` package, e.g.
2020-11-13 10:42:31 +01:00
```python
2020-11-19 12:38:39 +01:00
from pyd3tn.bundle7 import serialize_bundle7
2020-11-13 10:42:31 +01:00
2021-11-04 16:37:55 +01:00
serialize_bundle7("dtn://GS1/", "dtn://GS2/", b"Hello world!")
2020-11-13 10:42:31 +01:00
```
2024-11-12 11:33:24 +01:00
## Use Own Virtual Environment Handler
2023-02-23 17:02:58 +01:00
You may also use other tools or a manual approach for managing the virtual
environment, e.g., if you want it to use another path.
In this case, create and `activate` a new virtual environment using the method
of your choice and then install the dependencies:
2024-09-27 11:44:09 +02:00
```sh
2023-02-23 17:02:58 +01:00
make update-virtualenv
```
2024-11-12 11:33:24 +01:00
## Optional: `direnv` Support
2020-11-13 10:42:31 +01:00
[direnv ](https://direnv.net/ ) is an environment switcher loading environtal
variables depending on the current directory. It can be used to automatically
2024-11-12 11:33:24 +01:00
create and load Python virtual environments. This is an example `.envrc` file making use
2020-11-13 10:42:31 +01:00
of the Makefile `virtualenv` target.
```bash
layout_virtualenv() {
local venv=$1
if [ ! -d "${venv}" ]; then
make virtualenv
fi
source ${venv}/bin/activate
}
layout virtualenv .venv
```