How to Import Code from External Files
With a few changes, you can import your code from anywhere
The fastest way to get started writing labelling functions is via the programmatic web interface, but at a certain point of scale or complexity you may want to manage your code slightly differently, for example via an IDE or using source control on Python files. It's very easy to modify your main.py script so that you can import functions from files on disk. This is often useful if you have a large number of labelling functions which rely on common "helper" functions.

Step 1: Create the source files

Next to the main.py file in your project folder, create an empty file named __init__.py and another file (for example, custom_functions.py) which will hold your custom code. Afterwards, your project directory should look like this:
1
.
2
├── .humanloop
3
├── __init__.py
4
├── custom_functions.py
5
└── main.py
Copied!

Step 2: Modify the launch script

Add the following lines to the main.py project-launching script:
1
import sys
2
from pathlib import Path
3
4
sys.path.insert(0, str(Path(__file__).parent))
Copied!

Step 3: Use external code in a new labelling function

With those changes made, it is possible to import from external modules inside labelling functions in the programmatic UI:
1
from custom_functions import my_helper_method
Copied!