Skip to content

Commit 8e69c65

Browse files
usiemsmrbean-bremen
authored andcommitted
Update __spec__ too (if existing) when setting the import path
1 parent 47ca84e commit 8e69c65

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/PythonQt.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,20 @@ void PythonQt::overwriteSysPath(const QStringList& paths)
16801680
void PythonQt::setModuleImportPath(PyObject* module, const QStringList& paths)
16811681
{
16821682
PyModule_AddObject(module, "__path__", PythonQtConv::QStringListToPyList(paths));
1683+
#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4)
1684+
// Since Python 3.4 a module has a __spec__ member of type ModuleSpec which
1685+
// mirrors some module-specific attributes, see
1686+
// https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec .
1687+
//
1688+
// Python 3.9 prints an import warning "__package__ != __spec__.parent" on relative imports
1689+
// if we don't set the submodule_search_locations on __spec__ (this indirectly changes
1690+
// the value of parent)
1691+
PyObject* spec = PyObject_GetAttrString(module, "__spec__");
1692+
if (spec) {
1693+
PythonQt::self()->addVariable(spec, "submodule_search_locations", paths);
1694+
Py_DECREF(spec);
1695+
}
1696+
#endif
16831697
}
16841698

16851699
void PythonQt::stdOutRedirectCB(const QString& str)

0 commit comments

Comments
 (0)