IronPython - SimplyJacoby/Unipy GitHub Wiki
IronPython is an implementation of Python based on Python.Net.
- Download IronPython.3.4.0-alpha1.zip
- Extract the folder to your Unity project in Assets/Plugins. You should now have Assets/Plugins/IronPython.3.4.0-alpha1 in your Unity Project.
- Download Microsoft.Scripting.Metadata.dll and Microsoft.Scripting.Metadata.xml
- Move these two files into Assets/Plugins/IronPython.3.4.0-alpha1/net46.
- In Assets/Plugins/IronPython.3.4.0-alpha1, delete these folders:
- net5.0
- net5.0-windows
- netcoreapp2.1
- netcoreapp3.1
- netstandard2.0
- When everything is compiled you might see an error with IronPython.Wpf.dll.
- In the Assets/Plugins/IronPython.3.4.0-alpha1/net46/DLLs folder, delete IronPython.Wpf.dll. It seems to be unnecessary and fixes the issue.
- You also 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.
- Since IronPython is based on .NET and not C like CPython, you cannot use libraries which are C based. Numpy is the big one that comes to mind.
- In command prompt, cd into the /IronPython.3.4.0-alpha1/net46 directory.
- Execute
ipy -X:Frames -m ensurepip
, which makes sure you have pip installed. - Now you can add any library you want by executing
ipy -X:Frames -m pip install library-name
. - ** This seems to correctly install the libraries, however I'm still getting errors when importing libraries into Python scripts. The errors seem unrelated though, they have to do with the Microsoft.Scripting.Interpreter.
- Using the
IronPython.Hosting
namespace and theMicrosoft.Scripting.Hosting
namespace - Create the engine
ScriptEngine engine = Python.CreateEngine();
- Add the location of any libraries you want to use to the engine's search paths.
-
ICollection<string> searchPaths = engine.GetSearchPaths();
searchPaths.Add(@"C:/Location_of_Unity_project/Assets/Plugins/IronPython.3.4.0-alpha1/Lib");
searchPaths.Add(@"C:/Location_of_Unity_project/Assets/Plugins/IronPython.3.4.0-alpha1/Lib/site-packages");
engine.SetSearchPaths(searchPaths);
-
- Use the engine to run a Python script
dynamic py = engine.ExecuteFile("C:/path/to/file.py");
- Use py to access variables, methods, classes, etc in the Python script using dot notation
- See script.py and CallingPython.cs for examples of how to do this.
- If you simply want to run the script and don't care about accessing any data, you can skip assigning the variable
engine.ExecuteFile("path/to/file.py");
- External Python libraries don't work well.