I'm not sure what is going on, msbuild and mono work just fine, the referenced libmono-native library exists in the location specified (not sure why it drops back a directory, then goes back into lib though?).
After upgrading to Mono 6.11, I get the following, slightly different error:
>>> import clr
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: System.TypeInitializationException: The type initializer for 'Sys' threw an exception. ---> System.DllNotFoundException: /lib/../lib/libmono-native.so assembly:<unknown assembly> type:<unknown type> member:(null)
at (wrapper managed-to-native) Interop+Sys.LChflagsCanSetHiddenFlag()
at Interop+Sys..cctor () [0x00000] in <c2db598b04474bb8b2ca952ec0b0ce8b>:0
--- End of inner exception stack trace ---
at Interop.GetRandomBytes (System.Byte* buffer, System.Int32 length) [0x00000] in <c2db598b04474bb8b2ca952ec0b0ce8b>:0
at System.Guid.FastNewGuidArray () [0x00020] in <c2db598b04474bb8b2ca952ec0b0ce8b>:0
at System.Reflection.Emit.ModuleBuilder..ctor (System.Reflection.Emit.AssemblyBuilder assb, System.String name, System.String fullyqname, System.Boolean emitSymbolInfo, System.Boolean transient) [0x00035] in <c2db598b04474bb8b2ca952ec0b0ce8b>:0
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule (System.String name, System.String fileName, System.Boolean emitSymbolInfo, System.Boolean transient) [0x0005b] in <c2db598b04474bb8b2ca952ec0b0ce8b>:0
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule (System.String name) [0x00000] in <c2db598b04474bb8b2ca952ec0b0ce8b>:0
at Python.Runtime.CodeGenerator..ctor () [0x0002b] in <1d25771ef5ab45e58a874c804663e59c>:0
at Python.Runtime.DelegateManager..ctor () [0x00061] in <1d25771ef5ab45e58a874c804663e59c>:0
at Python.Runtime.PythonEngine.Initialize (System.Collections.Generic.IEnumerable`1[T] args, System.Boolean setSysArgv, System.Boolean initSigs) [0x0000a] in <1d25771ef5ab45e58a874c804663e59c>:0
at Python.Runtime.PythonEngine.Initialize (System.Boolean setSysArgv, System.Boolean initSigs) [0x00005] in <1d25771ef5ab45e58a874c804663e59c>:0
at Python.Runtime.PythonEngine.InitExt () [0x00000] in <1d25771ef5ab45e58a874c804663e59c>:0
Judging by stack trace, this must be a Mono issue. You can try reproducing it by opening C# REPL and running System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule("__CodeGenerator_Module").
Compiled mono master branch directly and it seems to be fixed (mono v16.13.0) now a different dll is missing
[13:12] zer0:mono (master) $ python -c "import clr"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: libmonosgen-2.0.so.1: cannot open shared object file: No such file or directory
Turns out it just wasn't in my path. Fixing that gets me a slightly different error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: System.TypeInitializationException: The type initializer for 'Sys' threw an exception. ---> System.DllNotFoundException: /usr/local/lib/../lib/libmono-native.so assembly:<unknown assembly> type:<unknown type> member:(null)
at (wrapper managed-to-native) Interop+Sys.LChflagsCanSetHiddenFlag()
at Interop+Sys..cctor () [0x00000] in <41515cbda246402e80204eed794202d2>:0
--- End of inner exception stack trace ---
at Interop.GetRandomBytes (System.Byte* buffer, System.Int32 length) [0x00000] in <41515cbda246402e80204eed794202d2>:0
at System.Guid.FastNewGuidArray () [0x00020] in <41515cbda246402e80204eed794202d2>:0
at System.Reflection.Emit.ModuleBuilder..ctor (System.Reflection.Emit.AssemblyBuilder assb, System.String name, System.String fullyqname, System.Boolean emitSymbolInfo, System.Boolean transient) [0x00035] in <41515cbda246402e80204eed794202d2>:0
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule (System.String name, System.String fileName, System.Boolean emitSymbolInfo, System.Boolean transient) [0x0005b] in <41515cbda246402e80204eed794202d2>:0
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule (System.String name) [0x00000] in <41515cbda246402e80204eed794202d2>:0
at Python.Runtime.CodeGenerator..ctor () [0x0002b] in <78c98e72d96d408ea33ed377e3ab5fd1>:0
at Python.Runtime.DelegateManager..ctor () [0x00061] in <78c98e72d96d408ea33ed377e3ab5fd1>:0
at Python.Runtime.PythonEngine.Initialize (System.Collections.Generic.IEnumerable`1[T] args, System.Boolean setSysArgv, System.Boolean initSigs) [0x0000a] in <78c98e72d96d408ea33ed377e3ab5fd1>:0
at Python.Runtime.PythonEngine.Initialize (System.Boolean setSysArgv, System.Boolean initSigs) [0x00005] in <78c98e72d96d408ea33ed377e3ab5fd1>:0
at Python.Runtime.PythonEngine.InitExt () [0x00000] in <78c98e72d96d408ea33ed377e3ab5fd1>:0
@lostmsu Yeah I was able to get that recognized by adding it to path, but libmono-native.so is still not found. I feel like this path is super fishy /usr/local/lib/../lib/libmono-native.so. If it's not expanding correctly then it may be the culprit.
I'm actually observing the same issue on my Gentoo system, looking into it. The path is fine, the actual issue (if you run with MONO_LOG_LEVEL=debug) seems to be that Mono fails to find the symbol mono_add_internal_call_with_flags while running (or loading) libmononative.so. However, that symbol is defined in libmonosgen-2.0.so which is already loaded, so the issue seems to be with Mono's P/Invoke implementation seems to get confused with libraries that require Mono's own symbols.
I experienced a similar problem in Ubuntu 18.4, not via import clr but by loading the mono embedded runtime from a Python extension library written in C++ (I am not using PythonNet). There were a lot of exceptions similar to the ones mentioned here, including failing to find the symbol mono_add_internal_call_with_flags.
For me, the solution was to pre-load libmono-2.0.so and libstdc++.so (important: this library contains the _ZTIPi symbol)
Activity
levihb commented on Jan 24, 2020
After upgrading to Mono 6.11, I get the following, slightly different error:
intendednull commented on Mar 9, 2020
I am experiencing an identical issue. Also on Arch Linux.
lostmsu commented on Mar 9, 2020
Judging by stack trace, this must be a Mono issue. You can try reproducing it by opening C# REPL and running System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule("__CodeGenerator_Module").
intendednull commented on Mar 12, 2020
Compiled mono master branch directly and it seems to be fixed (mono v16.13.0) now a different dll is missing
Turns out it just wasn't in my path. Fixing that gets me a slightly different error:
intendednull commented on Mar 13, 2020
There's a similar issue open over at mono/mono#17079
So far everyone encountering the issue has been using Arch and Python.
lostmsu commented on Mar 15, 2020
@Zer0897 as a workaround you could try adding the true path to libmonosgen-2.0.so.1 to PATH?
intendednull commented on Mar 15, 2020
@lostmsu Yeah I was able to get that recognized by adding it to path, but libmono-native.so is still not found. I feel like this path is super fishy /usr/local/lib/../lib/libmono-native.so. If it's not expanding correctly then it may be the culprit.
filmor commented on May 10, 2020
I'm actually observing the same issue on my Gentoo system, looking into it. The path is fine, the actual issue (if you run with MONO_LOG_LEVEL=debug) seems to be that Mono fails to find the symbol mono_add_internal_call_with_flags while running (or loading) libmononative.so. However, that symbol is defined in libmonosgen-2.0.so which is already loaded, so the issue seems to be with Mono's P/Invoke implementation seems to get confused with libraries that require Mono's own symbols.
b0g2 commented on May 10, 2020
I experienced a similar problem in Ubuntu 18.4, not via import clr but by loading the mono embedded runtime from a Python extension library written in C++ (I am not using PythonNet). There were a lot of exceptions similar to the ones mentioned here, including failing to find the symbol mono_add_internal_call_with_flags.
For me, the solution was to pre-load libmono-2.0.so and libstdc++.so (important: this library contains the _ZTIPi symbol)
By doing this, the problem went away. It can also be done from code, via dlopen, prior to import clr.
Masutani commented on Apr 26, 2021
Same issue on Amazon Linux in SageMaker : https://stackoverflow.com/questions/67212443/pythonnet-on-sagemaker-cannot-import-clr
lostmsu commented on Apr 27, 2021
@Masutani there's a workaround for 2.4.0 mentioned above. Previews of 3.0.x do not have this issue anymore.