1

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.

12
  • open python and type import wxversion ... if its not there you should re-install wxPython using apt-get (try following these directions wiki.wxpython.org/InstallingOnUbuntuOrDebian) Commented Jul 20, 2015 at 18:12
  • Sorry I forgot to mention that I am using a Mac OS and I've already used import wxversion and downloaded wxpython again but no luck @JoranBeasley Commented Jul 20, 2015 at 18:26
  • so you can import wxversion in a normal python shell? this implies you have multiple python interpretters (maybe 2.7 and 3.4) and whatever you are using to run your script is using the python that you do not have wx installed in ... Im guessing your wx is installed on one interpretter and your matplotlib is using a different interpretter. Commented Jul 20, 2015 at 18:46
  • @JoranBeasley no can't import anything, sorry for being unclear. Commented Jul 20, 2015 at 19:02
  • 1
    Let us continue this discussion in chat. Commented Jul 20, 2015 at 19:46

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer! I was completely stuck and this solved my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.