Results 1 to 3 of 3

Thread: unable to import pyside2 "DLL load failed while importing shiboken2 "?

  1. #1
    Join Date
    Feb 2025
    Posts
    4
    Thanks
    1

    Question unable to import pyside2 "DLL load failed while importing shiboken2 "?

    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?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,301
    Thanks
    312
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: unable to import pyside2 "DLL load failed while importing shiboken2 "?

    The runtime environment in Visual Studio is usually not the same as your normal command line / desktop Windows environment. Make sure your PATH includes the location of this DLL before starting Visual Studio. Also, in your Project configuration (Project -> Properties -> Debugging), make sure the Environment PATH entry there also include the DLL directory.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    eue (14th February 2025)

  4. #3
    Join Date
    Feb 2025
    Posts
    4
    Thanks
    1

    Default Re: unable to import pyside2 "DLL load failed while importing shiboken2 "?

    yes, that was it, thank you.

Similar Threads

  1. cmake error with Failed to find "glu32" in ""
    By kennethadammiller in forum Qt Programming
    Replies: 2
    Last Post: 6th September 2024, 11:39
  2. Replies: 2
    Last Post: 10th May 2016, 09:06
  3. Failed to load platform plugin "windows"
    By Eos Pengwern in forum Installation and Deployment
    Replies: 1
    Last Post: 19th September 2013, 17:57
  4. Replies: 1
    Last Post: 7th January 2013, 00:29
  5. qt cross compilation error "unable to load interpreter"
    By cpalm in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 12th July 2011, 11:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.