Python for Unity Editor Only

Hi, I’m wondering if Python for Unity will always only be editor-only, or if there are plans to make it work with standalone builds?

I’m trying to run external Python scripts in the best way and then read back their results, not really to control Unity with Python specifically. If Python for Unity will never work outside the editor I’ll need another solution, and it would be good to know now (early in my project).

You can use Process.Start to start python and pass it a script, redirect stdout/sdterr and use that.

If you do that, I strongly encourage you to bundle Python to not have to deal with version issues.

Do you have any resouces regarding bundling python into a build?

I am trying to train neural networks from within a build. Currently ML-Agents only supports editor training, due to the python dependency which is why I will either try to include the python part of ml-agents into the build or omit ML-Agents and wirte my own training scripts using Tensorflow. Though even that would require me to somehow bundle Python 3 into the build…

I haven’t tried, but it would be something like this. Googling for “bundling python” gives quite a few results as well.

1 Like

I second the need to use this package in standalone builds. I need to use a python library to get certain functionality working and this package helped immensely. Just need to be able to actually build with it because, no, I cannot use Process.Start() since I need to marschal data back to c# from my python scripts. That’s where Unity’s python bridge is essential to me.

Hi, is having desktop platform support sufficient for your use case? (macOS/Windows)

So does the package support in standalone builds now?

1 Like

I am really sorry for replying this late but I must’ve just missed the notification for your post. But yes, desktop support would be sufficient for the foreseeable future.

Hi folks,

The reason we don’t officially support standalone builds is that the underlying technology we’re using (Python for .NET) uses .NET features that il2cpp doesn’t support.

Disclaimer: I haven’t tried this myself, but I know someone on the Python for .NET mailing list was using Python in a Unity standalone project.

If you don’t mind hacking around a bit, you can copy the package contents into your project. You’ll need both com.unity.scripting.python and com.unity.scripting.python.. You’ll also have to tweak the plugin settings, move scripts from Editor to Runtime, create an appropriate .asmdef, and set your Player settings to use Mono instead of il2cpp. You’ll need to put the Python binaries (in the . package) into StreamingAssets so they get included in the build, and also modify PythonRunner.cs to find them.

What’s your use case for Python in standalone?

2 Likes

Oh yeah, I’ll definitely try that. Thanks.

Does 4.0.0-exp.5 have support to be included in builds?

I see in the description it states that “Python for Unity provides your Unity project the means to import the Python Runtime Environment as a namespace and execute Python scripts from both inside the Unity editor and built applications.” The preview package itself also contains a TestBuild.cs script.

However, whenever I try to make a build, it fails due to issues with the Python assembly references, and anything under that reference won’t be found. Is there a way for everything to be included in the build? It seems like there’s some sort of set up for builds in the package, but I can’t find anything to fix the assembly issues. I’d really need it include in the build because my application mostly executes Python code (and communicates with .cs classes) at runtime.

Sorry about that, the docs were wrong and have been updated. Using Python at runtime is not supported. TestBuild.cs just makes sure our package doesn’t break anyone’s builds. Python is not included in any runtime assemblies.

If you want to use Python in a runtime build, you’ll have to do some hacking. See my reply above: #9

Did you attempt to make it work? I’m in a similar situation where I want to run some piece of python code and pass data to and from unity and the python file.

So, if I have a python backend script, and I want it to run in parallel to the unity project (in runtime and in editor), how can I call the python script from the c# script? Like from a function like OnRuntimeMethodLoad or something similar…

Would be interesting if you could point out which features are not supported by il2cpp and if one can simply avoid using them…

Any plans to make it compatible with IL2CPP?

Oh god, I wish I’ve seen this thread a week earlier, before actually starting to work on implementing python code in Unity with this package. I guess good thing is only that it should theoretically work same in an embedded python version…
There should be an prompt that it’s editor only when installing the package, or at least in the docks page where or by the examples.

1 Like

if somebody needs scripting that works on il2cpp i suggest check out this project GitHub - pjc0247/SlowSharp: C# interpreter written in C#

if you will success, please give here solution.

I published a python plugin project. The plugins use python and it works in playmode, maybe it can be useful as example to someone

2 Likes