Issue with PyRun_SimpleFile in MFC Application Fails When Executing Python Script for Anomaly Detection

I have developed a Python script for anomaly detection using the anomalib library, which works flawlessly when executed directly in Python. Additionally, the script runs without any issues when executed from a console-based C++ application using the PyRun_SimpleFile function from the Python C API.

However, when I try to execute the same Python script from an MFC application using PyRun_SimpleFile, it fails. The script starts loading, but after some time, it abruptly stops while loading dependencies from the anomalib.engine.

Here are some details of the environment and steps I have taken:

Observations: The path to the Python script is correct, and the script begins executing. The issue specifically arises when the script is loading dependencies related to anomalib.engine. There is no error output; the application just stops abruptly.

What I’ve Tried: Verified that the script works fine in a standalone Python environment and from a console-based C++ application. Ensured that all paths and environment variables are correctly set within the MFC application. Checked for any memory management issues within the MFC application but couldn’t find anything specific.

I am unable to determine whether this issue is related to the anomalib library, the MFC framework, the Python C API, or possibly memory management within the MFC application.

Any insights or suggestions on how to diagnose or resolve this issue would be greatly appreciated.