Skip to content

Pyside2下opencv找不到xcb库(qt.qpa.plugin:Could not load the Qt platform plugin "xcb")

Dingfeng Shi edited this page Dec 5, 2020 · 2 revisions

不需要重新装pyqt,容易引起冲突
修改虚拟环境下的opencv的__init__.py文件

虚拟环境地址/lib/python3.6/site-packages/cv2/__init__.py

修改os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"]os.environ["QT_QPA_FONTDIR"]的地址至pyside2的地址:

import importlib
import os
import sys

from .cv2 import *
from .data import *

# wildcard import above does not import "private" variables like __version__
# this makes them available
globals().update(importlib.import_module("cv2.cv2").__dict__)

ci_and_not_headless = False

try:
    from .version import ci_build, headless

    ci_and_not_headless = ci_build and not headless
except:
    pass

#设置pyside2地址,eg: (虚拟环境地址/lib/python3.6/site-packages/PySide2/Qt/)
pyside_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../PySide2/Qt/')

# the Qt plugin is included currently only in the pre-built wheels
if (
        sys.platform == "darwin" or sys.platform.startswith("linux")
) and ci_and_not_headless:
    #修改这里
    os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.path.join(pyside_path, "plugins")

# Qt will throw warning on Linux if fonts are not found
if sys.platform.startswith("linux") and ci_and_not_headless:
    #修改这里
    os.environ["QT_QPA_FONTDIR"] = os.path.join(pyside_path, "fonts")
Clone this wiki locally