# Running Scripts

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

## [Running Generic Scripts](#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'
```

![](https://755189230-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll27EGqsMiXI1nmomPu%2F-Ll27FeL8mLBf_skJM_2%2F-Ll27HPcjsK7kvG_5uhE%2FIota_iPython_run_script.png?generation=1564494343037270\&alt=media) _Figure 3 - Start menu entry on Windows where Iota IPython Terminal is highlighted_

## [Running run(scenario) Scripts](#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/scenario) object and can be executed in different ways:

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

### [Running a script as a Post-Analysis in coarse-graining](#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](#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](#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.
