Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bz2 mistakenly made a hard requirement of matplotlib 2.2.2 #10866

jmd-dk opened this Issue Mar 22, 2018 · 4 comments


None yet
4 participants
Copy link

jmd-dk commented Mar 22, 2018

Bug report

Bug summary

The _bz2 module is made a dependency in matplotlib 2.2.2. At least up until matplotlib 2.1.1, this was not the case. I believe this is a mistake. Previously, the import of bz2 (in matplotlib/cbook/ was made only when needed, inside the to_filehandle() function. In 2.2.2, this import is moved up to at the top of the file, meaning that it always try to import bz2 and by extension _bz2, regardless of whether it will be used or not.

This is a problem as _bz2 is not always installed, and not listed as a required dependency of matplotlib. I am able to fix the issue easily by moving the import of bz2 back down inside of the to_filehandle() function, or alternatively placing it inside a try/except block.

Actual outcome

>>> import matplotlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jeppe/Desktop/Desktop/concept_update_test/python/lib/python3.6/site-packages/matplotlib/", line 127, in <module>
    from . import cbook
  File "/home/jeppe/Desktop/Desktop/concept_update_test/python/lib/python3.6/site-packages/matplotlib/cbook/", line 13, in <module>
    import bz2
  File "/home/jeppe/Desktop/Desktop/concept_update_test/python/lib/python3.6/", line 23, in <module>
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2

Matplotlib version

  • Operating system: Linux Mint 17.3
  • Matplotlib version: 2.2.2
  • Python version: 3.6

I have installed both Python and matplotlib from source.

@tacaswell tacaswell added this to the v2.2.3 milestone Mar 23, 2018


This comment has been minimized.

Copy link

tacaswell commented Mar 23, 2018

Fair enough. Optional parts of the standard library are surprising to me.

This change came in via 703dbdb in #10231 for 2.2.0 as what looks like just style cleanup (moving imports to the top).

tacaswell added a commit to tacaswell/matplotlib that referenced this issue Mar 23, 2018


This comment has been minimized.

Copy link

Ravi2712 commented Jun 6, 2018

I am getting this error. I have added matplotlib in requirements.txt and I am installing via pip. I am running my project in python virtualenv.

If I am running in system, without activating virtualenv. It's working.

Matplotlib version:

  • Operating system: Ubuntu 16.04
  • Matplotlib version: 2.2.2
  • Python version: 3.6

Error log:



This comment has been minimized.

Copy link

ImportanceOfBeingErnest commented Jun 6, 2018

@Ravi2712 You have as of now the following options:

  • Install bz2.
  • Revert back to matplotlib 2.1.2, where this error would not occur.
  • Use the workaround proposed in the OP, i.e. edit the matplotlib/cbook/ to put the line import bz2 back to its original place. This is what is done in the fix as well:
  • Install current development version of matplotlib where this is fixed.
  • Wait till July or so when the next release happens that fixes this.

This comment has been minimized.

Copy link

Ravi2712 commented Jun 7, 2018

@ImportanceOfBeingErnest : Installed current development version in virtualenv. It's working. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.