How do I install Matplotlib as a Macintosh Yosemite User

强颜欢笑 提交于 2019-12-12 01:46:35

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/29003653/how-do-i-install-matplotlib-as-a-macintosh-yosemite-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!