简体   繁体   English

由于 Pytho.net 缺少“编码”模块,无法从 C# 运行 Python 脚本

[英]Cannot run Python script from C# due to missing "encodings" module with Pythonnet

I'm trying to run a Python module from within C# by using an existing (and working) virtualenv.我试图通过使用现有的(和工作中的)virtualenv 从 C# 中运行 Python 模块。 I get ModuleNotFoundError: No module named 'encodings' , maybe due to an incorrect loading of the Python DLL.我收到ModuleNotFoundError: No module named 'encodings' ,可能是由于错误加载了 Python DLL。

I tried every possible order combo when executing Runtime.PythonDLL = @"C:\Users\ardiiva\AppData\Local\Programs\Python\Python38\python38.dll";我在执行Runtime.PythonDLL = @"C:\Users\ardiiva\AppData\Local\Programs\Python\Python38\python38.dll"; . . What I've noticed is that the python38.dll file is not present under my virtualenv folder, so I tried to copy it from my local windows machine user installation of Python, but also trying to reference it directly.我注意到的是python38.dll文件不在我的 virtualenv 文件夹下,所以我尝试从我的本地 windows 机器用户安装 Python 复制它,但也试图直接引用它。 The venv was created using the 3.8.9 Python version. venv 是使用 3.8.9 Python 版本创建的。 Do you have any idea?你有什么主意吗? Why I don't have the python.dll?为什么我没有 python.dll? Should it be generated in some way?它应该以某种方式生成吗?

    var pathToVirtualEnv = @"C:\rebrandingmanager\CFGRebrandingPythonManager\codice\venv";
            Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", @"C:\Users\ardiiva\AppData\Local\Programs\Python\Python38\python38.dll", EnvironmentVariableTarget.Process);
            Runtime.PythonDLL = @"C:\Users\ardiiva\AppData\Local\Programs\Python\Python38\python38.dll";

            var path = Environment.GetEnvironmentVariable("PATH").TrimEnd(';');
            path = string.IsNullOrEmpty(path) ? pathToVirtualEnv : path + ";" + pathToVirtualEnv;
            Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("PYTHONHOME", pathToVirtualEnv, EnvironmentVariableTarget.Process);            
            Environment.SetEnvironmentVariable("PYTHONPATH", $"{pathToVirtualEnv}\\Lib\\site-packages;{pathToVirtualEnv}\\Lib", EnvironmentVariableTarget.Process);            
            //PYTHONNET_PYDLL 
            
            PythonEngine.PythonHome = pathToVirtualEnv;
            PythonEngine.PythonPath = Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process);

            PythonEngine.Initialize();
            using (Py.GIL())
            {
                dynamic aaaa = Py.Import("TestPy.py");
                aaaa.add_watermark("CIAOOOOO", "KAKAKAKAKAKAK.pdf", "RG-39119_002_A_001.pdf");
            }

The traceback is:追溯是:

Exit code is 1 (Python path configuration:
  PYTHONHOME = 'C:\rebrandingmanager\CFGRebrandingPythonManager\codice\venv'
  PYTHONPATH = 'C:\rebrandingmanager\CFGRebrandingPythonManager\codice\venv\Lib\site-packages;C:\rebrandingmanager\CFGRebrandingPythonManager\codice\venv\Lib'
  program name = 'python'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = 'C:\\Users\\ardiiva\\AppData\\Local\\JetBrains\\Installations\\Rider221\\lib\\ReSharperHost\\TestRunner\\net461\\ReSharperTestRunner.exe'
  sys.base_prefix = ''
  sys.base_exec_prefix = ''
  sys.executable = 'C:\\Users\\ardiiva\\AppData\\Local\\JetBrains\\Installations\\Rider221\\lib\\ReSharperHost\\TestRunner\\net461\\ReSharperTestRunner.exe'
  sys.prefix = ''
  sys.exec_prefix = ''
  sys.path = [
    'C:\\rebrandingmanager\\CFGRebrandingPythonManager\\codice\\venv\\Lib\\site-packages',
    'C:\\rebrandingmanager\\CFGRebrandingPythonManager\\codice\\venv\\Lib',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

It appears that the standard PYTHONPATH is not set with the use of the dll. In my case, I took the dll dir ( @"C:\Users\ardiiva\AppData\Local\Programs\Python\Python38"; in your case) and added the proper folders to PythonEngine.PythonPath, using the function:使用 dll 似乎没有设置标准 PYTHONPATH。在我的例子中,我使用了 dll 目录( @"C:\Users\ardiiva\AppData\Local\Programs\Python\Python38";在你的例子中)并使用 function 将适当的文件夹添加到 PythonEngine.PythonPath:

string PythonPath(string pythonDir)
{
    return $"{pythonDir};{pythonDir}\\DLLs;{pythonDir}\\Lib;{pythonDir}\\Lib\\site-packages";
}

and then:然后:

string pythonPath = PythonPath(Path.GetDirectoryName(pythonDll) ?? ".");
PythonEngine.PythonPath += $";{pythonPath}";

Did the same for the virtual environment:对虚拟环境做了同样的事情:

string additional = PythonPath(pathToVirtualEnv);
PythonEngine.PythonPath +=  $";{additional}";

where pathToVirtualEnv is my .venv path.其中pathToVirtualEnv是我的.venv路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM