Search code examples
pythonmatplotlibwxpythonwxwidgets

matplotlib.pyplot and wx importing problems using python 2.7.10


I have installed many libraries for a project, but two specific libraries are giving me trouble when trying to import to python. These two are matplotlib.pyplot and wxpython. I have already followed many tutorial to see how to properly install these packages into python but so far I have had no luck. Here is the error that get when trying to import:

Traceback (most recent call last):
  File "/Users/AhmedNiri/Ahmed/2D_Mapping_Program_V7.py", line 13, in <module>
    import matplotlib.pyplot as plt
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.9-x86_64.egg/matplotlib/pyplot.py", line 98, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.9-x86_64.egg/matplotlib/backends/__init__.py", line 28, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.9-x86_64.egg/matplotlib/backends/backend_wxagg.py", line 6, in <module>
    import backend_wx    # already uses wxversion.ensureMinimal('2.8')
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib-1.3.1-py2.7-macosx-10.9-x86_64.egg/matplotlib/backends/backend_wx.py", line 53, in <module>
    raise ImportError(missingwxversion)
ImportError: Matplotlib backend_wx and backend_wxagg require wxversion, which was not found. 

So how do I know that these two libraries have been installed properly and in the desired directory? I know that this might be an easy question to answer but I have been trying for a while to solve this and I also haven't had too much experience with python. Thanks in advance.


Solution

  • The problem was solved and the solution was as follow. After I installed wxpython and captured the output using "brew install wxpython> install.log 2>error.log", I figured out that when the wxpython is installed it does not have the proper directory and it also tells you exactly how to change this directory. The captured output looked like follow:

    ==> Installing wxpython dependency: wxmac
    ==> Downloading https://homebrew.bintray.com/bottles/wxmac-3.0.2.yosemite.bottle.10.tar.gz
    Already downloaded: /Library/Caches/Homebrew/wxmac-3.0.2.yosemite.bottle.10.tar.gz
    ==> Pouring wxmac-3.0.2.yosemite.bottle.10.tar.gz
    🍺  /usr/local/Cellar/wxmac/3.0.2: 777 files, 41M
    ==> Installing wxpython
    ==> Downloading https://homebrew.bintray.com/bottles/wxpython-3.0.2.0.yosemite.bottle.tar.gz
    Already downloaded: /Library/Caches/Homebrew/wxpython-3.0.2.0.yosemite.bottle.tar.gz
    ==> Pouring wxpython-3.0.2.0.yosemite.bottle.tar.gz
        ==> Caveats
    
    Python modules have been installed and Homebrew's site-packages is not
    in your Python sys.path, so you will not be able to import the modules
    this formula installed. If you plan to develop with these modules,
    please run:
      mkdir -p /Users/AhmedNiri/Library/Python/2.7/lib/python/site-packages
      echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/AhmedNiri/Library/Python/2.7/lib/python/site-packages/homebrew.pth
    ==> Summary
    🍺  /usr/local/Cellar/wxpython/3.0.2.0: 944 files, 38M
    

    So if you look at the above execution you can see:

       Python modules have been installed and Homebrew's site-packages is not
        in your Python sys.path, so you will not be able to import the modules
        this formula installed. If you plan to develop with these modules,
        please run:
          mkdir -p /Users/AhmedNiri/Library/Python/2.7/lib/python/site-packages
          echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/AhmedNiri/Library/Python/2.7/lib/python/site-packages/homebrew.pth
    

    This was the part that was used to solve the problem. I hope that this can help someone else and I would like to thank @JoranBeasley for his help.