@@ -237,23 +237,32 @@ bool PythonWorker::init() {
237237
238238#ifdef _WIN32
239239 if (using_bundled) {
240- // Prepend Python DLL directories to PATH so the embedded interpreter can
241- // find .pyd extension dependencies (e.g. _ctypes.pyd -> libffi-8.dll).
242- // We use PATH instead of SetDefaultDllDirectories/AddDllDirectory because
243- // those restrict DLL search process-wide, which breaks the build tree
244- // where DLLs are spread across conda, deps, and build directories.
240+ // Register DLL directories via AddDllDirectory BEFORE Python init.
241+ // We do NOT call SetDefaultDllDirectories here — Python 3.8+ calls
242+ // SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS) internally
243+ // during Py_Initialize(), which enables LOAD_LIBRARY_SEARCH_USER_DIRS.
244+ // Directories registered via AddDllDirectory before that point will then
245+ // be searched for implicit DLL dependencies of .pyd extensions.
245246 QString dlls_dir = python_home + " /DLLs" ;
246247 QString app_dir = QCoreApplication::applicationDirPath ();
247- QString current_path = qgetenv (" PATH" );
248- QString new_path = dlls_dir + " ;" + python_home + " ;" + app_dir;
248+ AddDllDirectory (dlls_dir.toStdWString ().c_str ());
249+ AddDllDirectory (python_home.toStdWString ().c_str ());
250+ AddDllDirectory (app_dir.toStdWString ().c_str ());
249251
250252 // In the build tree, PYTHONHOME points to conda's prefix. Conda's .pyd
251253 // files need DLLs from conda's Library/bin/ (e.g. libexpat.dll for pyexpat).
252254 QString conda_library_bin = python_home + " /Library/bin" ;
253255 if (QDir (conda_library_bin).exists ()) {
254- new_path += " ; " + conda_library_bin;
256+ AddDllDirectory ( conda_library_bin. toStdWString (). c_str ()) ;
255257 }
256258
259+ // Also prepend to PATH for any DLL loading that happens before Python's
260+ // SetDefaultDllDirectories takes effect.
261+ QString current_path = qgetenv (" PATH" );
262+ QString new_path = dlls_dir + " ;" + python_home + " ;" + app_dir;
263+ if (QDir (conda_library_bin).exists ()) {
264+ new_path += " ;" + conda_library_bin;
265+ }
257266 new_path += " ;" + current_path;
258267 qputenv (" PATH" , new_path.toUtf8 ());
259268
0 commit comments