Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python rcParams error

I have installed both Anaconda and MacPorts to install various python packages and receive the following error when I try to import matplotlib:

bash-3.2$ python
Python 2.7.8 |Anaconda 2.1.0 (x86_64)| (default, Aug 21 2014, 15:21:46) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import conda
>>> import matplotlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/colinross/anaconda/lib/python2.7/site-packages/matplotlib-1.4.3-py2.7-macosx-10.5-x86_64.egg/matplotlib/__init__.py", line 169, in <module>
    from urllib2 import urlopen
  File "/Users/colinross/anaconda/lib/python2.7/urllib2.py", line 104, in <module>
    import bisect
  File "bisect.py", line 2, in <module>
    import matplotlib.pyplot as plt
  File "/Users/colinross/anaconda/lib/python2.7/site-packages/matplotlib-1.4.3-py2.7-macosx-10.5-x86_64.egg/matplotlib/pyplot.py", line 27, in <module>
    import matplotlib.colorbar
  File "/Users/colinross/anaconda/lib/python2.7/site-packages/matplotlib-1.4.3-py2.7-macosx-10.5-x86_64.egg/matplotlib/colorbar.py", line 32, in <module>
    import matplotlib.artist as martist
  File "/Users/colinross/anaconda/lib/python2.7/site-packages/matplotlib-1.4.3-py2.7-macosx-10.5-x86_64.egg/matplotlib/artist.py", line 11, in <module>
    from matplotlib import docstring, rcParams
ImportError: cannot import name rcParams

Does anyone know how to fix this?

like image 546
user3711780 Avatar asked Sep 01 '15 17:09

user3711780


1 Answers

The problem is caused by a file bisect.py in your current directory, which shadows the bisect module from the standard library: when you import matplotlib, matplotlib imports the urllib2 library, which in turn tries to import the standard library bisect module. Unfortunately, because of the way that Python's import name resolution works, urllib2 ends up importing your local bisect.py instead of the standard library one.

Note that at this point (the point where your bisect.py starts to be imported) we're still only halfway though the matplotlib import, since the matplotlib import can't be completed until it's finished importing urllib2, which in turn can't be completed until the bisect.py import is complete. That means that only some of the objects that the matplotlib import defines have been defined.

Now your bisect.py script apparently tries to make some more imports from matplotlib, and because the matplotlib namespace hasn't been fully populated yet you end with an ImportError.

Possible solutions: (1) rename your bisect.py script to something else; (2) start the Python interpreter from a different directory (one that doesn't contain your bisect.py script).

In general, the recommendation is to avoid giving your own modules or top-level packages names that match something in the standard library. Rather than having to have encyclopaedic knowledge of what's in the standard library, the easiest way to achieve this is to avoid names that are too generic. numbers.py, math.py and random.py are probably the most common offenders.

For diagnosing this sort of error in the future, note the line:

File "bisect.py", line 2, in <module>

in the traceback: all other lines show a module within /Users/colinross/anaconda/lib/python2.7, so are coming from the standard library or from a third-party package (like matplotlib) installed in /Users/colinross/anaconda/lib/python2.7/site-packages; the lack of path shows that bisect.py is being imported from the current directory.

like image 132
Mark Dickinson Avatar answered Oct 28 '22 13:10

Mark Dickinson