0

I am interested to call my existing python 3.8 modules from an developed WPF Application(Net Core3.1). I did the following:

  1. I installed pythonnet with: pip install pythonnet
  2. Set environment paths in C# (in VS2019) :

string pyInstallDir = @"C:\Python38"; Environment.SetEnvironmentVariable("PATH", pyInstallDir, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PYTHONHOME", pyInstallDir, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PYTHONPATH", Path.Combine(pyInstallDir, @"Lib\site-packages"), EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", pyInstallDir, EnvironmentVariableTarget.Process);

  1. Referenced the Python.Runtime.dll from the "C:\Python38\Lib\site-packages" folder in my project and added using Python.Runtime;

  2. Tried to import numpy(as Demo)

using (Py.GIL()) { dynamic np = Py.Import("numpy"); }

I get the following error:

Can anyone help to fix the error??

I have several approaches from the forums but without success.

Daniel
  • 1
  • 1

1 Answers1

0

If your Python modules are a dll file you can use AddDllDirectory

AddDllDirectory adds a directory for your program to search dll files for. By default dlls are only searched in the System32 folder and working directory (the location of your executable)

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern int AddDllDirectory(string NewDirectory);
Kye
  • 76
  • 1
  • 10
  • Unfortunately I still get the same error. Did it work for you? Is it possible that I'm doing something else wrong? – Daniel Nov 15 '22 at 09:33