"pyinstaller .exe在本地工作,但当被C#调用时失败?"

2024-07-01 07:20:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python2.7创建了一个脚本,并用pyinstaller将其编译成一个同名的exe,在本例中为“通用状态.py“变成”GeneralStats.exe“使用--onefile和-w参数。你知道吗

用C调用时,我使用:

var pythonDirectory = (Directory.GetCurrentDirectory());
var filePathExe1 = Path.Combine(pythonDirectory + "\\Python\\GeneralStats.exe");
Process.Start(filePathExe1);

在C#之外调用时,我可以在本地文件中运行.exe,结果是一个文本文件,其中包含许多值(正确运行)。你知道吗

但是,当用C#以这种格式运行时,我得到一个错误“GeneralStats returned-1!”你知道吗

我以前也遇到过这个问题,但这是一个简单的python错误,当我返回代码并运行它时,会收到一个我忽略的错误。 这次我的python代码不返回任何错误,并且在C#之外工作。你知道吗

你知道为什么会这样吗?我可以提供任何必要的代码或文件目录,请只是问,如果你觉得这将有助于调试。你知道吗

编辑:

解决方法是删除:

var filePathExe1 = Path.Combine(pythonDirectory + "\\Python\\GeneralStats.exe");
Process.Start(filePathExe1);

替换为:

ProcessStartInfo _processStartInfo = new ProcessStartInfo();
_processStartInfo.WorkingDirectory = Path.Combine(pythonDirectory + "\\Python");
_processStartInfo.FileName = @"GeneralStats.exe";
_processStartInfo.CreateNoWindow = true;
Process myProcess = Process.Start(_processStartInfo);

Tags: path代码脚本var错误processexestart
1条回答
网友
1楼 · 发布于 2024-07-01 07:20:48

您需要为Process设置工作目录—它可能试图从其工作目录加载文件,但找不到这些文件。你知道吗

参见,例如this

Use the ProcessStartInfo.WorkingDirectory property to set it prior to starting the process. If the property is not set, the default working directory is %SYSTEMROOT%\system32.

将其设置为GeneralStats.exe所在的路径。你知道吗

相关问题 更多 >

    热门问题