You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot run twice; Unity crashes after running more than once #701
usingPython.Runtime;usingUnityEngine;publicclassTestLib:MonoBehaviour{// Use this for initializationprivatevoidStart(){print("initializing python");PythonEngine.Initialize();print("running python code");using(Py.GIL()){dynamicnp=Py.Import("numpy");print(np.cos(np.pi*2));}print("Destroying python engine");PythonEngine.Shutdown();}}
Here's the output:
It's able to run once but when I try to run it again, Unity editor crashes.
Here's the entire process:
I open Unity. Inside it, I click play to run this code, it works. And then I stop, and then I run the code again, Unity crashes.
Reasoning
I'm suspecting that it's because python is still running in the background when I stop the game session.
But I tried Initialize() and Shutdown() twice in the code and it works (if I run it once). So I think it's not shutting down issue. It's something else that I cannot figure out.
This error depends on the python code for sure. If I remove the python part and run, it would be able to run as many times as I want.
It might also be a threading issue which I don't understand.
I am not entirely sure the case is the same, but I am currently completing #2/2 fixes for similar crashes. The first fix will be under review soon: #714
This should be fixed in master with #958
You have to use soft shutdown though to prevent Python from being unloaded.
AFAIK, numpy does not support being unloaded.
Hi, this maybe not a pythonnet problem, but I wonder if you could give me some advice on that? I'm using "Keras.NET" in Unity3d, which uses "pythonnet_standard_py38_win" which is related with pythonnet. I'm facing the exact same problem here--It only allows me to run unity once and then it crashes. I tried to follow #958 by adding "PythonEngine.Initialize(softShutdown: true)" but it says "the best overload for initialize does not have a parameter named softShutDown". I'm very new to .NET and I would appreciate if you can give me some advice!
@akaimody123 "related to pythonnet" here means they took an old version of Python.NET and forked it. We do not support their fork. Please, contact authors of Keras.NET.
Activity
filmor commentedon Jul 5, 2018
It could be that this is the issue that is supposedly fixed by this PR: #697
offchan42 commentedon Jul 7, 2018
We could close this issue for now. I don't have time to test that new PR yet. Anyone can reopen this if you find that the bug is not yet resolved.
lassond commentedon Aug 9, 2018
I am not entirely sure the case is the same, but I am currently completing #2/2 fixes for similar crashes. The first fix will be under review soon:
#714
felipepedroso commentedon Jun 3, 2020
I'm still facing this crash 😢
OS: Windows 10
Unity: 2019.3.0f6
Python 3.7.6
I installed pythonnet using
pip installon a virtualenv inside theAssetsfolder.jackyangzzh commentedon Nov 12, 2020
I can confirm this crash exists in Unity 2019.3.3, Python 3,8.0. I am running very similar code and it also crashes the second time I run the script
lostmsu commentedon Nov 12, 2020
This should be fixed in master with #958
You have to use soft shutdown though to prevent Python from being unloaded.
AFAIK, numpy does not support being unloaded.
@benoithudson @BadSingleton can you make a wiki page explaining how to use it?
jackyangzzh commentedon Nov 18, 2020
An update: #958 fixes it. Now I can run numpy many times without any crash
benoithudson commentedon Nov 18, 2020
Sure, and perhaps we can add the bug report code to @BadSingleton's unit tests.
akaimody123 commentedon Apr 26, 2021
Hi, this maybe not a pythonnet problem, but I wonder if you could give me some advice on that? I'm using "Keras.NET" in Unity3d, which uses "pythonnet_standard_py38_win" which is related with pythonnet. I'm facing the exact same problem here--It only allows me to run unity once and then it crashes. I tried to follow #958 by adding "PythonEngine.Initialize(softShutdown: true)" but it says "the best overload for initialize does not have a parameter named softShutDown". I'm very new to .NET and I would appreciate if you can give me some advice!
lostmsu commentedon Apr 27, 2021
@akaimody123 "related to pythonnet" here means they took an old version of Python.NET and forked it. We do not support their fork. Please, contact authors of Keras.NET.
benoithudson commentedon Jul 14, 2021
Unity provides an experimental package for Python now (based on Python.NET): https://docs.unity3d.com/Packages/com.unity.scripting.python@4.0/manual/index.html
Maybe try that? It solves a lot of the difficulty of getting things initialized properly.