Monday, September 05, 2016

Another try at Matplotlib

Awhile back, I finally managed to install Matplotlib (and Mayavi) through general package management tools on the Mac. Through a combination of Homebrew and Pip and a lot of finagling, my take-3-or-so try at getting the Python graphing libraries to work actually came to fruition.

Some recent feedback on the tXtFL AI prompted me to revisit the modeling software, but alas, Matplotlib was no longer working on my system. When I tried to run my scripts, I got a "Fatal Python error: PyThreadState_Get: no current thread. Abort trap: 6" error. As far as I can tell, the error tracks back to a bug in the latest version of vtk included with Homebrew.

What was odd to me was that the Virtualenv technique I had used was supposed to isolate the dependency chain so that the packages would continue to work even if some of them got updated and broke compatibility with other packages. I realized, however, that the Homebrew package installations take place outside of the virtual environment, so at least the way that I've been using Virtualenv probably does not fully isolate the environment.

Not being able to run a modeler just because of a broken dependency is of course a real bummer, so I set out to find alternative solutions. I've been reluctant to try commercial offerings in hopes of sticking with generalized package management solutions such as Homebrew, but I came across an at least partially open source solution called Anaconda. The trouble with installed Matplotlib from scratch is the sheer number of dependencies (no less than "pycairo, PyQt4, PyQt5, PySide, wxPython, PyGTK, Tornado, or GhostScript" listed on the official installation page), which makes having a dedicated package manager via  Anaconda for these and other Python scientific computing dependencies make perfect sense.

Anaconda offers the option of a complete install ("Anaconda") vs a minimalist install ("Miniconda"), and I went for the Miniconda install to see if I could get by with it. After installing the 64-bit Windows version, I created a separate virtual environment inside it with the Matplotlib and Mayavi libraries as follows:

conda create --name pystat matplotlib mayavi

Apparently including all required packages at environment creation time is preferable. Anaconda pulled in all the other required dependencies and set up the virtual environment. All of these packages apparently are available for new virtual environments as well, which streamlines future downloading.

I'm used to using Cygwin for all my Bash-within-Windows computing, but Anaconda and Cygwin do not appear to always play nicely with each other. I tried at activate the virtual environment within Cygwin, but the detected python appeared to be from Cygwin rather than from Anaconda.

Instead, I dropped into an old-fashioned Windows command prompt and fired up Anaconda from there. A simple "activate pystat" command brought me into the correct environment. I had to learn though that "cd d:\src" does not automatically bring one into that folder, but apparently typing "d:" as a separate command first is required. Clearly I'm a Windows command prompt newbie.

From there, both matplotlib and mayavi worked out of the box. I didn't even need to use the custom launch script that I had used previously. The beauty of Anaconda it is cross-platform, so I next plan to see if it will work as seamlessly back on my Mac as it has on Windows. Or maybe by then, vtk with brewed python will be friend again.