> For the complete documentation index, see [llms.txt](https://particle-analytics-1.gitbook.io/iota-python-api/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://particle-analytics-1.gitbook.io/iota-python-api/running_scripts.md).

# Running Scripts

This section describes how users can run the scripts that they have written using the [Iota Python Library](/iota-python-api/iota.md).

## [Running Generic Scripts](/iota-python-api/running_scripts.md#running-generic-scripts) <a href="#running-generic-scripts" id="running-generic-scripts"></a>

Users can run their [generic scripts](https://github.com/particle-analytics/iota-python-api/tree/79ae110f50c92abf90b9e5d280ab032d313fe62c/writing_scripts/README.md#generic-scripts) in the Iota IPython terminal by using the command [`%run`](https://ipython.org/ipython-doc/3/interactive/magics.html#magic-run). For example:

```python
%run 'C:/Users/stephen/My Iota Scripts/Analysis backhoe.py'
```

![](/files/-Ll27HPcjsK7kvG_5uhE) *Figure 3 - Start menu entry on Windows where Iota IPython Terminal is highlighted*

## [Running run(scenario) Scripts](/iota-python-api/running_scripts.md#running-run-scenario-scripts) <a href="#running-run-scenario-scripts" id="running-run-scenario-scripts"></a>

The `run(scenario)` scripts always take as argument a [`scenario`](/iota-python-api/iota/scenario.md) object and can be executed in different ways:

* [Running a script as a Post-Analysis in coarse-graining.](/iota-python-api/running_scripts.md#running-script-post-analysis-coarse-graining)
* [Running a script using the Iota `script.run` method](/iota-python-api/running_scripts.md#running-a-script-using-iota-script-run-method)
* [Runninng a script using import and run function](/iota-python-api/running_scripts.md#running-a-script-using-import-and-run-function)

### [Running a script as a Post-Analysis in coarse-graining](/iota-python-api/running_scripts.md#running-script-post-analysis-coarse-graining) <a href="#running-script-post-analysis-coarse-graining" id="running-script-post-analysis-coarse-graining"></a>

If a `run(scenario)` script has been imported into a project/scenario, users can add those scripts to the section [Post-Analysis](https://particle-analytics.gitbooks.io/iotasuite-user-manual/content/coarse_graining/cg_Output.html). Once the scripts have been added to the [Post-Analysis](https://particle-analytics.gitbooks.io/iotasuite-user-manual/content/coarse_graining/cg_Output.html) section, those scripts will be automatically run when running the coarse-graining for the scenario. A summary of the steps required to add and run the script as a Post-Analysis using the GUI is as follows:

1. Load a project
2. Import your `run(scenario)` to the `Scripts` section of the Project page.
3. Load a scenario
4. Go to coarse-graining page.
5. Set your coarse-graining settings
6. In the Output tab of the coarse-graining page, go to section "Post-Analysis" and select the script imported in the step 2.
7. Click on `Start` button to run the coarse-graining analysis including the `run(scenario)` script added in the previous step.

Note that if the script was added to the Post-Analysis sections and the coarse-graining settings were saved, executing the coarse-graining for that scenario in the Iota IPython Terminal will also run the coarse-graining analysis including the `run(scenario)`. This can be useful for the users because they can import the scripts and set the coarse-graining for multiple scenarios using the GUI and then execute coarse-graining in batch using the Iota Python Library.

### [Running a script using the iota script.run method](/iota-python-api/running_scripts.md#running-a-script-using-iota-script-run-method) <a href="#running-a-script-using-iota-script-run-method" id="running-a-script-using-iota-script-run-method"></a>

`run(scenario)` scripts that have been imported into the project/scenario can be also executed using the `run` method available for the iota scripts objects. The following example shows how to import and run a `run(scenario)` script using the `run` method:

```python
import iota
my_session = iota.session('stephen','C:/Users/stephen/ParticleAnalytics/config.ini')
my_project = my_session.get_project('Backhoe optimization')
my_script = my_project.add_script(name = 'My new script', filepath = 'C:/Users/stephen/My Iota Scripts/my_run_scenario_script.py',description = 'My new run(scenario) script')
my_scenario = my_project.get_scenario('Initial design') 
my_script.run(scenario = my_scenario)
```

If you had already imported the script into your projec/scenario you can replace the `add_script` step by the `get_script`:

```python
import iota
my_session = iota.session('stephen','C:/Users/stephen/ParticleAnalytics/config.ini')
my_project = my_session.get_project('Backhoe optimization')
my_script = my_project.get_script(name = 'My script name in Iota')
my_scenario = my_project.get_scenario('Initial design') 
my_script.run(scenario = my_scenario)
```

### [Runninng a script using import and run function](/iota-python-api/running_scripts.md#running-a-script-using-import-and-run-function) <a href="#running-a-script-using-import-and-run-function" id="running-a-script-using-import-and-run-function"></a>

Users can also run `run(scenario)` scripts using the standard Python `import`command and then calling to the `run` function already included in the `run(scenario)` script. In this case, users may need to add the directory where the script is located into the Python paths by using the `sys.path.append` function. See the example below:

```python
import iota
import sys
sys.path.append('C:/Users/stephen/My Iota Python Scripts')

my_session = iota.session('stephen','C:/Users/stephen/ParticleAnalytics/config.ini')
my_project = my_session.get_project('Backhoe optimization')
my_scenario = my_project.get_scenario('Initial design') 
import My_run_script 
My_run_script.run(scenario)
```

Note that in this case, the script does not need to be imported into a Iota project/scenario in order to execute the script.


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://particle-analytics-1.gitbook.io/iota-python-api/running_scripts.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.
