Running your python code in unity

Apr 24 · 4 min read

Python is one of the top 10 popular programming languages of 2021. Python is a general purpose and high level programming language. You can use Python for developing desktop GUI applications, websites and web applications. Also, you can use Python for developing complex scientific and numeric applications. Python is designed with features to facilitate data analysis and visualization. You can take advantage of the data analysis features of Python to create custom big data solutions without putting extra time and effort.

Currently Unity 3D developer used to code in C# because Unity 3D supports C# by default. But python is known for simplicity and rich in library support for data science. Today we are going to explore python in Unity 3D.

Getting Started

There are different ways to integrate python in Unity. Here we are going to explore python integration in two ways.

1. Unity Python Package

UnityPython is a plugin for Unity3D that provides support for running Python 2.x code in Unity3D on any platform which supports System.Reflection.Emit

The build target must support System.Reflection.Emit. here is a chart of platform support for Emit:

Download IronPython dll files and place them into Assets/Plugins directory. unity-python

Then, go to Edit > Project Settings > Player > Other Settings > Configuration and change Api Compatability Level to ".NET 4.x"

Assume you have a small code snippet in Python like this:

import randomclass Test():
def __init__(self, name): = name
def display(self):
return "Hi, " +
def random_number(self, start, end):
return random.randint(start, end)

You can use it from C# like this

var engine = Python.CreateEngine ();
ICollection<string> searchPaths = engine.GetSearchPaths ();
//Path to the folder of
searchPaths.Add (Application.dataPath);
//Path to the Python standard library
searchPaths.Add (Application.dataPath + @"\StreamingAssets" + @"\Lib\");
engine.SetSearchPaths (searchPaths);
dynamic py = engine.ExecuteFile (Application.dataPath + @"\StreamingAssets" + @"\Python\");dynamic test = py.Test ("Codemaker");
greeting.text = "Greeting: " + test.display ();
randomNumber.text = "Random Number: " + test.random_number (1, 5);

2. Python for Unity 2.0.1 preview package

Python for Unity facilitates Unity’s interaction with various media and entertainment industry applications and ensures that you can integrate Unity into a broader production pipeline seamlessly. It allows access from Python to the full C# API of UnityEditor, UnityEngine, as well as your own C# APIs added to your Unity projects, and running Python code from within C#.

  • 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.

Python for Unity requires some manual installation:

  1. First, follow the installation instructions in the manual
  2. Next, install Python for Unity itself: Edit your Unity manifest.json file (in the Packages folder of your project) so that it starts with
"dependencies": {
"com.unity.scripting.python": "2.0.1-preview.2",

Create a script named HelloWorld.cs and attach it to the Main Camera.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HelloWorld : MonoBehaviour
// Start is called before the first frame update
void Start()
Debug.Log("Unity: Hello World");
print ("Python: Hello World");
// Update is called once per frame
void Update()


Also you can try like this,

Run your application.

Go to Window -> General -> Python Console
Type the following code in the bottom window and click on the Execute button:

import UnityEngine
print ("Python: Hello World from python console")
UnityEngine.Debug.Log("Unity: Hello World from unity console")

The main disadvantage of this package is that it supports only on Unity Editor. It won’t add any builds.

There you have it! Your own Unity app with python support :)

I hope you had fun reading and/or following along. If you are interested in further exploring, here are some resources I found helpful along the way:

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store