Empty output when calling a python script in C# project - python

I would like to call a python script in my C# project , I'm using this function to do the job but unfortunately I didn't get any result and the result variable shows always an empty output. I would like to know what's the reason of this
public string RunFromCmd(string rCodeFilePath, string args)
{
string file = rCodeFilePath;
string result = string.Empty;
try
{
var info = new ProcessStartInfo(pythonPath);
info.Arguments = #"C:\Users\MyPc\ExternalScripts\HelloWorld.py" + " " + args;
info.RedirectStandardInput = false;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.CreateNoWindow = true;
using (var proc = new Process())
{
proc.StartInfo = info;
proc.Start();
proc.WaitForExit();
if (proc.ExitCode == 0)
{
result = proc.StandardOutput.ReadToEnd();
}
}
return result;
}
catch (Exception ex)
{
throw new Exception("R Script failed: " + result, ex);
}
}
Click Event ( Calling funtion )
private void Button1_Click(object sender, RoutedEventArgs e)
{
pythonPath = Environment.GetEnvironmentVariable("PYTHON_PATH");
RunFromCmd(pythonPath, "");
}
Python Script :
import sys
def main():
text = "Hello World"
return text
result = main()

I've fixed the issue by setting Copy if newer instead of Do Not Copy to HelloWorld.py Script

Related

.py file executed by C# process not waiting to finish

I want to run .py file from my C# project, and get the result. The python script is making an API request, and returns an auth_key token, which I want to use in my C# code. The only problem is that, for some reason the C# code doesn't wait for the process to finish, and thus that not every account has auth_key. Here is my C# code.
private static void GenerateTokens()
{
var url = ConfigurationManager.AppSetting[GeSettingsNode() + ":ip"];
for (int i = 0; i < accounts.Count; i++)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = ConfigurationManager.AppSetting["PythonPath"];
start.Arguments = string.Format($"python_operation_processor.py {accounts[i].client_key_id} {accounts[i].key_sercret_part} {url}");
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
Process process = Process.Start(start);
using (StreamReader reader = process.StandardOutput)
{
accounts[i].auth_key = reader.ReadToEnd().Trim();
}
}
}
And here is my Python script ( python_operation_processor.py )that's making the API requests.
if __name__ == '__main__':
client_key_id = sys.argv[1]
client_secret = sys.argv[2]
API_URL = sys.argv[3]
nonce = str(uuid.uuid4())
d = datetime.datetime.now() - datetime.timedelta(hours=3)
timestamp = d.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
signature = b64encode(hmac.new(b64decode(client_secret), msg=bytes(client_key_id + nonce + timestamp, 'utf-8'),
digestmod=hashlib.sha256).digest()).decode('utf-8')
r = requests.post(API_URL + '/v1/authenticate',
json={'client_key_id': client_key_id, 'timestamp': timestamp, 'nonce': nonce,
'signature': signature})
if r.status_code != 200:
raise Exception('Failed to authenticate: ' + r.text)
auth_token = r.json()['token']
print(auth_token)
Do you have any idea, how I can wait for the execution of every process, and get the token for every account ?
I recently created something similar and ended up with this because, whilst waiting for the process is easy, it is tricky to get the output stream filled correctly.
The method presented also allow you to display the output into a textblock or similar in your application.
If you use it like this, the token will be written to the StringBuilder, and used as return value.
private async Task<string> RunCommand(string fileName, string args)
{
var timeoutSignal = new CancellationTokenSource(TimeSpan.FromMinutes(3));
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = fileName;
start.Arguments = string.Format("{0}", args);
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
start.UseShellExecute = false;
start.CreateNoWindow = true;
var sb = new StringBuilder();
using (Process process = new Process())
{
process.StartInfo = start;
process.OutputDataReceived += (sender, eventArgs) =>
{
sb.AppendLine(eventArgs.Data); //allow other stuff as well
};
process.ErrorDataReceived += (sender, eventArgs) => {};
if (process.Start())
{
process.EnableRaisingEvents = true;
process.BeginOutputReadLine();
process.BeginErrorReadLine();
await process.WaitForExitAsync(timeoutSignal.Token);
//allow std out to be flushed
await Task.Delay(100);
}
}
return sb.ToString();
}
To render this to a textblock in a UI application, you'll need to:
implement an event which signals a new line has been read, which means forwarding the process.OutputDataReceived event.
if your thinking about a live feed, make sure you flush the stdio buffer in python setting flush to true: print(""hello world"", flush=True)
If you're using an older .net version; you can implement the WaitForExitAsync as described here: https://stackoverflow.com/a/17936541/2416958 as an extention method:
public static class ProcessHelpers
{
public static Task<bool> WaitForExitAsync(this Process process, TimeSpan timeout)
{
ManualResetEvent processWaitObject = new ManualResetEvent(false);
processWaitObject.SafeWaitHandle = new SafeWaitHandle(process.Handle, false);
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
RegisteredWaitHandle registeredProcessWaitHandle = null;
registeredProcessWaitHandle = ThreadPool.RegisterWaitForSingleObject(
processWaitObject,
delegate(object state, bool timedOut)
{
if (!timedOut)
{
registeredProcessWaitHandle.Unregister(null);
}
processWaitObject.Dispose();
tcs.SetResult(!timedOut);
},
null /* state */,
timeout,
true /* executeOnlyOnce */);
return tcs.Task;
}
}

How to fix "can't find '__main__' module in '' error?

I am calling my python file from Windows Service
the code is working fine by itself
and the service works fine too
but when I call the python code from the windows service I get this error
my python code is this
import pyodbc
import pandas as pd
ConnectionString = "Driver={SQL Server};Server=XYZ;Database=ABCD;Trusted_Connection=yes;"
conn = pyodbc.connect(ConnectionString)
df_results = pd.read_sql("EXEC TestService" , conn)
and he is my windows service in c#
Log("In cmd", true);
try
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = PythonPath;
string Script = PythonSuggestedDiagnosesFile;
psi.Arguments = $"\"{Script}\"";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
string Errors = "";
string Results = "";
using (var process = Process.Start(psi))
{
Errors = process.StandardError.ReadToEnd();
Results = process.StandardOutput.ReadToEnd();
}
Log("In cmd : " + "Errors:\n" + Errors + "\n\nResults:\n" + Results);
}
catch (Exception ex)
{
Log("ERROR (cmd) : " + ex.ToString());
}
and the error I get is this
In cmd : Errors:
C:\Users\MyID\AppData\Local\Programs\Python\Python310\python.exe: can't find '__main__' module in ''
Results:
how to fix that?
You should pass proper WorkingDirectory to your ProcessStartInfo. Like this.

Not able to pass arguments with spaces to python script from c#

I am calling python script from c# using ProcessInfoStart method. As an argument it receives JSON and is input to python script.
It works fine it we pass JSON without having any spaces but if there is any space then original JSON is splitted till space and passes as argument and rest ignored
public static bool ExecutePythonScript(string jRequest, string fileType)
{
string pythonExePath = Convert.ToString(ConfigurationManager.AppSettings["PythonExe"]);
bool bIsExecutionSuccess = true;
try
{
var psi = new ProcessStartInfo();
psi.FileName = pythonExePath;
var script = #"C:Scripts\pdf-to-csv.py";
psi.Arguments = $"\"{script}\" \"{jRequest}\"";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
var errors = "";
var results = "";
using (var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
results = process.StandardOutput.ReadToEnd();
}
if (!string.IsNullOrEmpty(errors))
bIsExecutionSuccess = false;
}
catch(Exception ex)
{
bIsExecutionSuccess = false;
}
return bIsExecutionSuccess;
}
Python script to accept arguments
input_params = sys.argv[1]
input_params = input_params.replace("'",'"')
data_params = json.loads(input_params)
Is there a way i can pass jRequest with spaces to python script.
Python script parameters can be wrapped in single quotes in order to read the whole string including spaces.
Try wrapping the JSON string in single quotes.

ShellExecuteEx and getexitcodeprocess

Using ShellExecuteEx(..) to lunch a python script and python script returning a value from python main using sys.exit(0) on success or some other error value. How to read a python script exit code?
After launching application waited to complete script by using MsgWaitForMultipleObjects (...) and then calling GetExitCodeProcess(...) some reason I always read value 1 from getExitCodeprocess(..)
Python Code:
def main():
time.sleep(10)
logger.info("************The End**********")
return (15)
if __name__ == "__main__":
sys.exit(main())
C++ Code:
SHELLEXECUTEINFO rSEI = { 0 };
rSEI.cbSize = sizeof(rSEI);
//rSEI.lpVerb = "runas";
rSEI.lpVerb = "open";
rSEI.lpFile = "python.Exe";
rSEI.lpParameters = LPCSTR(path.c_str());
rSEI.nShow = SW_NORMAL;
rSEI.fMask = SEE_MASK_NOCLOSEPROCESS;
if (ShellExecuteEx(&rSEI)) // you should check for an error here
;
else
errorMessageID = GetLastError(); //MessageBox("Error", "Status", 0);
WORD nStatus;
MSG msg; // else process some messages while waiting...
while (TRUE)
{
nStatus = MsgWaitForMultipleObjects(1, &rSEI.hProcess, FALSE, INFINITE, QS_ALLINPUT); // drop through on user activity
if (nStatus == WAIT_OBJECT_0)
{ // done: the program has ended
break;
}
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
DispatchMessage(&msg);
//MessageBox("Wait...", "Status", 0);
}
} // launched process has exited
DWORD dwCode=0;
if (!GetExitCodeProcess(rSEI.hProcess, &dwCode)) //errorvalue
{
DWORD lastError = GetLastError();
}
In this code as Python script exiting with 15, I am expecting to read 15 from dwCode from GetExitCodeProcess(rSEI.hProcess, &dwCode)?
Appreciates all of your help on this...
As the comments metioned, your python script fails.
Python Code Sample:
import sys
import time
import logging
import logging.handlers
logger = logging.getLogger("logger")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
logger.addHandler(handler)
def main():
time.sleep(10)
logger.info("************The End**********")
return (15)
if __name__ == "__main__":
sys.exit(main())
Name your Python script file with .py as the suffix instead of .Exe, such as "python.py"
Give path value to SHELLEXECUTEINFO.lpDirectory, and set SHELLEXECUTEINFO.lpParameters to NULL here.
Or Give the path and file combination to SHELLEXECUTEINFO.lpVerb, like "Path\\python.py"
C++ Code Sample:
#include <windows.h>
#include <iostream>
#include <string>
void main()
{
int errorMessageID = 0;
std::string path = "Path";
SHELLEXECUTEINFO rSEI = { 0 };
rSEI.cbSize = sizeof(rSEI);
//rSEI.lpVerb = "runas";
rSEI.lpVerb = "open";
rSEI.lpFile = "python.py";
rSEI.lpParameters = NULL;
rSEI.lpDirectory = path.c_str();
rSEI.nShow = SW_NORMAL;
rSEI.fMask = SEE_MASK_NOCLOSEPROCESS;
if (!ShellExecuteEx(&rSEI)) // you should check for an error here
errorMessageID = GetLastError(); //MessageBox("Error", "Status", 0);
WORD nStatus;
MSG msg; // else process some messages while waiting...
while (TRUE)
{
nStatus = MsgWaitForMultipleObjects(1, &rSEI.hProcess, FALSE, INFINITE, QS_ALLINPUT); // drop through on user activity
if (nStatus == WAIT_OBJECT_0)
{ // done: the program has ended
break;
}
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
DispatchMessage(&msg);
//MessageBox("Wait...", "Status", 0);
}
} // launched process has exited
DWORD dwCode = 0;
if (!GetExitCodeProcess(rSEI.hProcess, &dwCode)) //errorvalue
{
DWORD lastError = GetLastError();
}
}

fgetc causes a segfault after running the second time

I have an application that tries to read a specific key file and this can happen multiple times during the program's lifespan. Here is the function for reading the file:
__status
_read_key_file(const char * file, char ** buffer)
{
FILE * pFile = NULL;
long fsize = 0;
pFile = fopen(file, "rb");
if (pFile == NULL) {
_set_error("Could not open file: ", 1);
return _ERROR;
}
// Get the filesize
while(fgetc(pFile) != EOF) {
++fsize;
}
*buffer = (char *) malloc(sizeof(char) * (fsize + 1));
// Read the file and write it to the buffer
rewind(pFile);
size_t result = fread(*buffer, sizeof(char), fsize, pFile);
if (result != fsize) {
_set_error("Reading error", 0);
fclose(pFile);
return _ERROR;
}
fclose(pFile);
pFile = NULL;
return _OK;
}
Now the problem is that for a single open/read/close it works just fine, except when I run the function the second time - it will always segfault at this line: while(fgetc(pFile) != EOF)
Tracing with gdb, it shows that the segfault occurs deeper within the fgetc function itself.
I am a bit lost, but obviously am doing something wrong, since if I try to tell the size with fseek/ftell, I always get a 0.
Some context:
Language: C
System: Linux (Ubuntu 16 64bit)
Please ignore functions
and names with underscores as they are defined somewhere else in the
code.
Program is designed to run as a dynamic library to load in Python via ctypes
EDIT
Right, it seems there's more than meets the eye. Jean-François Fabre spawned an idea that I tested and it worked, however I am still confused to why.
Some additional context:
Suppose there's a function in C that looks something like this:
_status
init(_conn_params cp) {
_status status = _NONE;
if (!cp.pkey_data) {
_set_error("No data, open the file", 0);
if(!cp.pkey_file) {
_set_error("No public key set", 0);
return _ERROR;
}
status = _read_key_file(cp.pkey_file, &cp.pkey_data);
if (status != _OK) return status;
}
/* SOME ADDITIONAL WORK AND CHECKING DONE HERE */
return status;
}
Now in Python (using 3.5 for testing), we generate those conn_params and then call the init function:
from ctypes import *
libCtest = CDLL('./lib/lib.so')
class _conn_params(Structure):
_fields_ = [
# Some params
('pkey_file', c_char_p),
('pkey_data', c_char_p),
# Some additonal params
]
#################### PART START #################
cp = _conn_params()
cp.pkey_file = "public_key.pem".encode('utf-8')
status = libCtest.init(cp)
status = libCtest.init(cp) # Will cause a segfault
##################### PART END ###################
# However if we do
#################### PART START #################
cp = _conn_params()
cp.pkey_file = "public_key.pem".encode('utf-8')
status = libCtest.init(cp)
# And then
cp = _conn_params()
cp.pkey_file = "public_key.pem".encode('utf-8')
status = libCtest.init(cp)
##################### PART END ###################
The second PART START / PART END will not cause the segfault in this context.
Would anyone know a reason to why?

Categories