my python script failed when script tries to import PySide2 with the CPython function I wrote. It gives the following error:
Error while importing pyside2:
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/python-mini-projects-master/projects/Ascii_art/make_art_test.py", line 48, in <module>
import PySide2
File "E:\VS_ProJectS\msdiBIMPlatform-0.3\ThirdParty\tool\python\lib\site-packages\PySide2\__init__.py", line 80, in <module>
_setupQtDirectories()
File "E:\VS_ProJectS\msdiBIMPlatform-0.3\ThirdParty\tool\python\lib\site-packages\PySide2\__init__.py", line 31, in _setupQtDirectories
import shiboken2
File "E:\VS_ProJectS\msdiBIMPlatform-0.3\ThirdParty\tool\python\lib\site-packages\shiboken2\__init__.py", line 27, in <module>
from .shiboken2 import *
ImportError: DLL load failed while importing shiboken2: unable to find module
Error while importing pyside2:
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/python-mini-projects-master/projects/Ascii_art/make_art_test.py", line 48, in <module>
import PySide2
File "E:\VS_ProJectS\msdiBIMPlatform-0.3\ThirdParty\tool\python\lib\site-packages\PySide2\__init__.py", line 80, in <module>
_setupQtDirectories()
File "E:\VS_ProJectS\msdiBIMPlatform-0.3\ThirdParty\tool\python\lib\site-packages\PySide2\__init__.py", line 31, in _setupQtDirectories
import shiboken2
File "E:\VS_ProJectS\msdiBIMPlatform-0.3\ThirdParty\tool\python\lib\site-packages\shiboken2\__init__.py", line 27, in <module>
from .shiboken2 import *
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
void RunPythonScript(std::string python_module_or_script, std::string working_path)
{
if (!StartInterpreter())
{
_interpreterStatus = ShutDown;
throw Base::RuntimeError("Initialization Failed");
}
_interpreterStatus = Running;
// Set the locale to UTF-8 (necessary for mbstowcs to handle UTF-8 correctly)
std::setlocale(LC_ALL, "en_US.UTF-8");
// Allocate buffer for wide string conversion
size_t len = std::mbstowcs(nullptr, python_module_or_script.c_str(), 0) + 1; // Get the length of the wide string
wchar_t* wide_str = new wchar_t[len]; // Allocate memory for wide string
std::mbstowcs(wide_str, python_module_or_script.c_str(), len); // Convert char* to wchar_t*
FILE* fp = _Py_wfopen(wide_str, L"r");
if (fp != nullptr) {
PyRun_SimpleString("import sys");
//PyRun_SimpleString("sys.setdefaultencoding('utf-8')");
PyRun_SimpleString(msdiBIMPlatformBaseModule().c_str());
PyObject* pModule;
pModule = PyImport_AddModule("__main__");
if (pModule == NULL)
{
cout << "__main__ ??" << endl;
PyErr_Print();
return;
}
//pDict = PyModule_GetDict(pModule);
//AdditionalmsdiBIMPlatformModule(pModule);
std::set<std::string> namesBefore = GetObjectsName();
int res = PyRun_SimpleFile(fp, python_module_or_script.c_str());
std::set<std::string> namesAfter = GetObjectsName();
SetCreatePos(namesAfter, namesBefore, python_module_or_script);
if (res != 0) {
std::cout << "Run Script Failed" << res << std::endl;
PyErr_Print();
}
std::fclose(fp);
}
else {
std::cerr << "Failed to open file: " << python_module_or_script << std::endl;
}
EndInterpreter();
}
void RunPythonScript(std::string python_module_or_script, std::string working_path)
{
if (!StartInterpreter())
{
_interpreterStatus = ShutDown;
throw Base::RuntimeError("Initialization Failed");
}
_interpreterStatus = Running;
// Set the locale to UTF-8 (necessary for mbstowcs to handle UTF-8 correctly)
std::setlocale(LC_ALL, "en_US.UTF-8");
// Allocate buffer for wide string conversion
size_t len = std::mbstowcs(nullptr, python_module_or_script.c_str(), 0) + 1; // Get the length of the wide string
wchar_t* wide_str = new wchar_t[len]; // Allocate memory for wide string
std::mbstowcs(wide_str, python_module_or_script.c_str(), len); // Convert char* to wchar_t*
FILE* fp = _Py_wfopen(wide_str, L"r");
if (fp != nullptr) {
PyRun_SimpleString("import sys");
//PyRun_SimpleString("sys.setdefaultencoding('utf-8')");
PyRun_SimpleString(msdiBIMPlatformBaseModule().c_str());
PyObject* pModule;
pModule = PyImport_AddModule("__main__");
if (pModule == NULL)
{
cout << "__main__ ??" << endl;
PyErr_Print();
return;
}
//pDict = PyModule_GetDict(pModule);
//AdditionalmsdiBIMPlatformModule(pModule);
std::set<std::string> namesBefore = GetObjectsName();
int res = PyRun_SimpleFile(fp, python_module_or_script.c_str());
std::set<std::string> namesAfter = GetObjectsName();
SetCreatePos(namesAfter, namesBefore, python_module_or_script);
if (res != 0) {
std::cout << "Run Script Failed" << res << std::endl;
PyErr_Print();
}
std::fclose(fp);
}
else {
std::cerr << "Failed to open file: " << python_module_or_script << std::endl;
}
EndInterpreter();
}
To copy to clipboard, switch view to plain text mode
Here is my python script, which is a simple attempt to import PySide2
try:
import PySide2 # Or any other module you are trying to load
except Exception as e:
print("Error while importing pyside2:")
traceback.print_exc() # This will print the traceback of the error
try:
import PySide2 # Or any other module you are trying to load
except Exception as e:
print("Error while importing pyside2:")
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?
Bookmarks