23

I just set up the VS Code with all the Python extensions. Python version is 3.8.3 through Anaconda. The interpreter is venv. When I run this code

import matplotlib.pyplot as plt

An error shows -

Exception has occurred: AttributeError
module 'sip' has no attribute 'setapi'
  File "C:\test.py", line 145, in <module>
    import matplotlib.pyplot as plt

I've tried uninstalling and reinstalling matplotlib, but to no avail.

Egret
  • 421
  • 1
  • 3
  • 13

6 Answers6

28

This worked for me.

python -m pip install matplotlib==3.2
Nuno Lopes
  • 381
  • 2
  • 3
10

There seems to be an incompatibility issue using Matplotlib version 3.3 with IPython. For now, you can fix it by installing Matplotlib 3.2.

  • Thanks for the suggestion. According to another post, the Anaconda distribution does not include support for Python 3.2 https://stackoverflow.com/a/50108544/5560837 – Egret Oct 18 '20 at 04:28
  • 1
    I meant Matplotlib version 3.2 not Python 3.2. I edited the answer to clear that up. – Muhamad Sohaib Arif Oct 18 '20 at 14:24
  • for me matplotlib==3.3.4, downgrade the matplotlib to 3.2, it worked – Rushikesh Feb 03 '21 at 11:04
  • 1
    For reference, I also get this issue on VSCode, but when using Spyder in the same anaconda environment `%matplotlib qt5` works fine. So it's not a pure ipython issue, but something in the way ipython and VSCode work together. – alexandre iolov Mar 11 '21 at 10:27
8

I had the same problem. I was using PyCharm. Installing PyQt5 worked for me.

2

I also had the same issue, and none of the above answers worked for me. After some trial and error, it seems that pyqt and qt were causing my issues. I know you are using venv, but if you don't mind using Anaconda, the following environment should work for you:

conda create -n matplotlib python==3.8.3 matplotlib==3.3.4 pyqt==5.9.2 qt==5.9.7

I tested this solution on python==3.8.3 and python==3.7.10 using Windows 10.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Marktodisco
  • 146
  • 1
  • 3
1

PySide2 is the official Qt (v5) for Python package which is officially supported by the Qt Company.

Install this module when using matplotlib and Jupyter/IPython

pip install pyside2

There is also PySide6 which is the package for Qt (v6)

Don't ask me why pyside2 is for qt5...

crowie
  • 171
  • 5
1

I reinstalled matplotlib to the latest version and it came out fine.

pip uninstall matplotlib
pip install matplotlib

This upgrades the matplotlib version from 3.3.4 to 3.4.2.

*I'm using python 3.7.6, windows, in a virtual environment.

JamesAng
  • 344
  • 2
  • 9