[英]pyinstaller .exe works locally but fails when called by C#?
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. 我使用Python2.7创建了一个脚本,并使用pyinstaller将其编译为同名的exe,在这种情况下,使用--onefile和-w参数将“ GeneralStats.py”转换为“ GeneralStats.exe”。
When called with C# I use: 用C#调用时,我使用:
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). 当在C#外部调用时,因此在我的本地文件中,我可以运行.exe,结果是一个文本文件,其中包含很多值(正确运行)。
However, when ran with C# in this format, I get an error that "GeneralStats returned -1!" 但是,当以这种格式运行C#时,出现错误“ GeneralStats返回-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. 我以前遇到过问题,但这是一个简单的python错误,当我返回代码并运行它时,会收到一个我忽略的错误。 This time my python code returns no errors and works outside of C#.
这次,我的python代码未返回任何错误,并且可以在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. 您需要为
Process
设置工作目录-它可能正在尝试从其工作目录加载文件,但找不到它们。
Use the
ProcessStartInfo.WorkingDirectory
property to set it prior to starting the process.在启动过程之前,请使用
ProcessStartInfo.WorkingDirectory
属性进行设置。 If the property is not set, the default working directory is%SYSTEMROOT%\\system32
.如果未设置该属性,则默认工作目录为
%SYSTEMROOT%\\system32
。
Set it to the path where GeneralStats.exe
is. 将其设置为
GeneralStats.exe
所在的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.