Skip to content

Python.NET

Jacob edited this page May 27, 2021 · 30 revisions

Python.NET

Installation

Installing NuGet Package Manager

  • Download the latest NuGetForUnity .unitypackage here
  • In Unity, go to Assets > Import Package > Custom Package, locate where you downloaded NuGetForUnity, and install it.
  • You should now see NuGet in the Unity menu bar.

Installing Python.NET

  • In Unity, go to NuGet > Manage NuGet Packages, select Show All Versions and Show Prerelease.
  • Search for pythonnet.
  • Install pythonnet Version 3.0.0-preview2021-05-03.

Installing Embedded Python

  • Download a version of embedded python. I used latest release for winx64
  • In Unity, create a new folder under Assets called StreamingAssets.
  • Extract the .zip of your downloaded Python into the StreamingAssets folder.
  • To enable pip, download get-pip.py and save it in the Python folder (Assets/StreamingAssets/python-3.9.5-embed-amd64)
  • Run get-pip.py in command line. First cd into Assets/StreamingAssets/python-3.9.5-embed-amd64, then execute
python.exe get-pip.py
  • Now you can install any module you want with
python.exe -m pip install module-name

Final Steps

  • You should go to Edit > Project Settings > Player > Other Settings > Configuration and change the Api Compatibility Level to .Net 4.x to be safe. Apparently there can be some issues using .Net Standard 2.0.

Using Python.NET in Unity

Python.NET in a C# Script

  • In a C# script you will want to include in the Start() method
void Start()
    {
        Runtime.PythonDLL = Application.dataPath + "/StreamingAssets/python-3.9.5-embed-amd64/python39.dll";
        PythonEngine.Initialize(mode: ShutdownMode.Reload);
    }

Clone this wiki locally