Why matplotlib is not working on the VSCode


I wrote a basic plotting code and I run it on the VS code my somehow the program cannot run it

I deleted python itself and VS code and I downloaded them again, but the problem is not solved.

import matplotlib.pyplot as plt
x = [1,2,3]
y = [4,5,6]

Traceback (most recent call last):
File "c:/Users/xxx/Desktop/Cmpt Physics/dreams.py", line 1, in <module>
    import matplotlib.pyplot as plt
File "C:UsersxxxAppDataLocalProgramsPythonPython37libsite-packagesmatplotlib__init__.py", line 138, in <module>
    from . import cbook, rcsetup
File "C:UsersxxxAppDataLocalProgramsPythonPython37libsite-packagesmatplotlibrcsetup.py", line 24, in <module>
    from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
File "C:UsersxxxAppDataLocalProgramsPythonPython37libsite-packagesmatplotlibfontconfig_pattern.py", line 18, in <module>
    from pyparsing import (Literal, ZeroOrMore, Optional, Regex, StringEnd,
File "C:UsersxxxAppDataLocalProgramsPythonPython37libsite-packagespyparsing.py", line 104, in <module>       
    import copy
File "c:UsersxxxDesktopCmpt Physicscopy.py", line 5, in <module>
    from pylab import plot,show,grid
File "C:UsersxxxAppDataLocalProgramsPythonPython37libsite-packagespylab.py", line 1, in <module>
    from matplotlib.pylab import *
File "C:UsersxxxAppDataLocalProgramsPythonPython37libsite-packagesmatplotlibpylab.py", line 218, in <module>
    from matplotlib.dates import (
File "C:UsersxxxAppDataLocalProgramsPythonPython37libsite-packagesmatplotlibdates.py", line 156, in <module>
    from matplotlib import rcParams
ImportError: cannot import name 'rcParams' from 'matplotlib' (C:UsersxxxAppDataLocalProgramsPythonPython37libsite-packagesmatplotlib__init__.py)

Any idea why this happens ?

Asked By: seVenVo1d



It looks like you are running code from a directory that contains a module named copy which is shadowing the copy module from the stdlib (c:UsersxxxDesktopCmpt Physicscopy.py looks to be the culprit). Rename that file to something that isn’t the same name as something in the stdlib and it should fix the issue.

Typically shadowing the stdlib leads to odd results like this where it pulls in modules out of order or the wrong module and makes things break in an odd way.

Answered By: Brett Cannon

I just had this issue on Windows. My issue was due to some Python packages installed using pip while other installed using conda. I fixed it by uninstalling the pip packages and reinstalling everything with conda install.

Answered By: Custos Mortem
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.