-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
PR: Improve how Matplotlib backend is set in the IPython console #5758
Conversation
…lotlibrc setting it first
…ails for whatever reason Also simplify how we show errors caused by this.
I tested your pull request (* Spyder Version: 3.2.5.dev0 afeb581) and at startup of spyder I get the following message: Python 3.6.2 (default, Sep 1 2017, 12:03:48)
Type "copyright", "credits" or "license" for more information.
IPython 6.2.1 -- An enhanced Interactive Python.
NOTE: The following error appeared when setting your Matplotlib backend
Traceback (most recent call last):
File "/home/ich/Kram/spyder/spyder/utils/ipython/spyder_kernel.py", line 439, in _set_mpl_backend
get_ipython().run_line_magic('matplotlib', backend)
File "/usr/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2095, in run_line_magic
result = fn(*args,**kwargs)
File "<decorator-gen-107>", line 2, in matplotlib
File "/usr/lib/python3.6/site-packages/IPython/core/magic.py", line 187, in <lambda>
call = lambda f, *a, **k: f(*a, **k)
File "/usr/lib/python3.6/site-packages/IPython/core/magics/pylab.py", line 99, in matplotlib
gui, backend = self.shell.enable_matplotlib(args.gui)
File "/usr/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2983, in enable_matplotlib
self.enable_gui(gui)
File "/usr/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 482, in enable_gui
real_enable_gui(gui)
File "/usr/lib/python3.6/site-packages/ipykernel/eventloops.py", line 284, in enable_gui
raise RuntimeError("Cannot activate multiple GUI eventloops")
RuntimeError: Cannot activate multiple GUI eventloops After that error I can use the IPython console but the attempt to plot still results in an empty canvas that does not react on mouse clicks. |
Right, this means things are working as expected. I mean, at least we are able to catch that error and show it to users. This means you need to deactivate the
so that Matplotlib sets its backend by itself (i.e. without Spyder interference). That should fix your problem. Could you verify it? Thanks! |
If the Two comments on your pull request:
Python 3.6.2 (default, Sep 1 2017, 12:03:48)
Type "copyright", "credits" or "license" for more information.
IPython 6.2.1 -- An enhanced Interactive Python.
NOTE: Spyder *can't* set your selected Matplotlib backend because there is an external matplotlibrc file that's setting it first.
This file could be located at '/etc/matplotlibrc'.
Your backend will be TkAgg
|
Yeah, that's right. But after this PR we will be able to tell users that they need to deactivate our Graphics support, so that Matplotlib can do its job without us interfering on it. For that, I'll catch the
That's part of this PR and just informs users that Spyder can't set a new backend if there's one in
I think it's not exactly that, it's the fact that there's a default backend in Could you comment the |
Well there is a short and a long answer. Short answer
The short answer is: yes, Spyder can set the backend but you obviously have set the interactive backend manually if you call matplotlib outside of spyder. The long answer follows at the bottom. Given the scenario you write your plotting script in spyder and you commented your backend in Long answer
Well, I think we have a bit of a misunderstanding here as I meant some different behaviour of Spyder. I have two test cases: Test case AI commented the $ dnf list *matplotlib*
python-matplotlib-data.noarch 2.0.0-3.fc27 @fedora
python-matplotlib-data-fonts.noarch 2.0.0-3.fc27 @fedora
python2-matplotlib.x86_64 2.0.0-3.fc27 @fedora
python2-matplotlib-doc.x86_64 2.0.0-3.fc27 @fedora
python2-matplotlib-tk.x86_64 2.0.0-3.fc27 @fedora
python3-matplotlib.x86_64 2.0.0-3.fc27 @fedora
python3-matplotlib-gtk3.x86_64 2.0.0-3.fc27 @fedora
python3-matplotlib-tk.x86_64 2.0.0-3.fc27 @fedora Testing only the different installed backends gives the following results:
Test case BI commented the $dnf list *matplotlib*
python-matplotlib-data.noarch 2.0.0-3.fc27 @fedora
python-matplotlib-data-fonts.noarch 2.0.0-3.fc27 @fedora
python3-matplotlib.x86_64 2.0.0-3.fc27 @fedora
python3-matplotlib-gtk3.x86_64 2.0.0-3.fc27 @fedora
python3-matplotlib-qt4.x86_64 2.0.0-3.fc27 @fedora
python3-matplotlib-qt5.x86_64 2.0.0-3.fc27 @fedora
python3-matplotlib-tk.x86_64 2.0.0-3.fc27 @fedora Testing only the different installed backends gives the following results:
Python 3.6.2 (default, Sep 1 2017, 12:03:48)
Type "copyright", "credits" or "license" for more information.
IPython 6.2.1 -- An enhanced Interactive Python.
NOTE: The following error appeared when setting your Matplotlib backend
Traceback (most recent call last):
File "/home/ich/Kram/spyder/spyder/utils/ipython/spyder_kernel.py", line 439, in _set_mpl_backend
get_ipython().run_line_magic('matplotlib', backend)
File "/usr/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2095, in run_line_magic
result = fn(*args,**kwargs)
File "<decorator-gen-107>", line 2, in matplotlib
File "/usr/lib/python3.6/site-packages/IPython/core/magic.py", line 187, in <lambda>
call = lambda f, *a, **k: f(*a, **k)
File "/usr/lib/python3.6/site-packages/IPython/core/magics/pylab.py", line 99, in matplotlib
gui, backend = self.shell.enable_matplotlib(args.gui)
File "/usr/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2978, in enable_matplotlib
pt.activate_matplotlib(backend)
File "/usr/lib/python3.6/site-packages/IPython/core/pylabtools.py", line 307, in activate_matplotlib
import matplotlib.pyplot
File "/usr/lib64/python3.6/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/lib64/python3.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/usr/lib64/python3.6/site-packages/matplotlib/backends/backend_qt4agg.py", line 18, in <module>
from .backend_qt5agg import FigureCanvasQTAggBase as _FigureCanvasQTAggBase
File "/usr/lib64/python3.6/site-packages/matplotlib/backends/backend_qt5agg.py", line 16, in <module>
from .backend_qt5 import QtCore
File "/usr/lib64/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 26, in <module>
import matplotlib.backends.qt_editor.figureoptions as figureoptions
File "/usr/lib64/python3.6/site-packages/matplotlib/backends/qt_editor/figureoptions.py", line 20, in <module>
import matplotlib.backends.qt_editor.formlayout as formlayout
File "/usr/lib64/python3.6/site-packages/matplotlib/backends/qt_editor/formlayout.py", line 56, in <module>
from matplotlib.backends.qt_compat import QtGui, QtWidgets, QtCore
File "/usr/lib64/python3.6/site-packages/matplotlib/backends/qt_compat.py", line 127, in <module>
from PyQt5 import QtCore, QtGui, QtWidgets
RuntimeError: the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class
In [1]:
Python 3.6.2 (default, Sep 1 2017, 12:03:48)
Type "copyright", "credits" or "license" for more information.
IPython 6.2.1 -- An enhanced Interactive Python.
NOTE: The following error appeared when setting your Matplotlib backend
Traceback (most recent call last):
File "/home/ich/Kram/spyder/spyder/utils/ipython/spyder_kernel.py", line 439, in _set_mpl_backend
get_ipython().run_line_magic('matplotlib', backend)
File "/usr/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2095, in run_line_magic
result = fn(*args,**kwargs)
File "<decorator-gen-107>", line 2, in matplotlib
File "/usr/lib/python3.6/site-packages/IPython/core/magic.py", line 187, in <lambda>
call = lambda f, *a, **k: f(*a, **k)
File "/usr/lib/python3.6/site-packages/IPython/core/magics/pylab.py", line 99, in matplotlib
gui, backend = self.shell.enable_matplotlib(args.gui)
File "/usr/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2983, in enable_matplotlib
self.enable_gui(gui)
File "/usr/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 482, in enable_gui
real_enable_gui(gui)
File "/usr/lib/python3.6/site-packages/ipykernel/eventloops.py", line 284, in enable_gui
raise RuntimeError("Cannot activate multiple GUI eventloops")
RuntimeError: Cannot activate multiple GUI eventloops
In [1]:
In [1]: runfile('/home/ich/.config/spyder-py3/temp.py', wdir='/home/ich/.config/spyder-py3')
In [2]:
Python 3.6.2 (default, Sep 1 2017, 12:03:48)
Type "copyright", "credits" or "license" for more information.
IPython 6.2.1 -- An enhanced Interactive Python.
NOTE: The following error appeared when setting your Matplotlib backend
Traceback (most recent call last):
File "/home/ich/Kram/spyder/spyder/utils/ipython/spyder_kernel.py", line 439, in _set_mpl_backend
get_ipython().run_line_magic('matplotlib', backend)
File "/usr/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2095, in run_line_magic
result = fn(*args,**kwargs)
File "<decorator-gen-107>", line 2, in matplotlib
File "/usr/lib/python3.6/site-packages/IPython/core/magic.py", line 187, in <lambda>
call = lambda f, *a, **k: f(*a, **k)
File "/usr/lib/python3.6/site-packages/IPython/core/magics/pylab.py", line 99, in matplotlib
gui, backend = self.shell.enable_matplotlib(args.gui)
File "/usr/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2983, in enable_matplotlib
self.enable_gui(gui)
File "/usr/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 482, in enable_gui
real_enable_gui(gui)
File "/usr/lib/python3.6/site-packages/ipykernel/eventloops.py", line 284, in enable_gui
raise RuntimeError("Cannot activate multiple GUI eventloops")
RuntimeError: Cannot activate multiple GUI eventloops
In [1] So what I meant was that the behaviour of Gtk3 and Tkinter differ from test case A to test case B even though only the number of installed backends (and the included dependencies) is different in the two cases.
Regards |
I didn't fully look into this but it seem it might be useful here: |
@JakobJakobson, I think the problem is that you have both the PyQt4 and PyQt5 backends installed and you're using Spyder with PyQt4 (at least that's what you reported on issue #5718). What happens if you remove the PyQt4 backend and use Spyder with PyQt5 instead? |
Removing the PyQt4 backend and keeping the PyQt5 backend leaves me at the following situation:
Choosing either the
|
…ting an MPL backend - The problem is not with '/etc/matplotlibrc' setting a backend because that doesn't take effect until '%matplotlib foo' is run. - This simplifies the code a lot.
@JakobJakobson, please try your Test Case B again. I changed this PR's approach in my last commit to better inform users about this problem and what Matplotlib backend is going to be used in Spyder's consoles if one is already set. |
Recapping the Test Case B:
and I'm using the following Spyder configuration:
and the following plotting file
Using this configuration leads to the startup/plotting behaviour with the following backends:
|
@JakobJakobson, I'm going to merge this one because it's an improvement and I'm not seeing the errors you reported in Ubuntu 17.04, so I think it's an issue exclusive to Fedora (as you already pointed out). |
Fixes #5718
Fixes #5541