> For the complete documentation index, see [llms.txt](https://docs.tensorleap.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tensorleap.ai/tips-and-tricks/import-external-code.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.tensorleap.ai/tips-and-tricks/import-external-code.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
