|
2 | 2 | uid: overview
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -Bonsai.Scripting.Python is a [Bonsai](https://bonsai-rx.org/) interface for the [Python](https://www.python.org/) programming language. It uses [Python.NET](https://pythonnet.github.io/pythonnet/) under the hood to provide a seamless integration between the CPython runtime and data streaming in a Bonsai workflow. |
| 5 | +The Python Scripting package is a [Bonsai](https://bonsai-rx.org/) interface for the [Python](https://www.python.org/) programming language. It uses [Python.NET](https://pythonnet.github.io/pythonnet/) under the hood to provide a seamless integration between the CPython runtime and data streaming in a Bonsai workflow. |
6 | 6 |
|
7 |
| -You can use Bonsai.Scripting.Python to run native Python scripts, import any module available to Python, and read or write to any variable in a named scope. The package is designed to work with any Python version from 3.7 onwards, and you can also use it in combination with [Python virtual environments](https://docs.python.org/3/tutorial/venv.html) to fully isolate your project dependencies. |
| 7 | +You can use the Python Scripting package to run native Python scripts, import any module available to Python, and read or write to any variable in a named scope. The package is designed to work with any Python version from 3.7 onwards, and you can also use it in combination with [Python virtual environments](https://docs.python.org/3/tutorial/venv.html) to fully isolate your project dependencies. |
8 | 8 |
|
9 |
| -## Installing the package |
| 9 | +## How to install |
10 | 10 |
|
11 |
| -To install Bonsai.Scripting.Python use the Bonsai package manager and search for the **Bonsai - Python Scripting** package. |
| 11 | +1. Download [Bonsai](https://bonsai-rx.org/). |
| 12 | +2. From the package manager, search and install the **Bonsai - Python Scripting** package. |
| 13 | + |
| 14 | +## Create a Python environment |
| 15 | + |
| 16 | +In addition to the Python Scripting package you need to have a version of [Python](https://www.python.org/) installed in your system. We recommend installing the official distributions and using `venv` to create virtual environments to run your specific projects. |
| 17 | + |
| 18 | +To create a virtual environment you can run the following command from inside the folder where you want to install the environment: |
| 19 | + |
| 20 | +```ps |
| 21 | +python -m venv example-env |
| 22 | +``` |
| 23 | + |
| 24 | +## Create a Python runtime |
| 25 | + |
| 26 | +The [CreateRuntime](xref:Bonsai.Scripting.Python.CreateRuntime) node launches the Python kernel and creates the main global scope for running Python scripts. You must set the [PythonHome](xref:Bonsai.Scripting.Python.CreateRuntime.PythonHome) property to the environment folder (or your Python home directory) before starting the workflow: |
| 27 | + |
| 28 | +:::workflow |
| 29 | + |
| 30 | +::: |
| 31 | + |
| 32 | +Alternatively, you can activate the virtual environment *before* starting Bonsai. In this case you do not need to specify the Python home directory. |
| 33 | + |
| 34 | +> [!Tip] |
| 35 | +> You can set the value of the [**ScriptPath**](xref:Bonsai.Scripting.Python.CreateRuntime.ScriptPath) property to specify a main script that will be run when the Python runtime is initialized. This script can be used to import all the main modules to be used in your workflow, or initialize any state variables to be manipulated during execution. |
| 36 | +
|
| 37 | +## Run Python code |
| 38 | + |
| 39 | +The [Eval](xref:Bonsai.Scripting.Python.Eval) operator can be used anywhere in your workflow to interact with the Python runtime, just as if you were writing code in the Python REPL: |
| 40 | + |
| 41 | +:::workflow |
| 42 | + |
| 43 | +::: |
| 44 | + |
| 45 | +You can also use the [Exec](xref:Bonsai.Scripting.Python.Exec) operator to run Python statements dynamically, for example to import new modules: |
| 46 | + |
| 47 | +:::workflow |
| 48 | + |
| 49 | +::: |
| 50 | + |
| 51 | +## Read and write state variables |
| 52 | + |
| 53 | +To interface with Python state variables, you can use the [Get](xref:Bonsai.Scripting.Python.Get) and [Set](xref:Bonsai.Scripting.Python.Set) operators: |
| 54 | + |
| 55 | +:::workflow |
| 56 | + |
| 57 | +::: |
| 58 | + |
| 59 | +> [!Warning] |
| 60 | +> All the operators in the Python Scripting package run under the Python [Global Interpreter Lock](https://docs.python.org/3/glossary.html#term-global-interpreter-lock). This means that although execution of Python code can be triggered asynchronously anywhere in the workflow, there will be a bottleneck when accessing the interpreter. Because of this, we currently do not recommend running large number of parallel calls to Python. |
| 61 | +
|
| 62 | +## Scripting Extensions |
| 63 | + |
| 64 | +Custom script files can be executed when creating the Python runtime with [CreateRuntime](xref:Bonsai.Scripting.Python.CreateRuntime) or when creating a new module with [CreateModule](xref:Bonsai.Scripting.Python.CreateModule). |
| 65 | + |
| 66 | +To edit script files we recommend [Visual Studio Code](https://code.visualstudio.com/) and the [Python extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-python.python). If both your virtual environment and your custom Python scripts are placed relative to your main workflow, VS Code can be launched from the Bonsai IDE and will be able to correctly pick up the right environment when editing the scripts. |
0 commit comments