spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.27k stars 1.61k forks source link

Qt libraries (DLLs) always loaded in IPython console #13391

Open ocroquette opened 4 years ago

ocroquette commented 4 years ago

Issue Report Checklist

Problem Description

When working in an IPython console, the Qt libraries are loaded even when the matplotlib support is disabled.

What steps reproduce the problem?

  1. In the preferences, go to "IPython console" / "Graphics"
  2. Uncheck "Activate support"
  3. Restart Spyder (just to be sure)
  4. Run the following code in the IPython console:
import psutil, os
p = psutil.Process( os.getpid() )
for dll in p.memory_maps():
    if "Qt" in dll.path:
        print(dll.path)

What is the expected output? What do you see instead?

Expected output: nothing

Actual output:

...\python-3.8.2.amd64\Lib\site-packages\PyQt5\Qt\bin\Qt5Gui.dll
...\python-3.8.2.amd64\Lib\site-packages\PyQt5\Qt\bin\Qt5Widgets.dll
...\python-3.8.2.amd64\Lib\site-packages\PyQt5\Qt\bin\Qt5Core.dll
...\python-3.8.2.amd64\Lib\site-packages\PyQt5\QtWidgets.pyd
...\python-3.8.2.amd64\Lib\site-packages\PyQt5\QtGui.pyd
...\python-3.8.2.amd64\Lib\site-packages\PyQt5\QtCore.pyd
...\python-3.8.2.amd64\Lib\site-packages\PyQt5\sip.cp38-win_amd64.pyd

The problem is that I am working with a 3rd party component that uses also Qt, but the files used are not compatible with the ones provided on the Python side. Since Spyder already loaded them, the 3rd party component fails to work at all. I am force to start it in an external interpreter, but then I loose many features of the IDE (debugger, variable manager...).

Modifying the "Backend" setting (for instance, setting it to "Automatic") doesn't help.

Versions

I use Spyder 4.1.4 installed with pip.

4.0.1 is NOT impacted. If I install specifically this version, I can work with the 3rd party component. This is currently my workaround.

Dependencies

I cannot check at the moment, but I will if it helps

ccordoba12 commented 4 years ago

This happens because we are patching QApplication in order to allow people to run Qt apps multiple times in our consoles:

https://github.com/spyder-ide/spyder-kernels/blob/46e2c1b583f7cd8190795f8d189be58da7f6667e/spyder_kernels/customize/spydercustomize.py#L147-L183

The problem is that I am working with a 3rd party component that uses also Qt, but the files used are not compatible with the ones provided on the Python side

I really don't get this part. How is your component supposed to work if it doesn't import the PyQt libraries present in the Python environment you're using?

ccordoba12 commented 4 years ago

@impact27, I think @ocroquette expectations are correct in the sense that if "Activate support" is off, we should not patch QApplication on our side. Otherwise, use cases like his are not feasible in Spyder.

What do you think?

ocroquette commented 4 years ago

The problem is that I am working with a 3rd party component that uses also Qt, but the files used are not compatible with the ones provided on the Python side

I really don't get this part. How is your component supposed to work if it doesn't import the PyQt libraries present in the Python environment you're using?

Thanks for the quick feedback.

The 3rd party component is not using PyQt. Actually it doesn't have anything to do with Python. I am loading it with ctypes.

impact27 commented 4 years ago

@impact27, I think @ocroquette expectations are correct in the sense that if "Activate support" is off, we should not patch QApplication on our side. Otherwise, use cases like his are not feasible in Spyder.

What do you think?

Sounds reasonable, but this is not related to matplotlib support, so either we disable the patching entirely, or we add another option.

ccordoba12 commented 4 years ago

Ok, what name do you have in mind for another option? And where should we put it?

ocroquette commented 4 years ago

Is it possible to avoid an additional option and instead detecting at runtime automatically if the patch is necessary ? For instance, using an import hook like in sys.path_hooks (https://docs.python.org/3/reference/import.html)

ocroquette commented 3 years ago

Hi, since last year I was just modifying spydercustomize.py to make the patch (https://github.com/spyder-ide/spyder-kernels/blob/46e2c1b583f7cd8190795f8d189be58da7f6667e/spyder_kernels/customize/spydercustomize.py#L147-L183) optional and not active by default for our internal Python distribution. It worked fine, but while preparing a new version of the distribution, I realized that the PyQt modules were loaded again unconditionally.

Something else is loading 'matplotlib.backends.qt_editor', 'matplotlib.backends.qt_compat', 'matplotlib.backends.qt_editor._formlayout', 'matplotlib.backends.qt_editor.figureoptions', 'matplotlib.backends.qt_editor._formsubplottool', 'matplotlib.backends.backend_qt5', 'matplotlib.backends.backend_qt5agg'.

The problem is I don't know what is causing them to be loaded. Is there any way to find out?

It would be nice to find a solution. I am starting to think that Spyder is not an option anymore to develop PySide2 applications :(

dalthviz commented 3 years ago

Hi @ocroquette maybe the loading is related with the logic to detect if matplotlib is available and to initialize the backend value here?: https://github.com/spyder-ide/spyder-kernels/blob/46e2c1b583f7cd8190795f8d189be58da7f6667e/spyder_kernels/console/start.py#L146-L217

ocroquette commented 3 years ago

Thanks for the quick feedback. I doubt it, since this code looks very old. I actually downgraded Spyder to a 4.x version using PIP, and the Qt packages were still loaded. So I have the impression that this is caused by a dependency of Spyder, rather than Spyder itself. Still, I will have a closer look at start.py. Cheers, Olivier

ccordoba12 commented 3 years ago

I think I found the problem: Matplotlib added a mechanism to automatically set the "right" backend when imported, which in turn imports PyQt5. I'll try to fix that in the next version of spyder-kernels (see https://github.com/spyder-ide/spyder-kernels/pull/319).

ocroquette commented 3 years ago

Great, thank you for the feedback. Out of curiosity, do you happen to have a link to the change in matplotlib ?

ccordoba12 commented 3 years ago

No, I haven't, sorry.