my python script failed when script tries to import PySide2 with the CPython function I wrote. It gives the following error:

Qt Code:
  1. Error while importing pyside2:
  2. Traceback (most recent call last):
  3. File "C:/Users/Administrator/Desktop/python-mini-projects-master/projects/Ascii_art/make_art_test.py", line 48, in <module>
  4. import PySide2
  5. File "E:\VS_ProJectS\msdiBIMPlatform-0.3\ThirdParty\tool\python\lib\site-packages\PySide2\__init__.py", line 80, in <module>
  6. _setupQtDirectories()
  7. File "E:\VS_ProJectS\msdiBIMPlatform-0.3\ThirdParty\tool\python\lib\site-packages\PySide2\__init__.py", line 31, in _setupQtDirectories
  8. import shiboken2
  9. File "E:\VS_ProJectS\msdiBIMPlatform-0.3\ThirdParty\tool\python\lib\site-packages\shiboken2\__init__.py", line 27, in <module>
  10. from .shiboken2 import *
  11. ImportError: DLL load failed while importing shiboken2: unable to find module
To copy to clipboard, switch view to plain text mode 

Here is my CPython function I use to run my script

Qt Code:
  1. void RunPythonScript(std::string python_module_or_script, std::string working_path)
  2. {
  3. if (!StartInterpreter())
  4. {
  5. _interpreterStatus = ShutDown;
  6. throw Base::RuntimeError("Initialization Failed");
  7. }
  8. _interpreterStatus = Running;
  9.  
  10. // Set the locale to UTF-8 (necessary for mbstowcs to handle UTF-8 correctly)
  11. std::setlocale(LC_ALL, "en_US.UTF-8");
  12.  
  13. // Allocate buffer for wide string conversion
  14. size_t len = std::mbstowcs(nullptr, python_module_or_script.c_str(), 0) + 1; // Get the length of the wide string
  15. wchar_t* wide_str = new wchar_t[len]; // Allocate memory for wide string
  16.  
  17. std::mbstowcs(wide_str, python_module_or_script.c_str(), len); // Convert char* to wchar_t*
  18.  
  19. FILE* fp = _Py_wfopen(wide_str, L"r");
  20. if (fp != nullptr) {
  21. PyRun_SimpleString("import sys");
  22. //PyRun_SimpleString("sys.setdefaultencoding('utf-8')");
  23. PyRun_SimpleString(msdiBIMPlatformBaseModule().c_str());
  24.  
  25. PyObject* pModule;
  26. pModule = PyImport_AddModule("__main__");
  27. if (pModule == NULL)
  28. {
  29. cout << "__main__ ??" << endl;
  30. PyErr_Print();
  31. return;
  32. }
  33. //pDict = PyModule_GetDict(pModule);
  34. //AdditionalmsdiBIMPlatformModule(pModule);
  35. std::set<std::string> namesBefore = GetObjectsName();
  36. int res = PyRun_SimpleFile(fp, python_module_or_script.c_str());
  37. std::set<std::string> namesAfter = GetObjectsName();
  38. SetCreatePos(namesAfter, namesBefore, python_module_or_script);
  39.  
  40. if (res != 0) {
  41. std::cout << "Run Script Failed" << res << std::endl;
  42. PyErr_Print();
  43. }
  44. std::fclose(fp);
  45. }
  46. else {
  47. std::cerr << "Failed to open file: " << python_module_or_script << std::endl;
  48. }
  49.  
  50. EndInterpreter();
  51. }
To copy to clipboard, switch view to plain text mode 


Here is my python script, which is a simple attempt to import PySide2
Qt Code:
  1. try:
  2. import PySide2 # Or any other module you are trying to load
  3. except Exception as e:
  4. print("Error while importing pyside2:")
  5. traceback.print_exc() # This will print the traceback of the error
To copy to clipboard, switch view to plain text mode 

I am working under environment:
1. python 3.8.20
2. Qt 5.15.2
3. Pyside 5.15.8 and shiboken2 that comes along with pyside2 installation

I could import PySide2 just fine within python interpreter, no problem with a stand alone simple CPython program with SAME MSVC project configuration to run script.
BUT fails when running the current CPython program I have been working on for long time. Unable to find the reason for this.
Is there anyway to check? Maybe I overlooked any hidden MSVC configuration? Or maybe there is something code missing that blocks the correct search for dlls and correct import?