Pages

Wednesday, July 27, 2016

Issue when import matplotlib in Virtualenv in Python

Error:
import matplotlib.pyplot as plt

Traceback (most recent call last):

  File "", line 1, in

  File "/Users/jwithanawasam/MachineLearning/Caffe2/venv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 114, in

    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()

  File "/Users/jwithanawasam/MachineLearning/Caffe2/venv/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup

    globals(),locals(),[backend_name],0)

  File "/Users/jwithanawasam/MachineLearning/Caffe2/venv/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 24, in

    from matplotlib.backends import _macosx

RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ


If you encounter the above error, there is an easier way to fix that without using Matplotlib FAQ ;)

Soution:

If you have already installed Matplotlib install using the following command.
pip install matplotlib

cd ~/.matplotlib
vi matplotlibrc

Add the following content in opened matplotlibrc file.

backend: TkAgg

Try import matplotlib.pyplot as plt and it should work without errors.

Error: Segmentation fault: 11 in Caffe (PyCaffe)

Error: Segmentation fault: 11

If you get the above error during import caffe in Python, check if the following path in [Caffe installation directory]/ makefile.config points to system Python instead of Homebrew version of Python.

PYTHON_INCLUDE and PYTHON_LIB

PYTHON_INCLUDE := /usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/include/python2.7

PYTHON_LIB := /usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/

Then append Python directory in Caffe installation directory to PYTHONPATH as given below.

export PYTHONPATH=[Caffe installation directory]/python:$PYTHONPATH

Error: Fatal Python error: PyThreadState_Get: no current thread in Caffe (PyCaffe)

import caffe

Following error is encountered (and Python crashes) when executing the above import statement in Python. (PyCaffe)

Fatal Python error: PyThreadState_Get: no current thread

Usually, the error is due to the conflicts between different Python versions installed in the machine. If you tried the following step (re install), as mentioned in many other posts and still get the error, then try the trouble shooting steps that I have mentioned in this post. They worked for me :)

brew uninstall boost-python
brew install --build-from-source --fresh -vd boost-python

Caffe is primarily written in C++ and PyCaffe is it's Python interface. PyCaffe uses Boost Python
which is a C++ library to enable interoperability between C++ and the Python.

During the Caffe installation, we need to ensure that Boost Python is linked against Homebrew version of Python and not System Python. You can check the Python error report to check if there are any references to Python system libraries.

Use the following commands (in terminal) to check that.

otool -L [Caffe installation directory]/python/caffe/_caffe.so
otool -L /usr/local/opt/boost-python/lib/libboost_python.dylib

Note: You can replace the above libboost_python.dylib path with /usr/local/Cellar/boost-python/1.57.0/lib/libboost_python.dylib as well. (Additional info: Homebrew in /usr/local/Cellar/ - every formula is also linked to a /usr/local/opt directory. It provides a path for a formula's contents that does not change across version upgrades.)

otool is a command line tool that is being used to find dependencies of an executable. ‘-L’ option searches for the shared libraries used.

As a result of the above command, you may see a reference to Python system libraries as given below.
/System/Library/Frameworks/Python.framework/Versions/2.7/Python

Then you need to change the location to Homebrew version of Python using "install_name_tool" as given below.

install_name_tool [-change old new] input

E.g.,
sudo install_name_tool -change /System/Library/Frameworks/Python.framework/Versions/2.7/Python /usr/local/Frameworks/Python.framework/Versions/2.7/Python /usr/local/opt/boost-python/lib/libboost_python.dylib


After this, try import caffe, it should work as expected.