# Python

## Task Preparation

### Source Code Structure

In order to compile your source code, make sure you have the following file structure within the `❮sourceCodePath❯` folder that will be built.

📁 src \
└─ 📄 task.py

### Supported Libraries

We support most of the [Python's Standard Library](https://docs.python.org/3/library/index.html).  Please read about using [the Python standard library on WebAssembly platforms](https://docs.python.org/3/library/intro.html#webassembly-platforms). Additionally, we are limited by requirements for determinism. Therefore, modules for **threading**, **multiprocessing**, **subprocesses**, or **sockets** are not supported.

Truebit includes some [built-in](#import-built-in) libraries for you to use in your programs. We will be adding more built-in libraries, so please keep checking this page for updates.

* [NumPy version 1.26.4](https://numpy.org/devdocs/release/1.26.4-notes.html)
* [pandas version 2.2.0](https://pandas.pydata.org/docs/dev/whatsnew/v2.2.0.html)

### External files

To include your external files,  you need to use the TAR utility to pack them together. In this manner files will be packaged into a single file and can also be easily referenced from your code.&#x20;

**Setup TAR**

{% tabs %}
{% tab title="Linux" %}

```bash
sudo apt install TAR 
```

{% endtab %}
{% endtabs %}

**Creating The TAR file**

To create a single `fs.tar` file that includes all the folders and files you want, simply run the following command:

```bash
tar -H ustar -cvf fs.tar modules
```

In this example, the tar command will generate a file called `fs.tar` which contains all the files found in the **modules** directory.

**Considerations**

* `fs.tar` is a reserved name for tar files in Truebit. You must always name the file in that way.
* The files included in the `fs.tar` file are read-only.

{% hint style="info" %}
Click [Here](https://www.gnu.org/software/tar/manual/html_section/All-Options.html) for more information about TAR
{% endhint %}

### Input Data Files

You can use your own external data files in your function task. To include your data files,  you need to use the TAR utility to pack them together and then, reference those files in your source code.

{% hint style="info" %}
Click [here](https://devs.truebit.io/developing-truebit-tasks/how-to-create-function-tasks/function-task-examples/numpy-example) to see an example of calling external input data files
{% endhint %}

### Modules

#### **Import External Modules**

To import external modules, pack them all together into an `fs.tar` file, then reference them in your Python code like this:

```python
sys.path.append(os.path.abspath("modules"))

from fibonacci import fibonacci
from reverse import run_task as reverse_alphabet
```

#### **Import External Functions**

To import external functions, pack them all together into an `fs.tar` file, then reference them in your Python code like this:

```python
fibonacci = import_function_from_file('modules/fibonacci.py', 'fibonacci')
reverse_alphabet = import_function_from_file('modules/reverse.py', 'run_task')
```

In the previous example, there's a function called `fibonacci` in the `modules/fibonacci.py` file, and another function called `run_task` in the `modules/reverse.py` file.

#### Import Standard

To use pure python code, just import the stardard set of libraries

```python
import os
```

#### Import Built-in&#x20;

To include the built-in libraries provided by Truebit, you will need to import them in the following way

```python
import numpy as np
import pandas as pd 
```
