本文介绍了pyinstaller .exe在本地工作,但是在被C#调用时失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Python2.7创建了一个脚本,并使用pyinstaller将其编译为同名的exe,在这种情况下,使用--onefile和-w参数将"GeneralStats.py"转换为"GeneralStats.exe"./p>

使用C#调用时,我使用:

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

在C#外部调用时,因此在我的本地文件中,我可以运行.exe,结果是一个文本文件,其中包含很多值(正确运行).

但是,当以这种格式运行C#时,出现错误消息"GeneralStats返回-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;流程myProcess = Process.Start(_processStartInfo); 
解决方案

您需要为 Process 设置工作目录-它可能正在尝试从其工作目录中加载文件,但不是找到他们.

例如,参见:

将其设置为 GeneralStats.exe 所在的路径.

I have created a script using Python2.7 and compiled it using pyinstaller into an exe of the same name, in this case "GeneralStats.py" turns into "GeneralStats.exe" using --onefile and -w arguments.

When called with C# I use:

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

When called outside of C#, so in my local files I can run the .exe and the result is a text file with lots of values in (Running correctly).

However, when ran with C# in this format, I get an error that "GeneralStats returned -1!"

Which I have had issues with before, but it was a simple python error that when I returned to my code and ran it, I would receive an error that I overlooked.This time my python code returns no errors and works outside of C#.

Any ideas of why this could be? I can provide any code or file directories necessary, please just ask if you feel it would help with debugging.

EDIT:

Solved by removing:

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

And replacing with:

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

You need to set the working directory for the Process - it is probably trying to load files from its working directory but isn't finding them.

See, e.g. this:

Set it to the path where GeneralStats.exe is.

这篇关于pyinstaller .exe在本地工作,但是在被C#调用时失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 04:16