You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am calling a python script in a CUDA C++project, which imports matplotlib. I ensure that matplotlib has been installed correctly and the Python path has been configured in C++. When I run this Python script alone, everything works fine. But when I call this Python script in a C++program, I encounter the following problem:
ImportError:/ home/wbt/anaconda3/envs/piq/lib/python3.12/lib-dynload/pyexpat.cpython-312-x86_64-linux-gnu.so: undefined symbol: XML_SetReparseDeferralEnabled
The other third-party libraries I imported did not encounter any issues when called.
Code for reproduction
test.cu:
intmain(){
Py_Initialize();
std::vector<double>yy= {3, 5, 1, 4, 2};
std::vector<double>xx= {1,2,3,4,5};
pltDrawFigure(xx, yy);
Py_Finalize();
}
voidpltDrawFigure(conststd::vector<double>&xx, conststd::vector<double>&yy){
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('../python/')");
PyObject*pModule=PyImport_ImportModule("eva");
if (!pModule) {
printf("import python failed1!!\n");
return;
}
PyObject*pFunction=PyObject_GetAttrString(pModule, "drawFigure");
if (!pFunction) {
printf("get python function failed!!!\n");
return;
}
autosize=xx.size();
PyObject*pylist_x=PyList_New(size);
PyObject*pylist_y=PyList_New(size);
for (size_ti=0; i<size; ++i) {
PyObject*pyitemx=PyLong_FromLong(xx[i]);
PyObject*pyitemy=PyLong_FromLong(yy[i]);
if (PyList_SetItem(pylist_x, i, pyitemx) !=0) {
PyErr_Print();
std::cerr<<"Failed to set item in PyList"<<std::endl;
Py_DECREF(pyitemx);
Py_DECREF(pylist_x);
return;
}
if (PyList_SetItem(pylist_y, i, pyitemy) !=0) {
PyErr_Print();
std::cerr<<"Failed to set item in PyList"<<std::endl;
Py_DECREF(pyitemy);
Py_DECREF(pylist_y);
return;
}
}
PyObject*pArgs=PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, pylist_x);
PyTuple_SetItem(pArgs, 1, pylist_y);
PyObject_CallObject(pFunction, pArgs);
Py_DECREF(pFunction);
Py_DECREF(pModule);
}
CmakeLists.txt:
cmake_minimum_required(VERSION3.23)
project(LightSim_Eva_Zhu)
set(CMAKE_CUDA_STANDARD17)
find_package(CUDAREQUIRED)
include_directories("/home/wbt/openlibs/eigen-3.4.0/include/eigen3")
find_package(OpenCVREQUIREDPATHS/home/wbt/openlibs/opencv-4.5.5)
include_directories(${OpenCV_INCLUDE_DIRS})
#method1#include_directories(/home/wbt/anaconda3/envs/piq/include/python3.12)#LINK_DIRECTORIES(/home/wbt/anaconda3/envs/piq/lib) #LINK_LIBRARIES(python3.12)#method2set(Python3_EXECUTABLE"/home/wbt/anaconda3/envs/piq/bin/python")
set(Python3_INCLUDE_DIR"/home/wbt/anaconda3/envs/piq/include/python3.12")
set(Python3_LIBRARY"/home/wbt/anaconda3/envs/piq/lib/libpython3.12.so")
INCLUDE_DIRECTORIES(${Python3_INCLUDE_DIR})
cuda_add_executable(LightSim_Eva_Zhutest.cu#otherscripts)target_link_libraries( LightSim_Eva_Zhu ${OpenCV_LIBS} ${Python3_LIBRARY})
eva.py:
importmatplotlib.pyplotaspltdefdrawFigure(x, y):
print(x)
print(y)
plt.plot(x, y, marker='o')
plt.title("f-loss")
plt.xlabel("f")
plt.ylabel("loss")
plt.grid(True)
plt.savefig('../Output Pictures/fig.png')
Actual outcome
Traceback (most recent call last):
File "", line 1, in
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/site-packages/matplotlib/pyplot.py", line 55, in
import matplotlib.colorbar
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/site-packages/matplotlib/colorbar.py", line 19, in
from matplotlib import _api, cbook, collections, cm, colors, contour, ticker
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/site-packages/matplotlib/contour.py", line 15, in
from matplotlib.backend_bases import MouseButton
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/site-packages/matplotlib/backend_bases.py", line 49, in
from matplotlib import (
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/site-packages/matplotlib/text.py", line 16, in
from .font_manager import FontProperties
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/site-packages/matplotlib/font_manager.py", line 41, in
import plistlib
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/plistlib.py", line 70, in
from xml.parsers.expat import ParserCreate
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/xml/parsers/expat.py", line 4, in
from pyexpat import *
ImportError: /home/wbt/anaconda3/envs/piq/lib/python3.12/lib-dynload/pyexpat.cpython-312-x86_64-linux-gnu.so: undefined symbol: XML_SetReparseDeferralEnabled
[1, 2, 3, 4, 5]
[3, 5, 1, 4, 2]
Process finished with exit code 0
Expected outcome
draws a figure with x/y
Additional information
If I test eva.py in a Python virtual environment, eva.py can execute correctly. But If I call eva.py in C++ projects, it shows this import error.
If I did not import matplotlib in this script, it's all normal. I can use any other repositories in this virtual environment, so it doesn't seem to be an issue with my Python path settings in CmakeLists.txt.
Operating system
Ubuntu 20.04.5 LTS
Matplotlib Version
3.9.3
Matplotlib Backend
No response
Python version
3.12
Jupyter version
No response
Installation
pip
The text was updated successfully, but these errors were encountered:
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/plistlib.py", line 70, in
from xml.parsers.expat import ParserCreate
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/xml/parsers/expat.py", line 4, in
from pyexpat import *
ImportError: /home/wbt/anaconda3/envs/piq/lib/python3.12/lib-dynload/pyexpat.cpython-312-x86_64-linux-gnu.so: undefined symbol: XML_SetReparseDeferralEnabled
This failure isn't in Matplotlib; it's in the Python standard library, probably because your environment is inconsistent. See #28235, and python/cpython#115398.
Bug summary
I am calling a python script in a CUDA C++project, which imports matplotlib. I ensure that matplotlib has been installed correctly and the Python path has been configured in C++. When I run this Python script alone, everything works fine. But when I call this Python script in a C++program, I encounter the following problem:
ImportError:/ home/wbt/anaconda3/envs/piq/lib/python3.12/lib-dynload/pyexpat.cpython-312-x86_64-linux-gnu.so: undefined symbol: XML_SetReparseDeferralEnabled
The other third-party libraries I imported did not encounter any issues when called.
Code for reproduction
Actual outcome
Traceback (most recent call last):
File "", line 1, in
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/site-packages/matplotlib/pyplot.py", line 55, in
import matplotlib.colorbar
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/site-packages/matplotlib/colorbar.py", line 19, in
from matplotlib import _api, cbook, collections, cm, colors, contour, ticker
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/site-packages/matplotlib/contour.py", line 15, in
from matplotlib.backend_bases import MouseButton
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/site-packages/matplotlib/backend_bases.py", line 49, in
from matplotlib import (
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/site-packages/matplotlib/text.py", line 16, in
from .font_manager import FontProperties
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/site-packages/matplotlib/font_manager.py", line 41, in
import plistlib
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/plistlib.py", line 70, in
from xml.parsers.expat import ParserCreate
File "/home/wbt/anaconda3/envs/piq/lib/python3.12/xml/parsers/expat.py", line 4, in
from pyexpat import *
ImportError: /home/wbt/anaconda3/envs/piq/lib/python3.12/lib-dynload/pyexpat.cpython-312-x86_64-linux-gnu.so: undefined symbol: XML_SetReparseDeferralEnabled
[1, 2, 3, 4, 5]
[3, 5, 1, 4, 2]
Process finished with exit code 0
Expected outcome
draws a figure with x/y
Additional information
Operating system
Ubuntu 20.04.5 LTS
Matplotlib Version
3.9.3
Matplotlib Backend
No response
Python version
3.12
Jupyter version
No response
Installation
pip
The text was updated successfully, but these errors were encountered: