pythonc#python.net

Call Python from C# (Error: Runtime.PythonDLL was not set or does not point to a supported Python runtime DLL)


This is my code:

using Python.Runtime;
using System;

class Program
{
    static void Main()
    {
        try
        {
            // Set Python home and DLL paths
            PythonEngine.PythonHome = @"C:\Users\toto\AppData\Local\Programs\Python\Python311";
            Runtime.PythonDLL = @"C:\Users\toto\AppData\Local\Programs\Python\Python311\python311.dll";

            // Initialize the Python engine
            PythonEngine.Initialize();

            using (Py.GIL())
            {
                // Import and use Python modules
                dynamic sys = Py.Import("sys");
                Console.WriteLine($"Python version: {sys.version}");
            }

            // Shutdown the Python engine
            PythonEngine.Shutdown();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
}

I'm getting this console print out

Error: Runtime.PythonDLL was not set or does not point to a supported Python runtime DLL. See https://github.com/pythonnet/pythonnet#embedding-python-in-net

C:\Users\toto\Desktop\C# Python\ConsoleApp1\ConsoleApp1\bin\Debug\net6.0\ConsoleApp1.exe (process 6368) exited with code 0 (0x0).

Things that I have tried: I tried different Python versions from 8 to 12, I downgraded Target framework form 8 to 6 and still no luck. How can I solve this? I looked at similar topics and their solutions do not work for me, please help

Edit: My platform Target is x64

Microsoft Windows [Version 10.0.18363.418]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\toto>python -c "import platform; print(platform.architecture()[0])"
64bit

C:\Users\toto>where python
C:\Users\toto\AppData\Local\Programs\Python\Python311\python.exe
C:\Users\toto\AppData\Local\Microsoft\WindowsApps\python.exe

C:\Users\toto>

Solution

  • i removed "PythonEngine.PythonHome = @"C:\Users\toto\AppData\Local\Programs\Python\Python310";" and it worked.

    using Python.Runtime;
    using System;
    
    class Program
    {
        static void Main()
        {
            try
            {
                // Set Python home and DLL paths
                //PythonEngine.PythonHome = @"C:\Users\toto\AppData\Local\Programs\Python\Python310";
                Runtime.PythonDLL = @"C:\Users\toto\AppData\Local\Programs\Python\Python310\python310.dll";
    
                // Initialize the Python engine
                PythonEngine.Initialize();
    
                using (Py.GIL())
                {
                    // Import and use Python modules
                    dynamic sys = Py.Import("sys");
                    Console.WriteLine($"Python version: {sys.version}");
                }
    
                // Shutdown the Python engine
                PythonEngine.Shutdown();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }