Unable to load coreclr from Pythonnet in blender 4.0 #115778

Open
opened 2023-12-05 03:53:27 +01:00 by Logan-G · 12 comments

System Information
Operating system: Linux-6.2.0-37-generic-x86_64-with-glibc2.35 64 Bits, X11 UI
Graphics card: NVIDIA GeForce RTX 3080 Ti/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 535.129.03

Blender Version
Broken: version: 4.0.1, branch: blender-v4.0-release, commit date: 2023-11-16 16:40, hash: d0dd92834a08
Worked: 3.0.1

Short description of error
I'm trying to load coreclr via pythonnet in blender 4.0 but keep receiving the follow stack trace in my blender console:

Modules Installed (pythonnet_install_bug_demo) from '~/Blender/Pythonnet-bug-addon/pythonnet_install_bug_demo.py' into '~/.config/blender/4.0/scripts/addons'
module changed on disk: '~/.config/blender/4.0/scripts/addons/pythonnet_install_bug_demo.py' reloading...
Attempting to load coreclr
Failed to initialize pythonnet: System.TypeInitializationException: The type initializer for 'Delegates' threw an exception.
 ---> Python.Runtime.BadPythonDllException: Runtime.PythonDLL was not set or does not point to a supported Python runtime DLL. See https://github.com/pythonnet/pythonnet#embedding-python-in-net
 ---> System.MissingMethodException: Failed to load symbol Py_Main: ~/Downloads/blender-4.0.1-linux-x64/blender: undefined symbol: Py_Main
   at Python.Runtime.Platform.PosixLoader.GetFunction(IntPtr dllHandle, String name) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Native/LibraryLoader.cs:line 85
   at Python.Runtime.Runtime.Delegates.GetFunctionByName(String functionName, IntPtr libraryHandle) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 296
   --- End of inner exception stack trace ---
   at Python.Runtime.Runtime.Delegates.GetFunctionByName(String functionName, IntPtr libraryHandle) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 300
   at Python.Runtime.Runtime.Delegates..cctor() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 38
   --- End of inner exception stack trace ---
   at Python.Runtime.Runtime.Delegates.get_PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 319
   at Python.Runtime.Runtime.PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.cs:line 705
   at Python.Runtime.PythonEngine.AcquireLock() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/PythonEngine.cs:line 479
   at Python.Runtime.Py.GILState..ctor() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 27
   at Python.Runtime.Py.GIL() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 13
   at Python.Runtime.Loader.Initialize(IntPtr data, Int32 size) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Loader.cs:line 26
   at Python.Runtime.Runtime.Delegates.get_PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 319
   at Python.Runtime.Runtime.PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.cs:line 705
   at Python.Runtime.PythonEngine.AcquireLock() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/PythonEngine.cs:line 479
   at Python.Runtime.Py.GILState..ctor() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 27
   at Python.Runtime.Py.GIL() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 13
   at Python.Runtime.Loader.Initialize(IntPtr data, Int32 size) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Loader.cs:line 26Traceback (most recent call last):
  File "~/.config/blender/4.0/scripts/addons/pythonnet_install_bug_demo.py", line 26, in execute
    pythonnet.load("coreclr")
  File "~/.local/lib/python3.10/site-packages/pythonnet/__init__.py", line 146, in load
    raise RuntimeError("Failed to initialize Python.Runtime.dll")
RuntimeError: Failed to initialize Python.Runtime.dll
Error: Python: Traceback (most recent call last):
  File "~/.config/blender/4.0/scripts/addons/pythonnet_install_bug_demo.py", line 26, in execute
    pythonnet.load("coreclr")
  File "~/.local/lib/python3.10/site-packages/pythonnet/__init__.py", line 146, in load
    raise RuntimeError("Failed to initialize Python.Runtime.dll")
RuntimeError: Failed to initialize Python.Runtime.dll

The reason I believe this problem is blender-specific is because I'm able to load coreclr in blender 3.0.1, but it is now broken in 4.0.1. I isolated the issue to be about loading the coreclr via pythonnet into blender 4.0 and captured it in a plugin I've attached. I witnessed the issue when I used a larger plugin that also used pythonnet and loaded coreclr, but I don't believe the full plugin is needed to replicate the error. I have tried a couple different ideas to further narrow the problem and see if anything else might cause the it:

  • coreclr loaded correctly in blender 3.0.1.
  • I tried with different installations of python on my system such as locally via snap or a global installation.
  • Pythonnet itself isn't an issue because I used the same package install to test loading coreclr into blender. The version of pythonnet I'm using 3.0.3.
  • This issue also exists in blender 3.6.

Exact steps for others to reproduce the error

  1. Install the attached script as a plugin to blender 4.0.1.
  2. Make sure pythonnet 3.0.3 is installed.
  3. Enable "Developer Extras" in Preferences -> Interface.
  4. Press F3 and search for load pythonnet.
  5. Witness the above stack trace popup in the blender console or a popup that says "Loaded Pythonnet successfully!"
**System Information** Operating system: Linux-6.2.0-37-generic-x86_64-with-glibc2.35 64 Bits, X11 UI Graphics card: NVIDIA GeForce RTX 3080 Ti/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 535.129.03 **Blender Version** Broken: version: 4.0.1, branch: blender-v4.0-release, commit date: 2023-11-16 16:40, hash: `d0dd92834a08` Worked: 3.0.1 **Short description of error** I'm trying to load coreclr via pythonnet in blender 4.0 but keep receiving the follow stack trace in my blender console: ``` Modules Installed (pythonnet_install_bug_demo) from '~/Blender/Pythonnet-bug-addon/pythonnet_install_bug_demo.py' into '~/.config/blender/4.0/scripts/addons' module changed on disk: '~/.config/blender/4.0/scripts/addons/pythonnet_install_bug_demo.py' reloading... Attempting to load coreclr Failed to initialize pythonnet: System.TypeInitializationException: The type initializer for 'Delegates' threw an exception. ---> Python.Runtime.BadPythonDllException: Runtime.PythonDLL was not set or does not point to a supported Python runtime DLL. See https://github.com/pythonnet/pythonnet#embedding-python-in-net ---> System.MissingMethodException: Failed to load symbol Py_Main: ~/Downloads/blender-4.0.1-linux-x64/blender: undefined symbol: Py_Main at Python.Runtime.Platform.PosixLoader.GetFunction(IntPtr dllHandle, String name) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Native/LibraryLoader.cs:line 85 at Python.Runtime.Runtime.Delegates.GetFunctionByName(String functionName, IntPtr libraryHandle) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 296 --- End of inner exception stack trace --- at Python.Runtime.Runtime.Delegates.GetFunctionByName(String functionName, IntPtr libraryHandle) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 300 at Python.Runtime.Runtime.Delegates..cctor() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 38 --- End of inner exception stack trace --- at Python.Runtime.Runtime.Delegates.get_PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 319 at Python.Runtime.Runtime.PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.cs:line 705 at Python.Runtime.PythonEngine.AcquireLock() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/PythonEngine.cs:line 479 at Python.Runtime.Py.GILState..ctor() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 27 at Python.Runtime.Py.GIL() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 13 at Python.Runtime.Loader.Initialize(IntPtr data, Int32 size) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Loader.cs:line 26 at Python.Runtime.Runtime.Delegates.get_PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 319 at Python.Runtime.Runtime.PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.cs:line 705 at Python.Runtime.PythonEngine.AcquireLock() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/PythonEngine.cs:line 479 at Python.Runtime.Py.GILState..ctor() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 27 at Python.Runtime.Py.GIL() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 13 at Python.Runtime.Loader.Initialize(IntPtr data, Int32 size) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Loader.cs:line 26Traceback (most recent call last): File "~/.config/blender/4.0/scripts/addons/pythonnet_install_bug_demo.py", line 26, in execute pythonnet.load("coreclr") File "~/.local/lib/python3.10/site-packages/pythonnet/__init__.py", line 146, in load raise RuntimeError("Failed to initialize Python.Runtime.dll") RuntimeError: Failed to initialize Python.Runtime.dll Error: Python: Traceback (most recent call last): File "~/.config/blender/4.0/scripts/addons/pythonnet_install_bug_demo.py", line 26, in execute pythonnet.load("coreclr") File "~/.local/lib/python3.10/site-packages/pythonnet/__init__.py", line 146, in load raise RuntimeError("Failed to initialize Python.Runtime.dll") RuntimeError: Failed to initialize Python.Runtime.dll ``` The reason I believe this problem is blender-specific is because I'm able to load coreclr in blender 3.0.1, but it is now broken in 4.0.1. I isolated the issue to be about loading the coreclr via pythonnet into blender 4.0 and captured it in a plugin I've attached. I witnessed the issue when I used a larger plugin that also used pythonnet and loaded coreclr, but I don't believe the full plugin is needed to replicate the error. I have tried a couple different ideas to further narrow the problem and see if anything else might cause the it: - coreclr loaded correctly in blender 3.0.1. - I tried with different installations of python on my system such as locally via snap or a global installation. - Pythonnet itself isn't an issue because I used the same package install to test loading coreclr into blender. The version of pythonnet I'm using 3.0.3. - This issue also exists in blender 3.6. **Exact steps for others to reproduce the error** 1. Install the attached script as a plugin to blender 4.0.1. 2. Make sure pythonnet 3.0.3 is installed. 3. Enable "Developer Extras" in `Preferences -> Interface`. 4. Press F3 and search for `load pythonnet`. 5. Witness the above stack trace popup in the blender console or a popup that says "Loaded Pythonnet successfully!"
Logan-G added the
Priority
Normal
Type
Report
Status
Needs Triage
labels 2023-12-05 03:53:28 +01:00

It seems that the Pythonnet version 3.0.3 is not compatible with the Python version that comes with Blender 3.1+:

ERROR: Could not find a version that satisfies the requirement 3.0.3 (from versions: none)
ERROR: No matching distribution found for 3.0.3

To address this, you may want to try using a different version of Pythonnet that is compatible with Blender or consider using a different approach to achieve your desired functionality.

If you need further assistance, please feel free to provide additional details or seek help on the Blender community sites.

It seems that the Pythonnet version 3.0.3 is not compatible with the Python version that comes with Blender 3.1+: ``` ERROR: Could not find a version that satisfies the requirement 3.0.3 (from versions: none) ERROR: No matching distribution found for 3.0.3 ``` To address this, you may want to try using a different version of Pythonnet that is compatible with Blender or consider using a different approach to achieve your desired functionality. If you need further assistance, please feel free to provide additional details or seek help on the Blender community sites.
Germano Cavalcante added
Status
Needs Information from User
and removed
Status
Needs Triage
labels 2023-12-06 02:32:16 +01:00
Author

Can I ask how you produced those errors? If that's a pip error, pythonnet's current version is 3.0.3, so you should be able to just download it via something like pip install pythonnet.

Can I ask how you produced those errors? If that's a pip error, pythonnet's current version is 3.0.3, so you should be able to just download it via something like `pip install pythonnet`.
Germano Cavalcante added
Status
Needs Triage
and removed
Status
Needs Information from User
labels 2023-12-06 15:27:23 +01:00

I made a mistake in the previous installation. But I tried again and I can't reproduce the problem.

Looking at the error log, this problem should simply be replicable with the following commands:

D:\BlenderDev\x64-Release\bin\RelWithDebInfo\4.1\python\bin\python.exe -m ensurepip --upgrade
D:\BlenderDev\x64-Release\bin\RelWithDebInfo\4.1\python\bin\python.exe -m pip install pythonnet
D:\BlenderDev\x64-Release\bin\RelWithDebInfo\4.1\python\bin\python.exe -c "import pythonnet | pythonnet.load("coreclr")"

Note that the python.exe used is the one that comes within Blender, and the modules must be installed in that module's library within Blender.

But in your error log, the module directory is: "~/.local/lib/python3.10/site-packages/pythonnet/__init__.py".
This shows that you are using the module from another location, probably incompatible.

Could you ensure that you are installing and using the pythonnet module strictly within the Blender's Python environment?
Please rerun the installation and test with the noted commands, ensuring that you are referencing the correct python.exe that is bundled with Blender.

I made a mistake in the previous installation. But I tried again and I can't reproduce the problem. Looking at the error log, this problem should simply be replicable with the following commands: ``` D:\BlenderDev\x64-Release\bin\RelWithDebInfo\4.1\python\bin\python.exe -m ensurepip --upgrade D:\BlenderDev\x64-Release\bin\RelWithDebInfo\4.1\python\bin\python.exe -m pip install pythonnet D:\BlenderDev\x64-Release\bin\RelWithDebInfo\4.1\python\bin\python.exe -c "import pythonnet | pythonnet.load("coreclr")" ``` Note that the `python.exe` used is the one that comes within Blender, and the modules must be installed in that module's library within Blender. But in your error log, the module directory is: `"~/.local/lib/python3.10/site-packages/pythonnet/__init__.py"`. This shows that you are using the module from another location, probably incompatible. Could you ensure that you are installing and using the `pythonnet` module strictly within the Blender's Python environment? Please rerun the installation and test with the noted commands, ensuring that you are referencing the correct `python.exe` that is bundled with Blender.
Germano Cavalcante added
Status
Needs Information from User
and removed
Status
Needs Triage
labels 2023-12-06 17:18:24 +01:00
Author

Did you try reproducing the problem in blender? I'm able to perform the commands you suggested using blender's python binary directly and am able to replicate your result that there aren't any issues.

However, when I try to load coreclr inside of blender say in the python console, it fails with the original error. I verified that the pythonnet install I'm using is local to the python blender binary and is using the right python executable.

>>> import sys
>>> sys.executable
'~/Downloads/blender-4.0.1-linux-x64/4.0/python/bin/python3.10'

>>> import pythonnet
>>> import inspect
>>> inspect.getsourcefile(pythonnet)
'~/Downloads/blender-4.0.1-linux-x64/4.0/python/lib/python3.10/site-packages/pythonnet/__init__.py'

I also purged any other instances of pythonnet from my computer and reinstalled pythonnet to the local folder after ensuring it failed to import pythonnet.

Here's the error posted again for clarity in case something I didn't notice changed:

Failed to initialize pythonnet: System.TypeInitializationException: The type initializer for 'Delegates' threw an exception.
 ---> Python.Runtime.BadPythonDllException: Runtime.PythonDLL was not set or does not point to a supported Python runtime DLL. See https://github.com/pythonnet/pythonnet#embedding-python-in-net
 ---> System.MissingMethodException: Failed to load symbol Py_Main: ./blender: undefined symbol: Py_Main
   at Python.Runtime.Platform.PosixLoader.GetFunction(IntPtr dllHandle, String name) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Native/LibraryLoader.cs:line 85
   at Python.Runtime.Runtime.Delegates.GetFunctionByName(String functionName, IntPtr libraryHandle) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 296
   --- End of inner exception stack trace ---
   at Python.Runtime.Runtime.Delegates.GetFunctionByName(String functionName, IntPtr libraryHandle) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 300
   at Python.Runtime.Runtime.Delegates..cctor() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 38
   --- End of inner exception stack trace ---
   at Python.Runtime.Runtime.Delegates.get_PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 319
   at Python.Runtime.Runtime.PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.cs:line 705
   at Python.Runtime.PythonEngine.AcquireLock() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/PythonEngine.cs:line 479
   at Python.Runtime.Py.GILState..ctor() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 27
   at Python.Runtime.Py.GIL() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 13
   at Python.Runtime.Loader.Initialize(IntPtr data, Int32 size) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Loader.cs:line 26
   at Python.Runtime.Runtime.Delegates.get_PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 319
   at Python.Runtime.Runtime.PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.cs:line 705
   at Python.Runtime.PythonEngine.AcquireLock() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/PythonEngine.cs:line 479
   at Python.Runtime.Py.GILState..ctor() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 27
   at Python.Runtime.Py.GIL() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 13
   at Python.Runtime.Loader.Initialize(IntPtr data, Int32 size) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Loader.cs:line 26Traceback (most recent call last):
  File "~/Downloads/blender-4.0.1-linux-x64/4.0/scripts/addons/pythonnet_install_bug_demo.py", line 26, in execute
    pythonnet.load("coreclr")
  File "~/Downloads/blender-4.0.1-linux-x64/4.0/python/lib/python3.10/site-packages/pythonnet/__init__.py", line 146, in load
    raise RuntimeError("Failed to initialize Python.Runtime.dll")
RuntimeError: Failed to initialize Python.Runtime.dll
Error: Python: Traceback (most recent call last):
  File "~/Downloads/blender-4.0.1-linux-x64/4.0/scripts/addons/pythonnet_install_bug_demo.py", line 26, in execute
    pythonnet.load("coreclr")
  File "~/Downloads/blender-4.0.1-linux-x64/4.0/python/lib/python3.10/site-packages/pythonnet/__init__.py", line 146, in load
    raise RuntimeError("Failed to initialize Python.Runtime.dll")
RuntimeError: Failed to initialize Python.Runtime.dll
Did you try reproducing the problem in blender? I'm able to perform the commands you suggested using blender's python binary directly and am able to replicate your result that there aren't any issues. However, when I try to load coreclr inside of blender say in the python console, it fails with the original error. I verified that the pythonnet install I'm using is local to the python blender binary and is using the right python executable. ``` >>> import sys >>> sys.executable '~/Downloads/blender-4.0.1-linux-x64/4.0/python/bin/python3.10' >>> import pythonnet >>> import inspect >>> inspect.getsourcefile(pythonnet) '~/Downloads/blender-4.0.1-linux-x64/4.0/python/lib/python3.10/site-packages/pythonnet/__init__.py' ``` I also purged any other instances of pythonnet from my computer and reinstalled pythonnet to the local folder after ensuring it failed to import pythonnet. Here's the error posted again for clarity in case something I didn't notice changed: ``` Failed to initialize pythonnet: System.TypeInitializationException: The type initializer for 'Delegates' threw an exception. ---> Python.Runtime.BadPythonDllException: Runtime.PythonDLL was not set or does not point to a supported Python runtime DLL. See https://github.com/pythonnet/pythonnet#embedding-python-in-net ---> System.MissingMethodException: Failed to load symbol Py_Main: ./blender: undefined symbol: Py_Main at Python.Runtime.Platform.PosixLoader.GetFunction(IntPtr dllHandle, String name) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Native/LibraryLoader.cs:line 85 at Python.Runtime.Runtime.Delegates.GetFunctionByName(String functionName, IntPtr libraryHandle) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 296 --- End of inner exception stack trace --- at Python.Runtime.Runtime.Delegates.GetFunctionByName(String functionName, IntPtr libraryHandle) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 300 at Python.Runtime.Runtime.Delegates..cctor() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 38 --- End of inner exception stack trace --- at Python.Runtime.Runtime.Delegates.get_PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 319 at Python.Runtime.Runtime.PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.cs:line 705 at Python.Runtime.PythonEngine.AcquireLock() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/PythonEngine.cs:line 479 at Python.Runtime.Py.GILState..ctor() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 27 at Python.Runtime.Py.GIL() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 13 at Python.Runtime.Loader.Initialize(IntPtr data, Int32 size) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Loader.cs:line 26 at Python.Runtime.Runtime.Delegates.get_PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.Delegates.cs:line 319 at Python.Runtime.Runtime.PyGILState_Ensure() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Runtime.cs:line 705 at Python.Runtime.PythonEngine.AcquireLock() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/PythonEngine.cs:line 479 at Python.Runtime.Py.GILState..ctor() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 27 at Python.Runtime.Py.GIL() in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Py.cs:line 13 at Python.Runtime.Loader.Initialize(IntPtr data, Int32 size) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Loader.cs:line 26Traceback (most recent call last): File "~/Downloads/blender-4.0.1-linux-x64/4.0/scripts/addons/pythonnet_install_bug_demo.py", line 26, in execute pythonnet.load("coreclr") File "~/Downloads/blender-4.0.1-linux-x64/4.0/python/lib/python3.10/site-packages/pythonnet/__init__.py", line 146, in load raise RuntimeError("Failed to initialize Python.Runtime.dll") RuntimeError: Failed to initialize Python.Runtime.dll Error: Python: Traceback (most recent call last): File "~/Downloads/blender-4.0.1-linux-x64/4.0/scripts/addons/pythonnet_install_bug_demo.py", line 26, in execute pythonnet.load("coreclr") File "~/Downloads/blender-4.0.1-linux-x64/4.0/python/lib/python3.10/site-packages/pythonnet/__init__.py", line 146, in load raise RuntimeError("Failed to initialize Python.Runtime.dll") RuntimeError: Failed to initialize Python.Runtime.dll ```

Did you try reproducing the problem in blender?

Yes, and the addon worked normally in Blender.
I think the problem is not related to Blender.

Check environment variables such as PYTHONPATH, they can cause Python to use some modules in the system which can result in incompatibility.

I noticed that you use Linux. Perhaps this could influence the result. I'll let someone else on the triage team take a look.

> Did you try reproducing the problem in blender? Yes, and the addon worked normally in Blender. I think the problem is not related to Blender. Check environment variables such as PYTHONPATH, they can cause Python to use some modules in the system which can result in incompatibility. I noticed that you use Linux. Perhaps this could influence the result. I'll let someone else on the triage team take a look.
Germano Cavalcante added
Status
Needs Triage
and removed
Status
Needs Information from User
labels 2023-12-06 22:31:21 +01:00
Author

I think the problem is Blender Linux-related because I know this works fine on Blender on Windows. I should have probably mentioned that upfront, but I thought the System Info in the initial post would be enough.

I checked PYTHONPATH, but it's empty.

I think the problem is Blender Linux-related because I know this works fine on Blender on Windows. I should have probably mentioned that upfront, but I thought the System Info in the initial post would be enough. I checked PYTHONPATH, but it's empty.
Author

So I just tried importing pythonnet into blender via my laptop with the same Ubuntu distro and version as my desktop (Ubuntu 22.04 LTS), and I again received the same error. Pythonnet is again installed locally within the python binary supplied by blender. The only difference between the two setups is that my laptop has blender 4.0.2 and my desktop has blender 4.0.1. Any help would be appreciated on how to move forward. I'm completely blocked by this issue.

To reiterate the steps to reproduce the issue are as follows:

  1. Download the latest blender linux installation.
  2. Run <PATH_TO_BLENDER>/4.0/python/bin/python3.10 -m pip install pythonnet
  3. Run the following code snippet in the python binary outside of blender and verify no output is produced (runs successfully).
import pythonnet
pythonnet.load("coreclr")
  1. Open blender and go to scripting and repeat the same code snippet in the python console.
  2. Witness the error provided above as:
Error: Python: Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "<PATH_TO_BLENDER>/4.0/python/lib/python3.10/site-packages/pythonnet/__init__.py", line 146, in load
    raise RuntimeError("Failed to initialize Python.Runtime.dll")
RuntimeError: Failed to initialize Python.Runtime.dll
So I just tried importing pythonnet into blender via my laptop with the same Ubuntu distro and version as my desktop (Ubuntu 22.04 LTS), and I again received the same error. Pythonnet is again installed locally within the python binary supplied by blender. The only difference between the two setups is that my laptop has blender 4.0.2 and my desktop has blender 4.0.1. Any help would be appreciated on how to move forward. I'm completely blocked by this issue. To reiterate the steps to reproduce the issue are as follows: 1. Download the latest blender linux installation. 2. Run `<PATH_TO_BLENDER>/4.0/python/bin/python3.10 -m pip install pythonnet` 3. Run the following code snippet in the python binary outside of blender and verify no output is produced (runs successfully). ``` import pythonnet pythonnet.load("coreclr") ``` 4. Open blender and go to scripting and repeat the same code snippet in the python console. 5. Witness the error provided above as: ``` Error: Python: Traceback (most recent call last): File "<blender_console>", line 1, in <module> File "<PATH_TO_BLENDER>/4.0/python/lib/python3.10/site-packages/pythonnet/__init__.py", line 146, in load raise RuntimeError("Failed to initialize Python.Runtime.dll") RuntimeError: Failed to initialize Python.Runtime.dll ```
Member

this error is the important one

---> System.MissingMethodException: Failed to load symbol Py_Main: ./blender: undefined symbol: Py_Main

validating manually:

[root@SRV blender-4.0.2-linux-x64]# nm -CD ./blender | grep Py_Main
[root@SRV blender-4.0.2-linux-x64]# nm -CD ./4.0/python/bin/python3.10 | grep Py_Main
000000000041d647 T Py_Main

I don't have a working linux build currently, but i'd probably try linking the python libs with the --whole-archive flag

this error is the important one > ---> System.MissingMethodException: Failed to load symbol Py_Main: ./blender: undefined symbol: Py_Main validating manually: ``` [root@SRV blender-4.0.2-linux-x64]# nm -CD ./blender | grep Py_Main [root@SRV blender-4.0.2-linux-x64]# nm -CD ./4.0/python/bin/python3.10 | grep Py_Main 000000000041d647 T Py_Main ``` I don't have a working linux build currently, but i'd probably try linking the python libs with the `--whole-archive` flag
Author

I downloaded the blender source code and followed the build instructions at https://wiki.blender.org/wiki/Building_Blender/Linux/Ubuntu and am wondering where you suggest I add the --whole-archive flag? I can't find explicitly where the python library is linked during make, but I did find what make uses to build the python library.

I downloaded the blender source code and followed the build instructions at https://wiki.blender.org/wiki/Building_Blender/Linux/Ubuntu and am wondering where you suggest I add the `--whole-archive` flag? I can't find explicitly where the python library is linked during `make`, but I did find what `make` uses to build the python library.
Member

I just hacked into my build.ninja file, but can confirm linking the python libs that way fixes the issue, i'd leave the actual fix up to the linux platform maintainer.

we currently have PYTHON_LINKFLAGS where we could easily add this flag, however this is only half the battle, after the python libs are linked, order has to be restored and --no-whole-archive has to be passed or all libs that follow will be linked that way.

I just hacked into my `build.ninja` file, but can confirm linking the python libs that way fixes the issue, i'd leave the actual fix up to the linux platform maintainer. we currently have `PYTHON_LINKFLAGS` where we could easily add this flag, however this is only half the battle, after the python libs are linked, order has to be restored and `--no-whole-archive` has to be passed or all libs that follow will be linked that way.
Author

Thank you for following up. I figured out a way to rebuild using --whole-archive on libpython3,11.a when building bf_python_ext and that successfully fixes the issue on my end.

In the meantime, for anyone who runs into this issue as well, I had to modify macros.cmake and add this code snippet to blender_link_libraries:380

if("${library}" MATCHES ".*libpython3.1[0-9].a" AND "${target}" STREQUAL "bf_python_ext")
  set(library "-Wl,--whole-archive ${library} -Wl,--no-whole-archive")
endif()

since like you said we need to wrap the library within --whole-archive and --no-whole-archive. After adding this code, I rebuilt from scratch and reinstalled pythonnet to the provided python binary and now it works. This fix is a gross hack that's on a critical build path though, so I agree that we should wait for a proper fix as indicated by Ray.

Thank you for following up. I figured out a way to rebuild using `--whole-archive` on `libpython3,11.a` when building `bf_python_ext` and that successfully fixes the issue on my end. In the meantime, for anyone who runs into this issue as well, I had to modify `macros.cmake` and add this code snippet to `blender_link_libraries:380` ``` if("${library}" MATCHES ".*libpython3.1[0-9].a" AND "${target}" STREQUAL "bf_python_ext") set(library "-Wl,--whole-archive ${library} -Wl,--no-whole-archive") endif() ``` since like you said we need to wrap the library within `--whole-archive` and `--no-whole-archive`. After adding this code, I rebuilt from scratch and reinstalled pythonnet to the provided python binary and now it works. This fix is a gross hack that's on a critical build path though, so I agree that we should wait for a proper fix as indicated by Ray.

Tagging @ideasman42 (who might have more insight into this issue).

Tagging @ideasman42 (who might have more insight into this issue).
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#115778
No description provided.