Import Pandas library using PythonNet in .Net

46 Views Asked by At

I'm trying to import Pandas library using PythonNet. It always throws error "No Module names pandas".

        static void Main(string[] args)
        {
            // Set the path to the Python DLL
            Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", @"C:\Users\username\AppData\Local\Programs\Python\Python311-32\python311.dll", EnvironmentVariableTarget.Process);

            // Initialize Python engine
            PythonEngine.Initialize();

            // Use the Python Global Interpreter Lock (GIL)
            using (Py.GIL())
            {
                var currentDirectory = Directory.GetCurrentDirectory();
                var pythonFilePath = Path.GetFullPath(Path.Combine(currentDirectory, @"..\..\..\"));
                dynamic sys = Py.Import("sys");
                sys.path.append(pythonFilePath);

                dynamic pd = Py.Import("pandas"); // Error at this line

                dynamic pythonScript = Py.Import("mypythonscript");
                // Call the 'say_hello' function from the Python module
                dynamic result = pythonScript.return_dataset();

                // Print the result
                Console.WriteLine(result);

                // Print the result
                Console.WriteLine(result);
            }

            // Shutdown Python engine
            PythonEngine.Shutdown();
        }
  1. The official documentation does not mention much.
  2. Tried chat gpt
0

There are 0 best solutions below