相关的C#代码片段如下:
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;
}
相关的 Python 脚本完整如下:
Print("It worked?")
不幸的是,我没有得到想要的结果,而是从调试输出中得到以下内容:
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.
我尝试将 UseShellExecute 设置为 true (不)。
对类似错误的其他搜索返回的结果似乎与我无关(例如手动设置 PYTHONPATH)。有问题的脚本可以很容易地在这个实验之外运行(因此我怀疑我的 Python 3.10 安装可能没有呕吐)。我要提到的一个特点是,因为我不知道相关性,我正在尝试在 VDI 上执行此操作(我经常遇到缺乏对其他许多事情的管理员访问权限的问题,这可能是问题在这里?)。
我还没有尝试用 IronPython 做同样的事情,因为我希望能够通过尽可能少地干扰 devOPS 来重复地完成这一任务,但如果我在这里无事可做,那么这就是将来的事情了。