79202699

Date: 2024-11-19 08:53:29
Score: 1
Natty:
Report link

In my case, using Py_SetPythonHome() causes conflicts and I am not able to import the libraries from my virtual environment. What I did was to remove Py_SetPythonHome() and point the PYTHONPATH environment variable to the site-packages inside my virtual environment before calling Py_Initialize(). In short:

QString python_path = "PATH_TO_ENVIRONMENT/lib/python3.11/site-packages";
setenv("PYTHONPATH", python_path.toStdString().c_str(), 1);

Py_Initialize();
if (!Py_IsInitialized())
{
  qDebug() << "Failed to initialize Python interpreter.";
  return;
}
PyRun_SimpleString("import numpy"); // import modules from my venv
Py_Finalize();
Reasons:
  • Blacklisted phrase (1): I am not able to
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Azad Baykara