Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.DllNotFoundException when trying to import clr, despite referenced assembly existing #1034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
levihb opened this issue Jan 24, 2020 · 11 comments Β· Fixed by #1139
Closed

Comments

@levihb
Copy link

levihb commented Jan 24, 2020

Environment

  • Pythonnet version: Tried latest from pip, latest from git
  • Python version: Tried 3.5, 3.7, 3.8
  • Operating System: Arch Linux

Details

Trying to import clr so that I can import a library.

import clr

When trying to import this, the following error is thrown:

>>> import clr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: System.DllNotFoundException: /usr/lib/../lib/libmono-native.so assembly:<unknown assembly> type:<unknown type> member:(null)
  at (wrapper managed-to-native) Interop+Sys.GetNonCryptographicallySecureRandomBytes(byte*,int)
  at Interop.GetRandomBytes (System.Byte* buffer, System.Int32 length) [0x00000] in <894a218ab05a4151a4fb84b414f81888>:0 
  at System.Guid.FastNewGuidArray () [0x00020] in <894a218ab05a4151a4fb84b414f81888>: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 <894a218ab05a4151a4fb84b414f81888>:0 
  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule (System.String name, System.String fileName, System.Boolean emitSymbolInfo, System.Boolean transient) [0x0005b] in <894a218ab05a4151a4fb84b414f81888>:0 
  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule (System.String name) [0x00000] in <894a218ab05a4151a4fb84b414f81888>: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 
>>> 

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?).

@levihb
Copy link
Author

levihb commented Jan 24, 2020

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 

@intendednull
Copy link

intendednull commented Mar 9, 2020

I am experiencing an identical issue. Also on Arch Linux.

(venv) [13:57] zer0:pythonnet (master) $ python -c "import clr"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: System.DllNotFoundException: /usr/lib/../lib/libmono-native.so assembly:<unknown assembly> type:<unknown type> member:(null)
  at (wrapper managed-to-native) Interop+Sys.GetNonCryptographicallySecureRandomBytes(byte*,int)
  at Interop.GetRandomBytes (System.Byte* buffer, System.Int32 length) [0x00000] in <c4bddbfe864a4b8191bb818d6b204e9d>:0
  at System.Guid.FastNewGuidArray () [0x00020] in <c4bddbfe864a4b8191bb818d6b204e9d>: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 <c4bddbfe864a4b8191bb818d6b204e9d>:0
  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule (System.String name, System.String fileName, System.Boolean emitSymbolInfo, System.Boolean transient) [0x0005b] in <c4bddbfe864a4b8191bb818d6b204e9d>:0
  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule (System.String name) [0x00000] in <c4bddbfe864a4b8191bb818d6b204e9d>: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
Copy link
Member

lostmsu commented 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
Copy link

intendednull commented Mar 12, 2020

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

@intendednull
Copy link

intendednull commented 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
Copy link
Member

lostmsu commented Mar 15, 2020

@Zer0897 as a workaround you could try adding the true path to libmonosgen-2.0.so.1 to PATH?

@intendednull
Copy link

@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
Copy link
Member

filmor commented 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
Copy link

b0g2 commented 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)

LD_PRELOAD="libmono-2.0.so libstdc++.so" python3 test/test.py

By doing this, the problem went away. It can also be done from code, via dlopen, prior to import clr.

@filmor filmor mentioned this issue May 10, 2020
4 tasks
@Masutani
Copy link

Same issue on Amazon Linux in SageMaker : https://stackoverflow.com/questions/67212443/pythonnet-on-sagemaker-cannot-import-clr

@lostmsu
Copy link
Member

lostmsu commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants