Numpy.NET icon indicating copy to clipboard operation
Numpy.NET copied to clipboard

Azure AppService and System.DllNotFoundException: Could not load python311.dll with flags RTLD_NOW | RTLD_GLOBAL: python311.dll: cannot open shared object file: No such file or directory

Open dariusz-wozniak opened this issue 1 year ago • 5 comments

Hi, I'm using Numpy 3.11.1.33 and:

When running the project locally, the project along with Numpy-related code works fine.

But, after deployment to the Azure AppService, website stopped working with error:

Unhandled exception. System.TypeInitializationException: The type initializer for 'Delegates' threw an exception.
---> System.DllNotFoundException: Could not load python311.dll with flags RTLD_NOW | RTLD_GLOBAL: python311.dll: cannot open shared object file: No such file or directory
at Python.Runtime.Platform.PosixLoader.Load(String dllToLoad)
at Python.Runtime.Runtime.Delegates.GetUnmanagedDll(String libraryName)
at Python.Runtime.Runtime.Delegates..cctor()
--- End of inner exception stack trace ---
at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args, Boolean setSysArgv, Boolean initSigs)
at Python.Runtime.PythonEngine.Initialize(Boolean setSysArgv, Boolean initSigs)
at Python.Runtime.PythonEngine.Initialize()
at Numpy.np.InstallAndImport(Boolean force)
at Numpy.np.<>c.b__531_0()
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
at System.Lazy`1.CreateValue()
at System.Lazy`1.get_Value()
at Numpy.np.get_self()
at Numpy.np.arange(Int32 stop, Int32 step, Dtype dtype)
// ..

I'm using initialization of Python in the Program.cs before CreateHostBuilder(args).Build();:

np.arange(1);
Python.Runtime.PythonEngine.BeginAllowThreads();

Any ideas what's wrong? Is that because of installation fails? If yes, is it possible to use portable Python that is placed under application after build?

Note that targeted machine is a Linux, but the project is used locally in Windows/Linux/Mac environments.

dariusz-wozniak avatar Apr 03 '23 08:04 dariusz-wozniak

Probably the installation fails because the default install directory is in the user directory.

henon avatar Apr 03 '23 11:04 henon

When I run:

import sys; print(sys.path)

I get:

['', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages']

How can I point to the proper directory?

dariusz-wozniak avatar Apr 03 '23 12:04 dariusz-wozniak

I'll look later when I got time as I am quite busy but you can check out Python.Included which is used by numpy.net under the hood and see if you find out yourself.

henon avatar Apr 03 '23 12:04 henon

Can we assume that Numpy.NET using Python.Included is a windows-only package (and thus not suitable for azure linux-based containers)?

ddombrowsky avatar Jul 08 '23 02:07 ddombrowsky

Yes Python.Included and Numpy.NET are Windows only libs because they contain the windows version of python and numpy, but they each have a companion library that can be used with linux. You have to pack a linux version of python and the numpy wheel in your .NET assembly (as embedded resources) and then use Python.Deployment to install them. See the examples in the https://github.com/henon/Python.Included repo. Then you can use Numpy.Bare to use the numpy wheel you installed.

henon avatar Jul 08 '23 16:07 henon