0

I am a Macintosh Yosemite user. When I try to import matplotlib I get the following error.

    import matplotlib
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    import matplotlib
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/__init__.py", line 180, in <module>
    from matplotlib.cbook import is_string_like
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/cbook.py", line 33, in <module>
    import numpy as np
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/__init__.py", line 170, in <module>
    from . import add_newdocs
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/core/__init__.py", line 6, in <module>
    from . import multiarray
ImportError: cannot import name 'multiarray'

When I use from pylab import axis that would work just fine. I do not know what is going on and I am just so lost. I have tried using homebrew, macports installs, dmg installs. I also have very little experience installing things through terminal so I just followed what other people said. Though, that is still not working.

Pranjal Mittal
  • 10,772
  • 18
  • 74
  • 99

2 Answers2

2

Mac OS X ships with python by default. And there is one provided by homebrew. I would recommend using homebrew python over the default python.

Here, I suspect your numpy installation has landed up in the site-packages directory managed by the non-homebrew pip package manager whereas matplotlib package is installed in a different site-packages directory. (But I am not sure). Nevertheless it has something to do with multiple python's / package managers being used. This may not be the best answer, but so far the only solution I can think of to fix your problems is to uninstall pip and also remove everything in any python site-packages directory you can find on your filesystem,. And install python via homebrew again, and then install all packages required using the pip (which gets installed automatically when you install python using homebrew)

Warning: Make sure you list out the package names and store the names somewhere before deleting them because you will have to install them again.

brew uninstall python
#(ATTN) Uninstall macports and don't use it with brew
#(ATTN) Delete the contents of all python site-packages directories
rm /usr/local/lib/python2.7/site-packages/*
brew install python
# Homebrew comes with its own pip installed
pip install <package1>, <package2> ...

Here is a related question from someone who faced a similar problem: Numpy build fails with cannot import multiarray

My advice: Don't use Homebrew and Macports or any other package manager together. They mess up with each other and I have faced the consequences in the past.I just use homebrew python now. For installing scientific python packages either Anaconda or Canopy (choose one) are really helpful, which can be installed on top of the homebrew python.

Community
  • 1
  • 1
Pranjal Mittal
  • 10,772
  • 18
  • 74
  • 99
  • "It is recommended to use homebrew python": I guess you meant "I recommend to…", right? In fact, MacPorts is a good alternative. – Eric O. Lebigot Mar 12 '15 at 08:14
  • @EOL: Right, I should mention that because it is a personal preference. Updated! – Pranjal Mittal Mar 12 '15 at 08:20
  • Thank you for your help! After much misery, I downloaded **Anaconda** and have been using the **Spyder** interface built in. That seems to have all the scientific libraries already installed including `numpy`, `matplotlib`, and like 190 more. However, I will follow your instruction in order to install packages through `homebrew` because I want to learn that. When I install **Python** through `homebrew` (which I have no idea what it is), am I forced to use it in a **terminal** window or will I still have an **IDLE**? Thank you for your help. – Clever Programmer Mar 12 '15 at 15:49
  • if i reinstall python by `brew install python`, will it be the same 2.7 version or will it get changed ? – arvindh Aug 11 '15 at 14:22
  • **Solution**: Installing Anaconda was the ultimate answer. It lets me create multiple virtual environments and takes care of all the packages without any headache for me! Hope this helps someone! **Download Here**: [Anaconda Install](http://continuum.io/downloads). – Clever Programmer Aug 21 '15 at 09:28
1

After having a lot of problems similar to the ones you describe, using sudo pip install -U matplotlib worked fine for me.

Sara
  • 11
  • 1