Pythonnet: Crash on initialization in Unity

Created on 20 Aug 2019  路  2Comments  路  Source: pythonnet/pythonnet

Environment

  • Pythonnet version: 2.4.0
  • Python version: 3.7.4m
  • Operating System: Ubuntu 16.04
  • Unity: 2018.4.4f1

Details

I'm trying to embed PythonNET in to a Unity project but for some reason it crashes on Initialization.

Minimal example to get this to work:

锘縰sing System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Python.Runtime;

public class PythonTest : MonoBehaviour
{
    void Start()
    {
        using (Py.GIL())
        {
            Debug.Log("This didn't crash");
        }
    }

    void Update()    { }
}

Attach this script to any object in the scene and it will crash on running it.

Traceback from editor log:

Receiving unhandled NULL exception
Launching bug reporter
#0  0x00000042267897 in (wrapper managed-to-native) Python.Runtime.Runtime:PyObject_GetAttrString (intptr,string)
/home/builduser/buildslave/unity/build/Editor/Platform/Linux/TempStubs.cpp:PlatformImageForIconForExtension
/home/builduser/buildslave/unity/build/Editor/Platform/Linux/UsbDevices.cpp:UsbDevicesQuery
[Unity Package Manager (Upm)]
Parent process [6903] was terminated

Previously this worked just fine but now, despite no changes, just doesn't and I don't know how to debug this any further.

Most helpful comment

Nope, still crashes in precisely the same way - presuming of course mean a call to Python.Runtime.PythonEngine.Initialize()? Python.Runtime.Initialize is an internal method, it isn't exposed so I can't call it directly:
https://github.com/pythonnet/pythonnet/blob/f1da55e5d13f7c82d2eb62e211afd93d574d5fe8/src/runtime/runtime.cs#L171

Besides, that shouldn't be necessary. calling GIL() calls Runtime.Initialize at the start anyway:
https://github.com/pythonnet/pythonnet/blob/f1da55e5d13f7c82d2eb62e211afd93d574d5fe8/src/runtime/pythonengine.cs#L612-L620

https://github.com/pythonnet/pythonnet/blob/f1da55e5d13f7c82d2eb62e211afd93d574d5fe8/src/runtime/pythonengine.cs#L138-L146

https://github.com/pythonnet/pythonnet/blob/f1da55e5d13f7c82d2eb62e211afd93d574d5fe8/src/runtime/pythonengine.cs#L158-L169

I understand if you don't want to help, open source after all, but can you please not prematurely close this ticket?

All 2 comments

It doesn't look like you actually initialised Python.Net, which is required to execute anything. Try adding a call to Python.Runtime.Initialize() before trying to capture the GIL.

Nope, still crashes in precisely the same way - presuming of course mean a call to Python.Runtime.PythonEngine.Initialize()? Python.Runtime.Initialize is an internal method, it isn't exposed so I can't call it directly:
https://github.com/pythonnet/pythonnet/blob/f1da55e5d13f7c82d2eb62e211afd93d574d5fe8/src/runtime/runtime.cs#L171

Besides, that shouldn't be necessary. calling GIL() calls Runtime.Initialize at the start anyway:
https://github.com/pythonnet/pythonnet/blob/f1da55e5d13f7c82d2eb62e211afd93d574d5fe8/src/runtime/pythonengine.cs#L612-L620

https://github.com/pythonnet/pythonnet/blob/f1da55e5d13f7c82d2eb62e211afd93d574d5fe8/src/runtime/pythonengine.cs#L138-L146

https://github.com/pythonnet/pythonnet/blob/f1da55e5d13f7c82d2eb62e211afd93d574d5fe8/src/runtime/pythonengine.cs#L158-L169

I understand if you don't want to help, open source after all, but can you please not prematurely close this ticket?

Was this page helpful?
0 / 5 - 0 ratings