← 输入框|github.com|刷新
Skip to content

Cannot run twice; Unity crashes after running more than once #701

Closed

Description

@offchan42

Environment

  • Pythonnet version: 2.3.0 py35
  • Python version: 3.5.4
  • Operating System: Windows 10

Details

I want to run this code on my Unity project:

using Python.Runtime;
using UnityEngine;

public class TestLib : MonoBehaviour
{
    // Use this for initialization
    private void Start()
    {
        print("initializing python");
        PythonEngine.Initialize();
        print("running python code");
        using (Py.GIL())
        {
            dynamic np = Py.Import("numpy");
            print(np.cos(np.pi * 2));
        }

        print("Destroying python engine");
        PythonEngine.Shutdown();
    }
}

Here's the output:
2018-07-06_05-02-13

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.

What are the possible causes and fixes?

Activity

  1. filmor commented on Jul 5, 2018

    @filmor
    Member

    It could be that this is the issue that is supposedly fixed by this PR: #697

  2. offchan42 commented on Jul 7, 2018

    @offchan42
    Author

    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.

  3. lassond commented on Aug 9, 2018

    @lassond

    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

  4. felipepedroso commented on Jun 3, 2020

    @felipepedroso

    I'm still facing this crash 😢

    OS: Windows 10
    Unity: 2019.3.0f6
    Python 3.7.6

    I installed pythonnet using pip install on a virtualenv inside the Assets folder.

  5. jackyangzzh commented on Nov 12, 2020

    @jackyangzzh

    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

  6. lostmsu commented on Nov 12, 2020

    @lostmsu
    Member

    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?

  7. jackyangzzh commented on Nov 18, 2020

    @jackyangzzh

    An update: #958 fixes it. Now I can run numpy many times without any crash

  8. benoithudson commented on Nov 18, 2020

    @benoithudson
    Contributor

    Sure, and perhaps we can add the bug report code to @BadSingleton's unit tests.

  9. akaimody123 commented on Apr 26, 2021

    @akaimody123

    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!

  10. lostmsu commented on Apr 27, 2021

    @lostmsu
    Member

    @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.

  11. benoithudson commented on Jul 14, 2021

    @benoithudson
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions