> For the complete documentation index, see [llms.txt](https://www.opal-rt.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opal-rt.com/docs/rt-lab/automation-api-and-scripting/introduction-to-automation/use-python-script.md).

# Automate with a Python Script

## Requirements <a href="#requirements" id="requirements"></a>

Please refer to the [Software Compatibilities](https://www.opal-rt.com/docs/rt-lab/overview/workstation-requirements/software-compatibilities.md#compatible-python-versions) page to know which **version of Python** is supported in this RT-LAB version.

## The RT-LAB Python Interface

RT-LAB includes a **Python interface** (script editor and interpreter) ​which allows the user to:​

* **Run** Python Scripts​
* **Debug** Python Scripts

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-4f6c72753edf81e883c1e428a2b1741c3637fd0f%2FRTLAB-console-script.png?alt=media" alt=""><figcaption></figcaption></figure>

## Implement a Python script

{% stepper %}
{% step %}

### Create a new Python script in the project

Open your project and add a new file.

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-780efe328ba3036548a0454a84349f0f799988ba%2FRTLAB-add-new-script-py.png?alt=media" alt=""><figcaption></figcaption></figure>

Select the destination folder, enter the file name, and ensure the **.py** extension is included.

Use the [RT-LAB Python Interface](https://www.opal-rt.com/docs/rt-lab/automation-api-and-scripting/introduction-to-automation.md#rt-lab-python-interface) to edit your script
{% endstep %}

{% step %}

### Edit the script

**RtlabApi.py** is the module to use when creating Python scripts, as it serves as the primary interface to the RT-LAB API.

When RT-LAB is started (specifically the [MetaController](https://www.opal-rt.com/docs/rt-lab/automation-api-and-scripting/introduction-to-automation/launch-the-metacontroller.md) process), the **PYTHONPATH** is automatically configured to include the appropriate RT-LAB Python directories. As a result, importing RtlabApi.py in your scripts will link to the version corresponding to the most recently launched RT-LAB instance.

<pre class="language-python" data-overflow="wrap" data-line-numbers><code class="lang-python">import RtlabApi

ProjectPath ='C:\RT-LAB PROJECT\API_basic_example\API_basic_example.llp'
<strong>RtlabApi.OpenProject(ProjectPath)
</strong><strong>...
</strong></code></pre>

Consult the [Python API Reference](https://www.opal-rt.com/docs/rt-lab/automation-api-and-scripting/rt-lab-api/python-rtlab-api-reference.md) for a complete list of available functions and constants.

Leverage additional APIs such as the [**ePHASORSIM API**](https://www.opal-rt.com/docs/rt-lab/automation-api-and-scripting/python-ephasorsim-api.md), [**Datalogger API**](https://www.opal-rt.com/docs/rt-lab/automation-api-and-scripting/python-datalogger-api-reference.md), [**ScopeView API**](https://www.opal-rt.com/docs/scopeview/automation-api-and-scripting/use-python-scopeview-api.md), [**Dashboard API**](https://www.opal-rt.com/docs/rt-lab/automation-api-and-scripting/python-dashboard-api.md), or [**Orchestra API**](https://www.opal-rt.com/docs/rt-lab/automation-api-and-scripting/orchestra-api-reference.md) to enhance functionality and integration capabilities.
{% endstep %}

{% step %}

### Execute the script

* **Run mode​** : Executes the entire script from start to finish, stopping only if an error occurs.
* **Debug mode:** Enables step-by-step execution and variable monitoring. A breakpoint must be set beforehand, and you can press **F5** to advance to the next step.
  {% endstep %}
  {% endstepper %}


---

# 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://www.opal-rt.com/docs/rt-lab/automation-api-and-scripting/introduction-to-automation/use-python-script.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.
