Home > front end >  How to set Runtime.PythonDLL for PythonNET for Console & WPF .NET Application (PIP Installation)
How to set Runtime.PythonDLL for PythonNET for Console & WPF .NET Application (PIP Installation)

Time:10-25

I am trying to install PythonNET on Python 3.10 and embedding Python into the .NET console app first before subsequently trying it on the WPF app.

However, I am still confused about the installation for PythonNET, after downloading the Native code on GitHub and installing it within the unzipped code folder via pip install .. The install does complete successfully, and Python.RunTime.dll here C:\Users\Justin\AppData\Local\Programs\Python\Python310\Lib\site-packages\pythonnet\runtime\Python.RunTime.dll.

However, when I try pointing towards it in C# code, it still triggers the BadPythonDllException error.

C# Configuration

using System;
using System.Collections.Generic;        // For some implementation using List<> later on
using Python.Runtime;

namespace ConsoleApp_PythonNET_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string EnvPath = @"C: \Users\Justin\AppData\Local\Programs\Python\Python310";
            string pythonPath = @"C:\Users\Justin\AppData\Local\Programs\Python\Python310\Lib\site-packages\pythonnet";
            Environment.SetEnvironmentVariable("PATH", EnvPath, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("PYTHONHOME", EnvPath, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("PYTHONPATH", pythonPath, EnvironmentVariableTarget.Process);
            PythonEngine.PythonHome = Environment.GetEnvironmentVariable("PYTHONHOME", EnvironmentVariableTarget.Process);
            PythonEngine.PythonPath = Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process);

            //Using (Py.GIL()) Method for some Python implementations
        }
    }
}

Can I check what I am missing? Am I simply still pointing towards the wrong dir? Additionally, would the embedding process be the same within a WPF application, or would there be any additional step(s) required?

Thanks in advance! Cheers.

CodePudding user response:

From the README in the repository:

Runtime.PythonDLL ... typical values are python38.dll (Windows), libpython3.8.dylib (Mac), libpython3.8.so (most other *nix).

E.g. you need to set Runtime.PythonDLL to path to python39.dll.

When embedding into C#, there's no need to install the pip package. The NuGet will be enough.

  • Related