You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@aaronavi, sorry for the very late reply. With the new NumSharp.Interop.pythonnet package (0.70.0, #628) you can pull the array straight out of the dict.
Set up the Python engine once at startup - either point pythonnet at an installed Python, or deploy one from NuGet with Python.Included (no system install needed):
usingNumSharp;usingNumSharp.Interop.PythonNet;usingPython.Runtime;// Option A - existing Python: the PYTHONNET_PYDLL env var, or the DLL directlyRuntime.PythonDLL="python312.dll";// or Environment.GetEnvironmentVariable("PYTHONNET_PYDLL")PythonEngine.Initialize();// Option B - deploy an embedded Python from NuGet (dotnet add package Python.Included)awaitPython.Included.Installer.SetupPython();awaitPython.Included.Installer.PipInstallModule("numpy");PythonEngine.Initialize();
Explicit:
using(Py.GIL()){// pyDict is your PyObject (a Python dict) holding a numpy arrayPyObjectarr=pyDict["weights"];// the ndarray, still a PyObjectNDArraynd=arr.AsNDArray();// -> NumSharp NDArrayConsole.WriteLine(nd.shape[0]);// 4}
Implicit (register the codec once, then use pythonnet's own As<NDArray>()):
NDArrayPythonInterop.RegisterCodec();// once per processusing(Py.GIL()){PyObjectarr=pyDict["weights"];NDArraynd=arr.As<NDArray>();// implicit decode via the codecConsole.WriteLine(nd.shape[0]);// 4}
Activity
Nucs commentedon Sep 7, 2026
@aaronavi, sorry for the very late reply. With the new NumSharp.Interop.pythonnet package (0.70.0, #628) you can pull the array straight out of the dict.
Set up the Python engine once at startup - either point pythonnet at an installed Python, or deploy one from NuGet with Python.Included (no system install needed):
Explicit:
Implicit (register the codec once, then use pythonnet's own
As<NDArray>()):Requires NumSharp.Interop.pythonnet (
dotnet add package NumSharp.Interop.pythonnet). Closing.