# Import External Code

To include any existing code in your Tensorleap [Dataset Script](/user-interface/resources-management/integration-scripts.md#dataset-script) you can create a library and import all your existing classes and functions to your script.&#x20;

Here is an example of how to import a [Custom Layer](/tensorleap-integration/writing-integration-code/custom-layers.md) from an external file.

## Custom Layer Import from File

1. First, create a file that includes the custom layers that are included in your model. Here we show an example of a custom dense layer.

{% code title="custom\_dense.py" %}

```python
import tensorflow as tf

class CustomDense(tf.keras.layers.Layer):
    def __init__(self, n, **args):
        super(CustomDense, self).__init__()
        self.n = n
        self.dense = tf.keras.layers.Dense(self.n)
    
    def call(self, inputs):
        return self.dense(inputs) 
    
    def get_config(self):
        config = super().get_config()
        config.update({
            "n": self.n,
        })
        return config
```

{% endcode %}

2\.   In your [Tensorleap Data Folder](broken://pages/R0xvhzASaO2FYTmpojv5#tensorleap-data-folder), create a new folder that will include the file you've created and an empty **\_\_init\_\_.py** file. For example:

```
|
├── ....
|   ├── custom_layers/
|           ├── __init__.py
|           └── custom_dense.py
```

3\.  Now, use one of two possible methods to import the classes Into the dataset script.

## Append system path

{% code title="Tensorleap dataset script" %}

```python
# custom function imports
import sys
sys.path.append('/home/username/tensorleap/data/custom_layers')
from custom_dense import CustomDense
...
leap_binder.set_custom_layer(CustomDense, 'CustomDense')
```

{% endcode %}

OR

### Import using `importlib`

{% code title="Tensorleap dataset script" %}

```python
# custom function imports
import importlib.util
import sys
from inspect import getmembers, isclass
...
# adding custom layers from a file using imports
spec = importlib.util.spec_from_file_location('custom_layers.custom_dense',
 '/home/username/tensorleap/data/custom_layers/custom_dense.py')
custom_layers_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(custom_layers_module)

custom_layers_titles = getmembers(custom_layers_module, isclass)
for i in range(len(custom_layers_titles)):
    class_name = custom_layers_titles[i][0]
    function_class = getattr(custom_layers_module, class_name)
    leap_binder.set_custom_layer(function_class, class_name)
...
```

{% endcode %}

Parsing the dataset will now import the custom layers from the external files and add it to your Tensorleap environment.


---

# 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.tensorleap.ai/tips-and-tricks/import-external-code.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.
