hi,

i have a python module which i compile to an EXE (using Py2EXE).

this EXE will be called from another program which was created in C++.

my Q is, how can i return an error status from my PY exe to the calling program. is there anyway i can alert the calling to an error?

thanx for your interest!

Dani AI

Generated

As already pointed out, the usual way is to have the Python process terminate with a numeric status so the caller can read it. Two practical additions that may help here.

Many IDEs (IDLE, PyScripter, etc.) run code inside a host interpreter or shell and deliberately catch the SystemExit exception so the IDE itself does not quit. That is why calling the same exit routine behaves differently inside PyScripter versus when you run the compiled EXE from cmd.exe. If you need a guaranteed immediate termination (no exception handling by a host), use the low-level terminate call that bypasses Python cleanup (see os._exit in the Python docs) or run the EXE from an external console. See Python docs for details: sys.exit and SystemExit behavior and os._exit.

From the C++ side on Windows, spawn the EXE and query its exit code. The common, robust pattern is CreateProcess + WaitForSingleObject + GetExitCodeProcess. Example:

#include <windows.h>
#include <stdio.h>

STARTUPINFOA si = { sizeof(si) };
PROCESS_INFORMATION pi;
if (!CreateProcessA(NULL, "C:\\path\\to\\pyexe.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
    printf("CreateProcess failed: %lu\n", GetLastError());
    return 1;
}
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD code = 0;
GetExitCodeProcess(pi.hProcess, &code);
printf("child exit code = %lu\n", code);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

If you prefer simpler calls, system() also returns the child's code (on POSIX you must decode it with WEXITSTATUS). When debugging, run the EXE from a plain console and check %ERRORLEVEL% (Windows) to verify what the compiled program actually returns. See GetExitCodeProcess docs for details: GetExitCodeProcess.

Recommended Answers

All 5 Replies

If the python program quits with an error the return code is 1. If it successfully runs the return code is 0.

You can alternately specify your own return codes using sys.exit(return_code_value) at specific exit points of your code.

ah, thanx for the info. i came across sys.exit() but didnt know then what it did.

i'll try this out.

hi, this works. :)

just out of curiosity, thru my PyScripter, sys.exit() does not actually exit the codes. it will continue on. for compiled EXE it will exit whne sys.exit() is executed.

wld you know the reason to this?

I'm sure it has something to do with your PyScripter.

Try running the code from the console. It could just be that PyScripter dumps you into a shell after your program has completed... But I've never used that IDE, so I'm not sure.

yeah, pretty sure its PyScripter.

thanx for your help. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.