Attempting to call a simple Python script from C# but failing due to a "failed to get the Python codec" fatal error. How can I fix this?
Asked Answered
V

1

0

The relevant C# snippet is as follows:

 static string CallPython(string fileName)
        {
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo(@"C:\Python310\python.exe", fileName)
            {
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };
            p.Start();

            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();

            return output;
        }

and the relevant Python script, in its entirety, is:

Print("It worked?")

Unfortunately, I'm not getting the desired result and instead getting the following from my debug output:

Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = 'C:\Python310\python.exe'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = 'C:\\Python310\\python.exe'
  sys.base_prefix = 'C:\\Python310'
  sys.base_exec_prefix = 'C:\\Python310'
  sys.platlibdir = 'lib'
  sys.executable = 'C:\\Python310\\python.exe'
  sys.prefix = 'C:\\Python310'
  sys.exec_prefix = 'C:\\Python310'
  sys.path = [
    'C:\\Python310\\python310.zip',
    'C:\\Python310\\DLLs',
    'C:\\Python310\\lib',
    'C:\\Python310',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
Traceback (most recent call last):
  File "C:\Python310\lib\encodings\__init__.py", line 33, in <module>
ImportError: cannot import name 'aliases' from partially initialized module 'encodings' (most likely due to a circular import) (C:\Python310\lib\encodings\__init__.py)
'PythonCallExperiment.exe' (CLR v4.0.30319: PythonCallExperiment.exe): Loaded 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\PrivateAssemblies\Runtime\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

I tried setting UseShellExecute to true (nope).

Other searches for similar errors have returned results that didn't seem relevant to me (e.g. manually setting the PYTHONPATH). The script in question can easily be run outside of this experiment (thus I would suspect that my Python 3.10 installation probably isn't barfy). One pecularity that I will mention because I don't know the relevance is that I'm trying to do this on a VDI (I've frequently had issues with a lack of admin access for a number of other things, could that be the issue here?).

I have not yet tried to do the same thing with IronPython due to a desire to be able to reproducibly accomplish this by hassling devOPS as little as possible, but that's on the table in the future if there's just nothing to be done for me here.

Vocoid answered 8/12, 2022 at 1:24 Comment(2)
Did you see Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding, when trying to start uwsgi? Might that answer your question?Overbid
It seems relevant, but I don't believe uwsgi is installed on my vdi. Would I need it? Or are there settings elsewhere I should look for to try and set my virtual environment permissions properly?Vocoid
Q
0

I would like to share my solution, it works successfully at one of my projects..

 static string CallPython(string inputText)
    {
        Process p = new Process();
        p.StartInfo = new ProcessStartInfo()
        {
            FileName = "C:\\Python310\\python.exe",
            Arguments = "C:\\myPyscripts\\translateEng.py --text \""+inputText + "\\" ,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };
        p.Start();

        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        return output;
    }
Quintessa answered 11/4, 2023 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.