Gtk-ERROR : GTK+ 2.x symbols detected. Using GTK+2.x and GTK+3 in the same process is not support

Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+3 in the same process is not support


程序员遇到这种环境问题的时候前列腺都不好, 记录一下, 方便以后回查~


报错:

报错有两种形式

形式1:

Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+3 in the same process is not support

形式2:

Gtk-Message: 18:20:57.303: Failed to load module "canberra-gtk-module"

原因

一般原因:
python 里 matplotlib 的 gtk使用版本(TK3AGG)与系统后端默认(一般是TKAGG)的gtk版本不一样,有时候就算你没用上import matplotlib包,opencv或者别的什么包也用到了, 一样报错, 其直接结果就是imshow没有图片。

本人原因:
我的情况是我写的rospy节点里面用到了opencv-python, ros节点在后面运行的时候, 其它用c++写的节点在使用cv::imshow()的时候运行报错程序而且程序还不崩只是没有图片。

报错的一般复现

下面脚本可以简单检查自己是否有这种版本冲突的问题:

import cv2
import matplotlib.plot as plt 
cv2.namedWindow("test")

解决方案

这里我使用这个方案, 因为别的像 重新编译opencv, 太头大了:
命令行输入:

quit()

会显示一个文件:

/etc/matplotlibrc

接着命令行输入,

sudo gedit /etc/matplotlibrc

会出现这么个文件

### MATPLOTLIBRC FORMAT

# This is a sample matplotlib configuration file - you can find a copy
# of it on your system in
# site-packages/matplotlib/mpl-data/matplotlibrc.  If you edit it
# there, please note that it will be overwritten in your next install.
# If you want to keep a permanent local copy that will not be
# overwritten, place it in the following location:
# unix/linux:
#     $HOME/.config/matplotlib/matplotlibrc or
#     $XDG_CONFIG_HOME/matplotlib/matplotlibrc (if $XDG_CONFIG_HOME is set)
# other platforms:
#     $HOME/.matplotlib/matplotlibrc
#
# See http://matplotlib.org/users/customizing.html#the-matplotlibrc-file for
# more details on the paths which are checked for the configuration file.
#
# This file is best viewed in a editor which supports python mode
# syntax highlighting. Blank lines, or lines starting with a comment
# symbol, are ignored, as are trailing comments.  Other lines must
# have the format
#    key : val # optional comment
#
# Colors: for the color values below, you can either use - a
# matplotlib color string, such as r, k, or b - an rgb tuple, such as
# (1.0, 0.5, 0.0) - a hex string, such as ff00ff - a scalar
# grayscale intensity such as 0.75 - a legal html color name, e.g., red,
# blue, darkslategrayGTKAgg GTKCairo GTK3Agg GTK3Cairo
# MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG

#### CONFIGURATION BEGINS HERE

# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
# MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
# Template.
# You can also deploy your own backend outside of matplotlib by
# referring to the module name (which must be in the PYTHONPATH) as
# 'module://my_backend'.
#
# If you omit this parameter, it will always default to "Agg", which is a
# non-interactive backend.
backend      : GTKAgg

# If you are using the Qt4Agg backend, you can choose here
# to use the PyQt4 bindings or the newer PySide bindings to
# the underlying Qt4 toolkit.
#backend.qt4 : PyQt4        # PyQt4 | PySide

# Note that this can be overridden by the environment variable
# QT_API used by Enthought Tool Suite (ETS); valid values are
# "pyqt" and "pyside".  The "pyqt" setting has the side effect of
# forcing the use of Version 2 API for QString and QVariant.

# The port to use for the web server in the WebAgg backend.
# webagg.port : 8888

把 GTKAgg改成GTKAgg GTKCairo GTK3Agg GTK3Cairo MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG中的一个, 这个GTKAgg应该是gtk+2.0, 可以改成GTK3Agg, 由于在jetson板子上问题的原因可能多种多样这里建议使用QT5Agg。因为python2 pip 好像已经不支持QT4了, 所以还没安装的话只能下载QT5。

x86架构下

pip install PyQt5

Arm架构下(jetson)

sudo apt-get install qt5-doc qt5-doc-html qtbase5-doc-html qtbase5-examples -y

保存以后问题的复现就没有了。

另外多提一句:
如果是在x86架构下(就是我们正常的电脑)其实可以降opencv-python的版本

pip install opencv-python==3.4.0

或许可行, 但是我仔细想想可能不够稳。

另外:

千万别神经刀删除GTK 比如 sudo apt-get purge gtk2.0-dev && sudo apt-get autoremove 那你的环境基本凉了, 因为你见到的很多基础的视觉库都是基于GTK这个基础的可视化包依赖来开发的,系统会删除使用GTK为依赖的所有库。

参考文献

非常非常感谢这几个链接
https://github.com/opencv/opencv/issues/6680
https://github.com/yosinski/deep-visualization-toolbox/issues/10
https://www.cnblogs.com/longwhite/p/10397754.html

正是同行的分享才可以让很多问题变得方便。

祝, 武运昌隆~

  • 14
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值