Python Forum
Python/C API - PythonQTError
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python/C API - PythonQTError
#1
Hello All,

I have some Python/C API code embedded within a C++ DLL on Windows. Here is a snippet of the code:

PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue, *pJunk, *pDir;
char python_home[] = "C:\\Users\\SPARC PSOCT\\anaconda3\\envs\\oct38";
char env_path[] = "C:\\Users\\SPARC PSOCT\\Documents\\GitHub\\oct-cbort\\oct";
char env_path2[] = "C:\\Users\\SPARC PSOCT\\Documents\\GitHub\\oct-cbort";
size_t strsize = 0;
int nReturn = 0;

Py_SetPythonHome(Py_DecodeLocale(python_home, &strsize));

// Initialize the Python Interpreter
Py_Initialize();

PyObject* sysPath = PySys_GetObject("path");
pDir = PyUnicode_FromString(env_path);
nReturn = PyList_Append(sysPath, pDir);
if (nReturn == -1)
{
PyErr_Print();
return(nReturn);
}
pDir = PyUnicode_FromString(env_path2);
nReturn = PyList_Append(sysPath, pDir);
if (nReturn == -1)
{
PyErr_Print();
return(nReturn);
}

PyObject* repr = PyObject_Repr(sysPath);
PyObject* str = PyUnicode_AsEncodedString(repr, "utf-8", "~E~");
const char *bytes = PyBytes_AS_STRING(str);

Py_XDECREF(repr);
Py_XDECREF(str);

// Build the name object
pName = PyUnicode_DecodeFSDefault("rt");

// Load the module object
pModule = PyImport_Import(pName);
if (pModule == NULL)
{
//Get error message
std::string errorstr;
PyObject* repr = PyObject_Repr(pvalue);
PyObject* str = PyUnicode_AsEncodedString(repr, "utf-8", "~E~");
const char *error = PyBytes_AS_STRING(str);
errorstr.assign(error);
return(-1);
}

The DLL is not using Qt at all and everything builds fine within VisualStudio.

When I try to call this DLL from a Qt/C++ application, the pModule = PyImport_Import(pName) call is failing with the following error message:

PythonQtError('No Qt bindings could be found.\n\nnapari requires either PyQt5 or PySide2 to be installed in the environment.\nTo install the default backend (currently PyQt5), run "pip install napari[all]"\nYou may also use "pip install napari[pyside2]" for Pyside2, or "pip install napari[pyqt5]" for PyQt5')

I was able to install pyqt5 and pyside2 as suggested. However it did not make a difference as the same error occurs after installing pyqt5 and pyside2.

First of all, why does the code in the DLL depend on PyQt5 to be installed in the environment when the DLL is not even using Qt?
Second of all, how can I resolve the issue happening when the DLL is called from the Qt/C++ application?

Thanks
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020