why can’t i import module in my python code?
What i want is that i want to execute python code in c++ code, and then this python code imports the package of the local virtual environment
here is my c++ code:
#include <Python.h>
#include <iostream>
int main() {
setenv("PYTHONPATH", "/home/timeplus/ssd/project/proton_sake/contrib/cpython-cmake/cpython/Lib:/home/timeplus/venvs/env2/lib/python3.10/site-packages", 1);
Py_Initialize();
const char * source = "import sys\n"
"sys.path.append('/home/timeplus/venvs/env2/lib/python3.10/site-packages')\n"
"print(sys.path)\n"
"from lxml import etree\n"
"print(etree.__version__)";
// PyRun_SimpleString(source);
PyObject * pySource = Py_CompileString(source, "", Py_file_input);
if (pySource == NULL) {
PyErr_Print();
return 1;
}
PyObject *result = PyEval_EvalCode(pySource, PyModule_GetDict(PyImport_AddModule("__main__")), NULL);
if (result == NULL) {
PyErr_Print();
return 1;
}
Py_XDECREF(result);
Py_Finalize();
return 0;
}
I am sure this virtual environment has this package.
here is the errro message:
['/home/timeplus/ssd/project/proton_sake/contrib/cpython-cmake/cpython/Lib', '/home/timeplus/venvs/env2/lib/python3.10/site-packages', '/usr/bin/python3/lib/python313.zip', '/usr/bin/python3/lib/python3.13', '/usr/bin/python3/lib/python3.13/lib-dynload', '/home/timeplus/venvs/env2/lib/python3.10/site-packages']
Traceback (most recent call last):
File "", line 4, in <module>
ImportError: cannot import name 'etree' from 'lxml' (/home/timeplus/venvs/env2/lib/python3.10/site-packages/lxml/__init__.py)
what should i do? If need more message pleage let me know.