Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Python for Unity Editor Only

Discussion in 'Formats & External Tools Previews' started by damonbla, Jun 18, 2020.

  1. damonbla

    damonbla

    Joined:
    Sep 15, 2014
    Posts:
    2
    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).
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    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.
     
  3. Fressbrett

    Fressbrett

    Joined:
    Apr 11, 2018
    Posts:
    97
    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...
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    I haven't tried, but it would be something like this. Googling for "bundling python" gives quite a few results as well.
     
    Fressbrett likes this.
  5. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    248
    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.
     
  6. markvi

    markvi

    Joined:
    Oct 31, 2016
    Posts:
    118
    Hi, is having desktop platform support sufficient for your use case? (macOS/Windows)
     
  7. goojunxi123

    goojunxi123

    Joined:
    May 25, 2021
    Posts:
    1
    So does the package support in standalone builds now?
     
    Gregne likes this.
  8. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    248
    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.
     
  9. markvi

    markvi

    Joined:
    Oct 31, 2016
    Posts:
    118
    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.<platform>. 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 .<platform> 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?
     
    Deleted User and Gregne like this.
  10. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    248
    Oh yeah, I'll definitely try that. Thanks.
     
  11. thislookshard

    thislookshard

    Joined:
    May 18, 2019
    Posts:
    1
    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.
     
  12. markvi

    markvi

    Joined:
    Oct 31, 2016
    Posts:
    118
    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
     
  13. wwaero

    wwaero

    Joined:
    Feb 18, 2020
    Posts:
    42
    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.
     
  14. jonatanAntinItsko

    jonatanAntinItsko

    Joined:
    Dec 7, 2021
    Posts:
    1
    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...
     
  15. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    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?
     
  16. Carrotpie

    Carrotpie

    Joined:
    Sep 25, 2014
    Posts:
    30
    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.
     
    skopto likes this.
  17. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
  18. talaviyahetal

    talaviyahetal

    Joined:
    Nov 26, 2018
    Posts:
    2
    if you will success, please give here solution.
     
  19. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    332
    AhmadKarim and q6gong like this.