Running Python 3.7 from C# using Python .NET

5

I am trying to include a sample script (the one provided in the README.md) into a test C# code in Visual Studio 2019. I am using a Python virtualenv.

Some information on my environment:

  • Pythonnet version: pythonnet-2.4.0-cp37-cp37m-win_amd64.whl
  • Python version: Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)]
  • Operating System: Windows 10 64-bit

First of all, I created a brand-new virtualenv and installed the package, as long as numpy.

mkvirtualenv pynet
workon pynet
pip3 install numpy
pip3 install pythonnet

This was the resulting output for pythonnet:

Collecting pythonnet
  Using cached pythonnet-2.4.0-cp37-cp37m-win_amd64.whl (70 kB)
Installing collected packages: pythonnet
Successfully installed pythonnet-2.4.0

I created a sample Visual Studio 2019 project. The first thing I did was unset the tick on Properties -> Build -> Prefer 32-bit, then I added to reference the Python.Runtime.dll, in my case it was under C:\Users\andrea\Envs\pynet\Lib\site-packages\Python.Runtime.dll. Then I created the following sample script:

using Python.Runtime;
using System;
using System.Collections.Generic;

namespace WrapperPython
{
    class Program
    {
        static void Main()
        {
            var pythonPath = @"C:\Users\andrea\Envs\pynet\Scripts";
            //var pythonPath = @"C:\Users\andrea\AppData\Local\Programs\Python\Python37";

            Environment.SetEnvironmentVariable("PATH", $@"{pythonPath};" + Environment.GetEnvironmentVariable("PATH"));
            Environment.SetEnvironmentVariable("PYTHONHOME", pythonPath);

            Environment.SetEnvironmentVariable("PYTHONPATH ", $@"{pythonPath}\..\Lib;{pythonPath}\..\Lib\site-packages;");
            //Environment.SetEnvironmentVariable("PYTHONPATH ", $@"{pythonPath}\Lib");

            PythonEngine.PythonHome = Environment.GetEnvironmentVariable("PYTHONHOME", EnvironmentVariableTarget.Process);
            PythonEngine.PythonPath = Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process);

            using (Py.GIL())
            {
                dynamic np = Py.Import("numpy");
                Console.WriteLine(np.cos(np.pi * 2));

                dynamic sin = np.sin;
                Console.WriteLine(sin(5));

                double c = np.cos(5) + sin(5);
                Console.WriteLine(c);

                dynamic a = np.array(new List<float> { 1, 2, 3 });
                Console.WriteLine(a.dtype);

                dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);
                Console.WriteLine(b.dtype);

                Console.WriteLine(a * b);
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }
    }
}

But I get the following message into the opened prompt:

Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00003504 (most recent call first):

And then it terminates with error:

'WrapperPython.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. 
'WrapperPython.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\andrea\Documents\Personali\visual studio\WrapperPython\bin\Debug\WrapperPython.exe'. Symbols loaded.
'WrapperPython.exe' (CLR v4.0.30319: WrapperPython.exe): Loaded 'C:\Users\andrea\Documents\Personali\visual studio\WrapperPython\bin\Debug\Python.Runtime.dll'. 
'WrapperPython.exe' (CLR v4.0.30319: WrapperPython.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. 
'WrapperPython.exe' (CLR v4.0.30319: WrapperPython.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. 
'WrapperPython.exe' (CLR v4.0.30319: WrapperPython.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll'. 
'WrapperPython.exe' (CLR v4.0.30319: WrapperPython.exe): Loaded '__CodeGenerator_Assembly'. 
The program '[28052] WrapperPython.exe' has exited with code -1073740791 (0xc0000409).

It is such a simple example that I can't figure out what could have gone wrong.


UPDATE: It seems a problem only with the virtualenv. I performed the same setup for the "global" installation of python, and it is working. Just switching the commented rows in the path from the previous example, it works.

How can I make it work inside a virtualenv?

c#
.net-core
python.net
asked on Stack Overflow Mar 26, 2020 by Andrea • edited Mar 26, 2020 by Andrea

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0