简体   繁体   English

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

[英]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设置工作目录-它可能正在尝试从其工作目录加载文件,但找不到它们。

See, eg this : 见,例如这个

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.

相关问题 C#:从测试方法中调用时方法失败,但在其他情况下效果很好 - C#: Method fails when called from test method but works fine otherwise 使用C#运行PyInstaller Exe文件 - Run PyInstaller Exe File with C# C#DLL在本地运行良好,但在作为WCF服务公开时却无法正常工作 - C# DLL works fine locally, but not when exposed as WCF service 从本地计算机使用时,调用SQL存储过程的C#函数可以工作,但是从云中的Azure函数调用时,失败 - C# function that calls a SQL stored procedure works when used from local machine but fails when called from an Azure function in the cloud 调用时.exe进程未从C#应用程序运行 - .exe processes are not running from a C# application when called WebApi在本地工作,但在发布时失败 - WebApi works locally but when published It fails 代理在本地工作,但在上传到webhost时失败 - Proxy works locally but fails when uploaded to webhost 在VS内从C#调用C ++ DLL失败。 在VS外运行exe,它可以工作 - Calling C++ DLL from C# within VS fails. Run exe outside VS and it works 在 C# 控制台应用程序中运行 Python EXE(在 PyInstaller 中创建) - Run a Python EXE (created in PyInstaller) in C# Console Application C#文件上传在本地工作; 不在Azure上 - C# File Upload works locally; not on Azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM