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

qt_loaders.py uses the removed imp module (Python 3.12) #1531

Closed
ondrudav opened this issue Mar 4, 2024 · 6 comments · Fixed by #1569
Closed

qt_loaders.py uses the removed imp module (Python 3.12) #1531

ondrudav opened this issue Mar 4, 2024 · 6 comments · Fixed by #1569
Labels
bug Something isn't working

Comments

@ondrudav
Copy link

ondrudav commented Mar 4, 2024

Environment data

  • debugpy version: 1.8.1
  • OS and version: Windows 10 Enterprise 10.0.19045
  • Python version (& distribution if applicable, e.g. Anaconda): 3.12.1 and pip 24.0
  • Using VS Code or Visual Studio: VS Code

Actual behavior

Pydevd runs into an exception when enabling matplotlib interactive mode for Qt5 backend.

Failed to enable GUI event loop integration for 'qt'
Traceback (most recent call last):
  File "c:\Users\Z004N92M\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydev_ipython\matplotlibtools.py", line 30, in do_enable_gui    enable_gui(guiname)
  File "c:\Users\Z004N92M\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydev_ipython\inputhook.py", line 540, in enable_gui        
    return gui_hook(app)
           ^^^^^^^^^^^^^
  File "c:\Users\Z004N92M\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydev_ipython\inputhook.py", line 176, in enable_qt
    from pydev_ipython.qt_for_kernel import QT_API, QT_API_PYQT5
  File "c:\Users\Z004N92M\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydev_ipython\qt_for_kernel.py", line 116, in <module>      
    QtCore, QtGui, QtSvg, QT_API = load_qt(api_opts)
                                   ^^^^^^^^^^^^^^^^^
  File "c:\Users\Z004N92M\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydev_ipython\qt_loaders.py", line 276, in load_qt
    if not can_import(api):
           ^^^^^^^^^^^^^^^
  File "c:\Users\Z004N92M\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydev_ipython\qt_loaders.py", line 152, in can_import       
    if not has_binding(api):
           ^^^^^^^^^^^^^^^^
  File "c:\Users\Z004N92M\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydev_ipython\qt_loaders.py", line 115, in has_binding      
    import imp
ModuleNotFoundError: No module named 'imp'
Backend QtAgg is interactive backend. Turning interactive mode on.

Expected behavior

Interactive mode is turned on without printing a traceback.

Steps to reproduce:

  1. Install matplotlib 3.8.3, PyQt5 5.15.10 and setuptools 69.1.1
  2. Debug this script with a breakpoint on the print call:
import matplotlib
print()
@int19h int19h added the bug Something isn't working label Mar 4, 2024
@luyun1
Copy link

luyun1 commented Apr 11, 2024

Hello, following your instructions, I reinstalled matplotlib 3.8.3, PyQt5 5.15.10 and setuptools 69.1.1. However, I still made the above error. Here is my error message. Could you please provide me with some relevant guidance? I would really appreciate it.
OS: Windows 11
Python version: 3.12.2
IDE: Pycharm 2024.1
Snipaste_2024-04-11_18-08-24

Error: Failed to enable GUI event loop integration for 'qt' Traceback (most recent call last): import imp ModuleNotFoundError: No module named 'imp' Backend QtAgg is interactive backend. Turning interactive mode on.

@Jenia-Trullion
Copy link

To support python 3.12 you should change the code in:_vendored/pydevd/_pydevd_bundle/pydevd_utils.py
from
spec = spec_from_file_location('__main__', file, loader=loader) m = module_from_spec(spec) sys.modules['__main__'] = m return m
to
spec = importlib.util.spec_from_loader('__main__', loader=None) m = importlib.util.module_from_spec(spec) # Assign the new module to sys.modules['__main__'] sys.modules['__main__'] = m return m

@luyun1
Copy link

luyun1 commented Jul 22, 2024 via email

@hugs7
Copy link

hugs7 commented Sep 15, 2024

Until the PR gets merged I rewrote the has_binding function in qt_loaders.py as follows. Works as a drop in replacement if in a pinch

def has_binding(api):
    """Safely check for PyQt4 or PySide, without importing
       submodules

       Parameters
       ----------
       api : str [ 'pyqtv1' | 'pyqt' | 'pyside' | 'pyqtdefault']
            Which module to check for

       Returns
       -------
       True if the relevant module appears to be importable
    """
    module_name_mapping = {
        'pyqtv1': 'PyQt4',
        'pyqt': 'PyQt4',
        'pyside': 'PySide',
        'pyqtdefault': 'PyQt4',
        'pyqt5': 'PyQt5'
    }
    
    module_name = module_name_mapping.get(api)
    if module_name is None:
        return False
    
    import importlib.util

    try:
        mod_spec = importlib.util.find_spec(module_name)
        if mod_spec is None:
            return False

        mod = importlib.import_module(module_name)

        submodules = ['QtCore', 'QtGui', 'QtSvg']
        for submodule in submodules:
            submodule_name = f"{module_name}.{submodule}"
            submodule_spec = importlib.util.find_spec(submodule_name)
            if submodule_spec is None:
                return False

        if api == 'pyside':
            if hasattr(mod, '__version__'):
                return check_version(mod.__version__, '1.0.3')
            else:
                return False

        return True

    except Exception as e:
        print(f"An error occurred: {e}")
        return False

@luyun1
Copy link

luyun1 commented Sep 15, 2024 via email

@judej judej assigned debonte and unassigned debonte Oct 14, 2024
@luyun1
Copy link

luyun1 commented Oct 22, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants