Wizard Notes

Python, JavaScript を使った音楽信号分析の技術録、作曲活動に関する雑記

PyQtGraph エラー対処(ImportError: cannot import name 'sip', Exception: pyqtgraph requires Qt version >= 5.12)

f:id:Kurene:20210606001252p:plain
PyQtGraph example

Python をver. 3.8にアップデートして PyQtGraphをインストール後,PyQtGraphを使ったスクリプトを実行した際に以下の2つのエラーが発生しました.

  • ImportError: cannot import name 'sip' from 'PyQt5'
  • Exception: pyqtgraph requires Qt version >= 5.12 (your version is 5.9.7)

この2つのエラー対処後,PyQtGraphを使ったスクリプトが正常起動したので,その方法をメモとして残します.

バージョン情報

  • Python==3.8.8
  • pyqtgraph==0.12.1
  • pyside2==5.15.2
  • pyside6==6.1.1
  • pyqt==5.9.2
  • pyqt5-qt5==5.15.2
  • pyqt5-sip==12.9.0

ImportError: cannot import name 'sip' from 'PyQt5'

■エラーメッセージ

  File "C:\ProgramData\Anaconda3\lib\site-packages\pyqtgraph\Qt.py", line 137, in <module>
    from PyQt5 import QtGui, QtCore, QtWidgets, sip, uic
ImportError: cannot import name 'sip' from 'PyQt5' (C:\ProgramData\Anaconda3\lib\site-packages\PyQt5\__init__.py)

pyqtgraphsipがないとのこと.

なお,sipPythonC++インターフェイスコードの生成モジュールです.

これは以下のようにpyqtgraph\Qt.pyを書き換えるとエラー解消できました.

■編集前

    from PyQt5 import QtGui, QtCore, QtWidgets, uic

■編集後

    from PyQt5 import QtGui, QtCore, QtWidgets, uic
    import sip

Exception: pyqtgraph requires Qt version >= 5.12

■エラーメッセージ

  File "C:\ProgramData\Anaconda3\lib\site-packages\pyqtgraph\Qt.py", line 392, in <module>
    raise Exception('pyqtgraph requires Qt version >= %d.%d  (your version is %s)' % (versionReq[0], versionReq[1], QtVersion))
Exception: pyqtgraph requires Qt version >= 5.12  (your version is 5.9.7)

Qt のバージョンが低いとのこと.

該当するコードは以下になります.

pyqtgraph==0.11.1 pyqtgraph/Qt.py

## Make sure we have Qt >= 4.7
versionReq = [4, 7]
m = re.match(r'(\d+)\.(\d+).*', QtVersion)

pyqtgraph==0.12.0 pyqtgraph/Qt.py

## Make sure we have Qt >= 5.12
versionReq = [5, 12]
m = re.match(r'(\d+)\.(\d+).*', QtVersion)

pyqtgraph==0.12.0, 0.12.1では qtのバージョンは5.12が要求されています.

PyQt5関係のアップデートなどを試してみましたが,それは上手くいきませんでした.

上手くいった方法は以下の2つ

コメントアウト

以下のようにpyqtgraph\Qt.pyのバージョンチェック部をコメントアウトするとエラー解消はできます.

私の環境でのパスは,"C:\ProgramData\Anaconda3\lib\site-packages\pyqtgraph\Qt.py"でした.

■編集前

## Make sure we have Qt >= 5.12
versionReq = [5, 12]
m = re.match(r'(\d+)\.(\d+).*', QtVersion)
if m is not None and list(map(int, m.groups())) < versionReq:
   print(list(map(int, m.groups())))
    raise Exception('pyqtgraph requires Qt version >= %d.%d  (your version is %s)' % (versionReq[0], versionReq[1], QtVersion))

■編集後

## Make sure we have Qt >= 5.12
versionReq = [5, 12]
m = re.match(r'(\d+)\.(\d+).*', QtVersion)
#if m is not None and list(map(int, m.groups())) < versionReq:
#   print(list(map(int, m.groups())))
#    raise Exception('pyqtgraph requires Qt version >= %d.%d  (your version is %s)' % (versionReq[0], versionReq[1], QtVersion))

現状,自作のPyQtGraphアプリは問題なく動いていますが,Qt==5.10以上が要求されるようなPyQtGraphの機能を使うと問題が発生すると考えられます.

PySide2 / PySide6を使う

PyQy5を諦め,PySide2/ PySide6を利用する方法です. PyQtGraphをimportするスクリプトの方を書き換えます.

■編集前

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg

■編集後

import os
import PySide2
from PySide2 import QtGui, QtCore
dirname = os.path.dirname(PySide2.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path

import pyqtgraph as pg

環境変数 QT_QPA_PLATFORM_PLUGIN_PATH が設定されていないと,以下のようなエラーが起こるので,事前に設定しておきます.

qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: direct2d, minimal, offscreen, windows.

PySideで `qt.qpa.plugin: Could not find the Qt platform ...` のエラーが出てくるときの対処