title: Embedded python crashes upon import of matplotlib.pyplot
body: <p>I am messing about, trying to build something similar to IPython/Jupyter notebooks. I'm writing my application in QT5, so much of this is related to 'embedding' Python in a native application.</p>
  
  <p>I figured out how to embed python and how to allow it to execute scripts entered by the user. I would like to be able to use plotting libraries (such as matplotlib), and display their output in my application. (in fact, the thing I am  trying to do appears to be very similar to what is described in <a href="https://stackoverflow.com/questions/18678982/embedding-a-matplotlib-chart-into-qt-c-application">this question</a>).</p>
  
  <p>However, when I try to import the plotting library using <code>import matplotlib.pyplot</code>, my application segfaults (I tried debugging, but the crash is not in my code, so I can't get anything sensible out of it).</p>
  
  <p>The code I use to initialize the embedded Python, and to run arbitrary scripts is shown at the bottom of this question.</p>
  
  <p>I can import other libraries (such as <code>sys</code> or <code>numpy</code>) fine. I can import <code>matplotlib</code> fine. But when I try to import <code>matplotlib.pyplot</code>, it segfaults.</p>
  
  <p>Does anyone have any suggestions?</p>
  
  <p>EDIT:
  I have determined that the cause lies (for some reason) with me using QT. When I compile a simple C or C++ program that imports matplotlib, it does <em>not</em> segfault...</p>
  
  <p>My code:</p>
  
  <pre><code>#include "pythoninteractor.h"
  
  #include &lt;QString&gt;
  #include &lt;Python.h&gt;
  #include &lt;string&gt;
  #include &lt;QList&gt;
  
  PythonInteractor::PythonInteractor()
  { 
      this-&gt;pyOutput_redir =
  "import sys\n\
  class CatchOutErr:\n\
      def __init__(self):\n\
          self.value = ''\n\
      def write(self, txt):\n\
          self.value += txt\n\
  catchOutErr = CatchOutErr()\n\
  sys.stdout = catchOutErr\n\
  sys.stderr = catchOutErr\n\
  "; //this is python code to redirect stdouts/stderr
  
  
      QString paths[] = {"",
                         "/home/tcpie/anaconda3/lib/python35.zip",
                         "/home/tcpie/anaconda3/lib/python3.5",
                         "/home/tcpie/anaconda3/lib/python3.5/plat-linux",
                         "/home/tcpie/anaconda3/lib/python3.5/lib-dynload",
                         "/home/tcpie/anaconda3/lib/python3.5/site-packages",};
  
      Py_SetProgramName(L"qt-notepad-tut");
      Py_Initialize();
  
      PyObject *pModule = PyImport_AddModule("__main__"); //create main module
      PyRun_SimpleString(this-&gt;pyOutput_redir.toStdString().c_str()); //invoke code to redirect
  
      PyObject *sys_path;
      PyObject *path;
  
      sys_path = PySys_GetObject("path");
      if (sys_path == NULL)
          return;
  
      PySequence_DelSlice(sys_path, 0, PySequence_Length(sys_path));
      for (size_t i = 0; i &lt; sizeof(paths) / sizeof(QString); i++) {
          path = PyUnicode_FromString(paths[i].toStdString().c_str());
  
          if (path == NULL)
              continue;
  
          if (PyList_Append(sys_path, path) &lt; 0)
              continue;
      }
  }
  
  QString PythonInteractor::run_script(QString script)
  {
      QString ret = "";
      PyObject *pModule = PyImport_AddModule("__main__");
      PyRun_SimpleString(script.toStdString().c_str());
      PyErr_Print(); //make python print any errors
  
      PyObject *catcher = PyObject_GetAttrString(pModule,"catchOutErr"); //get our catchOutErr created above
  
      if (catcher == NULL) {
          Py_Finalize();
          return ret;
      }
  
      PyObject *output = PyObject_GetAttrString(catcher,"value"); //get the stdout and stderr from our catchOutErr object
  
      if (output == NULL) {
          return ret;
      }
  
      ret = QString(PyUnicode_AsUTF8(output));
      return ret;
  }
  </code></pre>