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

Fix IPython import issue #19597

Merged
merged 2 commits into from Mar 1, 2021
Merged

Conversation

philnagel
Copy link
Contributor

When blocking import of IPython by defining sys.modules["IPython"] = None, creating plots fails because backend_bases.FigureCanvasBase._fix_ipython_backend2gui attempts to check if IPython is already imported by looking for an entry in sys.modules. It then attempts to import IPython, which fails due to the import being blocked.

Code sample to reproduce the issue:

import sys
sys.modules['IPython'] = None

from matplotlib import pyplot
plt = pyplot.subplots(1,1)

which results in:

Traceback (most recent call last):
  File "<module1>", line 5, in <module>
  File "C:\Users\Phil\envs\mp_dev\lib\site-packages\matplotlib\cbook\deprecation.py", line 451, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Phil\envs\mp_dev\lib\site-packages\matplotlib\pyplot.py", line 1287, in subplots
    fig = figure(**fig_kw)
  File "C:\Users\Phil\envs\mp_dev\lib\site-packages\matplotlib\pyplot.py", line 687, in figure
    figManager = new_figure_manager(num, figsize=figsize,
  File "C:\Users\Phil\envs\mp_dev\lib\site-packages\matplotlib\pyplot.py", line 315, in new_figure_manager
    return _backend_mod.new_figure_manager(*args, **kwargs)
  File "C:\Users\Phil\envs\mp_dev\lib\site-packages\matplotlib\backend_bases.py", line 3493, in new_figure_manager
    fig = fig_cls(*args, **kwargs)
  File "C:\Users\Phil\envs\mp_dev\lib\site-packages\matplotlib\figure.py", line 341, in __init__
    FigureCanvasBase(self)  # Set self.canvas.
  File "C:\Users\Phil\envs\mp_dev\lib\site-packages\matplotlib\backend_bases.py", line 1686, in __init__
    self._fix_ipython_backend2gui()
  File "C:\Users\Phil\envs\mp_dev\lib\site-packages\matplotlib\backend_bases.py", line 1713, in _fix_ipython_backend2gui
    import IPython
ModuleNotFoundError: import of IPython halted; None in sys.modules

This PR addresses this issue by not only checking if IPython is present in sys.modules, but also if it is blocked from being imported.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@tacaswell
Copy link
Member

Well, I just learned something about the expected behavior of sys.modules (could not quickly find documentation of this, but found a bug report where CPython made sure this behavior worked as expected https://bugs.python.org/issue31642 which is good enough for me).

@tacaswell tacaswell added this to the v3.4.0 milestone Feb 28, 2021
@tacaswell
Copy link
Member

This suggests we should make a similar change in qt_compat.py where we try to sort out if any of the Qt frameworks are imported already.

@QuLogic QuLogic merged commit b0aab9f into matplotlib:master Mar 1, 2021
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Mar 1, 2021
@QuLogic
Copy link
Member

QuLogic commented Mar 1, 2021

Thanks @philnagel! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again.

QuLogic added a commit that referenced this pull request Mar 2, 2021
…597-on-v3.4.x

Backport PR #19597 on branch v3.4.x (Fix IPython import issue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants