0

I am trying to run some Python scripts in my UWP project with pythonnet. After setting Runtime.PythonDLL equal to python38.dll path, PythonEngine.Initialize(); throws a 'System.DllNotFoundException'. Specifically, Win32Exception: Access Denied. Here's the code that I am trying to run:

private void searchCatButton_Click(object sender, RoutedEventArgs e) {

            Runtime.PythonDLL = @"**path to Python38**\python38.dll";

            PythonEngine.Initialize();
            using(Py.GIL()) {

                dynamic os = Py.Import("os");
                dynamic dir = os.listdir();
                Console.WriteLine(dir);

                foreach (var d in dir) {

                    Console.WriteLine(d);
                }
            }
        }

The python38.dll file is located in AppData folder. Not sure if I am supposed to give some kind of permissions to the project or if I am missing something much bigger.

Thanks for the help in advance, and let me know if there's information missing from the post!

Edit:

I tried to run as administrator... same error. I created a whole new project and can't seem to get it to work there either. I'm completely out of ideas.

Edit 2.0:

I was able to get rid of this error by installing the LostTech.Python.Runtime nuget package. Someone recommended from a different post. But now I'm getting a different error... PythonEngine.Initialize(); now outputs the following error message: Exception thrown: 'System.TypeInitializationException' in Python.Runtime.dll The type initializer for 'Delegates' threw an exception.. Not sure if my previous error is fixed by installing the nuget package, but now I have this error I cannot seem to fix.

levente.nas
  • 83
  • 2
  • 10
  • Hi does this help https://stackoverflow.com/questions/11779143/how-do-i-run-a-python-script-from-c – ChrisBD Aug 21 '22 at 15:45
  • Hi! Unfortunately I couldn't find a solution from the link you sent. Most of those answers solve issues regarding IronPython. Since I'm using Python 3.8, IronPython isn't an option for me... which is why I though pythonnet is the best option. – levente.nas Aug 21 '22 at 15:55
  • What was/is the Win32 error code in that `Win32Exception`? That's kinda important... – Dai Aug 22 '22 at 09:22
  • It was "access denied", and it could not load python38.dll. Since I installed the nuget package I get "Exception thrown: 'System.IO.FileLoadException' in Python.Runtime.dll Could not load C:\Users\leven\AppData\Local\Programs\Python\Python38\python38.dll". – levente.nas Aug 22 '22 at 09:53

0 Answers0