Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: Calling a python script which imports matplotlib in C++ project shows ImportError #29291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hongyifei opened this issue Dec 12, 2024 · 1 comment

Comments

@hongyifei
Copy link

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

test.cu:
int main(){
    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();
}

void pltDrawFigure(const std::vector<double> & xx, const std::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;
    }
    auto size = xx.size();
    PyObject* pylist_x = PyList_New(size);
    PyObject* pylist_y = PyList_New(size);
    
    for (size_t i = 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(VERSION 3.23)
project(LightSim_Eva_Zhu)
set(CMAKE_CUDA_STANDARD 17)
find_package(CUDA REQUIRED)
include_directories("/home/wbt/openlibs/eigen-3.4.0/include/eigen3")
find_package(OpenCV REQUIRED PATHS /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)
#method2
set(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_Zhu test.cu #otherscripts)
target_link_libraries( LightSim_Eva_Zhu ${OpenCV_LIBS} ${Python3_LIBRARY})

eva.py:
import matplotlib.pyplot as plt

def drawFigure(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

@QuLogic
Copy link
Member

QuLogic commented Dec 13, 2024

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.

@QuLogic QuLogic closed this as not planned Won't fix, can't repro, duplicate, stale Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants