docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Using the Python API

    The Python Script Editor makes it easy to test Python scripts and to create a menu item to allow users to easily access the script.

    In addition you can directly call Python from your C# scripts. Python Scripting offers a high-level API and a low-level API.

    High-level API: PythonRunner.RunString

    The following C# code will create a menu item that prints "hello world" in the Unity console:

    using UnityEditor.Scripting.Python;
    using UnityEditor;
    
    public class HelloWorld
    {
        [MenuItem("Python/Hello World")]
        static void PrintHelloWorldFromPython()
        {
            PythonRunner.RunString(@"
                    import UnityEngine;
                    UnityEngine.Debug.Log('hello world')
                    ");
        }
    }
    

    You can use any assembly that is available in C# by simply importing it with the Python import statement -- in the example, we show importing UnityEngine.

    PythonRunner.RunFile

    Instead of inlining your Python code inside of a C# script, you can execute a whole Python script using the PythonRunner.RunFile method. For example, this Python script loops over all the GameObjects in a scene and makes sure all the names end up with an underscore:

    import UnityEngine
    
    all_objects = UnityEngine.Object.FindObjectsOfType(UnityEngine.GameObject)
    for go in all_objects:
        if go.name[-1] != '_':
            go.name = go.name + '_'
    

    Put this file in Assets/ensure_naming.py. Script files can be located anywhere on your file system, they don't need to be in the project. You can run this Python script from C# as follows:

    using UnityEditor.Scripting.Python;
    using UnityEditor;
    using UnityEngine;
    
    public class EnsureNaming
    {
        [MenuItem("Python/Ensure Naming")]
        static void RunEnsureNaming()
        {
            PythonRunner.RunFile($"{Application.dataPath}/ensure_naming.py");
        }
    }
    

    Low-level API

    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 Python for .NET documentation.

    There are some caveats:

    • Make sure Python is properly initialized by calling PythonRunner.EnsureInitialized().
    • Always grab the CPython Global Interpreter Lock (GIL).

    Sample code:

    using Python.Runtime;
    using UnityEditor.Scripting.Python;
    
    public class MyPythonScript
    {
        [MenuItem("MyPythonScript/Run")]
        public static void Run()
        {
            PythonRunner.EnsureInitialized();
            using (Py.GIL()) {
                try {
                    dynamic sys = Py.Import("sys");
                    UnityEngine.Debug.Log($"python version: {sys.version}");
                } catch(PythonException e) {
                    UnityEngine.Debug.LogException(e);
                }
            }
        }
    }
    

    You'll need to add an assembly reference in order for the compiler to know where to find the low-level API in Python.Runtime. In the project view, right-click and create a new assembly reference: Creating an assembly reference

    Then add a reference to com.unity.scripting.python.editor: Assign an assembly reference


    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • High-level API: PythonRunner.RunString
    • PythonRunner.RunFile
    • Low-level API
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)