0

I'm trying to embed a bit of python code in a C++ application, as to be able to plot some calculation results easily using matplotlib. Unfortunately, trying to import the matplotlib.pyplot module using

PyObject* pyplotName = PyUnicode_FromString( "matplotlib.pyplot" );
PyObject* matplotlib = PyImport_Import( pyplotName );

I get the following error message

Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\matplotlib\pyplot.py", line 98, in <module
>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()

  File "C:\Python33\lib\site-packages\matplotlib\backends\__init__.py", line 28,
 in pylab_setup
    globals(),locals(),[backend_name],0)
  File "C:\Python33\lib\site-packages\matplotlib\backends\backend_tkagg.py", lin
e 8, in <module>
    import tkinter as Tk, tkinter.filedialog
  File "C:\Python33\Lib\tkinter\__init__.py", line 36, in <module>
    from tkinter import _fix
  File "C:\Python33\Lib\tkinter\_fix.py", line 65, in <module>
    import _tkinter
UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: inval
id character

Apparently the error is coming from the tkinter module. Just to be safe, I tried to import the tkinter module explicitly and, as expected, got the same error message. However, the problem only occurs if I use the C API, as I'm able to use matplotlib.pyplot in IDLE without any issues.

I'd really like to be able to use Python for the plots, so I'm hoping someone can help me resolve the issue.

Just in case it's relevant: Both the Python installation and the C++ program are x64, running on Windows (as seen from the paths in the error message).

Edit: I've managed to compile and run the program on Linux without a problem, so the problem seems to be Windows specific.

4

0 回答 0