Unity With Python
The In-Process API is meant for scripts that are stateless, which means thatthey do not keep information between runs. This is because Unity often reloadsassemblies (during domain reload), and the Python for Unity assembly willre-initialize the Python interpreter when its assembly gets reloaded. This meansanything that is stored in the Python interpreter will eventually get destroyedby the garbage collector.
Examples of scripts that are good candidates for the In-Process API are:
- Scripts processing scene data like creating, deleting or modifying assets
- Scripts using external scene description (EDL, json) in order to assemble ascene
- Validation scripts before pushing to production
- ...
Scripts that need to keep information, like scripts providing UI elements builtwith SDKs like PySide should use the Out-of-Process APIsince these scripts need to survive assembly reloads.

Python for Unity will use the system Python packaged by Apple within the Unity process. GitHub - Unity-Technologies/pythonnet: Python for.NET is a package that gives Python programmers nearly seamless integration with the.NET Common Language Runtime (CLR) and provides a powerful application scripting tool for.NET developers. The In-Process API is meant for scripts that are stateless, which means that they do not keep information between runs. This is because Unity often reloads assemblies (during domain reload), and the Python for Unity assembly will re-initialize the Python interpreter when its assembly gets reloaded. I used Keras in Python to design a neural network calculating something like a noise-reducing-function. It works pretty good so far, and now I want to use this network to clean the data inside a Unity-Project of mine. I would not have thought that this could be so difficult.
Python Script Editor
The Python Script Editor makes it easy to testin-process Python scripts and to create a menu item to allow users to easilyaccess the script.
PythonRunner.RunString
The following C# code will create a menu item that prints 'hello world' in theUnity console:
You can use any assembly that is available in C# by simply importing it with thePython import statement.
PythonRunner.RunFile
Instead of inlining your Python code inside of a C# script, you can execute awhole Python script using the PythonRunner.RunFile method. For example, thisPython script loops over all the GameObjects in a scene and makes sure all thenames end up with an underscore:
Script files can be located anywhere on your computer, and in this example wechose to put it under Assets/ensure_naming.py. You can runthis Python script from C# the following way:
Directly using Python for .NET
You can directly use Python for .NET, which wraps the CPython API. Using the C#dynamic type, you can write C# that looks like Python. Refer to the Pythonfor .NET documentation.
There are some caveats:
- Make sure Python is properly initialized by calling
PythonRunner.EnsureInProcessInitialized(). - Always grab the CPython Global Interpreter Lock (GIL).
Sample code:
Limitations
- Some important packages implemented in native code are not usable with thein-process API because they do not finalize and re-initialize cleanly. Aftera domain reload, they may be unusable or even cause a crash. Examples include
numpyandPySide. If you need these packages, use theOut-of-Process API. - On macOS, if you add a new environment variable after Python initializes,Unity will likely crash on the next domain reload. This is due to a bugin Python.
- In the current version of Python for Unity, threads that call into python --including threads spawned by Python's threading module -- do not run as oftenas might be expected. This is because the main thread holds the GIL toooften. The issue will be remediated in a future version.
Python 2.7.5 (64 bits) or later. The package does not work with Python 3.
Unity 2019.3. We recommend installing the latest version of Unity 2019 via the Unity Hub; 2019.3 is the minimum.
Optional: To run the PySide example, you will need the PySide package.
Windows
You must use Windows 10, patched to build 1803 or later.
Install the software listed above in the default locations.
When installing Python, make sure to check the option to add to the path is on.
To get PySide after installing Python, open a command terminal and run:
Mac
Install the Unity Hub and Unity in the default location.


System Python
Python for Unity will use the system Python packaged by Apple within the Unity process.
Python with PySide
For the out-of-process API with PySide, installation is more complicated because PySide support is lacking. There are a few workarounds.
The key goal:
- Within Unity, go to
Edit -> Project Settings -> Pythonand set theOut of process Pythonto point to a Python that includes PySide support. - Verify installation by running the PySide example.
We tested three options to install PySide in a form usable from Unity.
MacPorts
Unity Python Support
- Install MacPorts
- Install Python and PySide by pasting in the Terminal:
sudo port install python27 py27-pyside - Within Unity, go to
Edit -> Project Settings -> Pythonand set the out of process Python setting to read/opt/local/bin/python2.7 - Restart Unity.
Using Python from Autodesk® Shotgun®
- Install the Autodesk® Shotgun® Desktop app
- Within Unity, go to
Edit -> Project Settings -> Pythonand set the out of process Python setting to read/Applications/Shotgun.app/Contents/Resources/Python/bin/python - Restart Unity.
Using Python from Autodesk® Maya®
- Install Autodesk® Maya®
- Within Unity, go to
Edit -> Project Settings -> Pythonand set the out of process Python setting to read/Applications/Autodesk/maya2019/Maya.app/Contents/bin/mayapy - Restart Unity.
On CentOS7
Python is part of the distribution and is compatible with the integration.
To install PySide paste in a terminal:
Unity Communicate With Python
It is also possible to install PySide from pip, but it requires the qt-devel package to be installed:
