[ROS2]解决PyQt5和sip的各种报错问题 stderr: qt_gui_cpp

前言

编译ros环境的时候遇到了qt_gui_cpp各种编译问题,但是鉴于网上解决方法基本没有,故记录下来帮助后来者。整篇文章总结下来就是一句话:PyQt5和sip安装过程或安装版本有问题,需要重新安装。

问题与解决方法

如果PyQt5你是正常安装的,就正常往下执行即可。

如果在解决过程中遇到pyqt5的各种安装问题,建议先把其卸载干净,确保/usr/lib/python3/dist-packages/anaconda3/envs/myenv/lib/python3.8/site-packages/下都没有PyQt5和PyQt5dist之类的相关文件,如果uninstall不了就直接rm -rf暴力删除。

1. Use shutil.which instead of find_executable

--- stderr: qt_gui_cpp                              
/home/wang/ros2_humble/install/python_qt_binding/share/python_qt_binding/cmake/sip_configure.py:20: 
DeprecationWarning: Use shutil.which instead of find_executable
  qmake_exe = 'qmake-qt5' if find_executable('qmake-qt5') else 'qmake'
/home/wang/ros2_humble/install/python_qt_binding/share/python_qt_binding/cmake/sip_configure.py:46: 
DeprecationWarning: Use shutil.which instead of find_executable

故名思意,找到sip_configure.py文件的第20和46行代码,将find_executable改成shutil.which,当然,记得要 import shutil 先

2. 安装PyQt5卡在preparing metadata

执行pip install PyQt5 --force后,卡在preparing metadata一步,故在参考overflow解决方案后,

改成以下命令安装:

pip install pyqt5 --config-settings --confirm-license= --verbose

ModuleNotFoundError: No module named 'PyQt5'

代码显示找不到PyQt5 module

--- stderr: qt_gui_cpp                              
Traceback (most recent call last):
  File "/home/wang/ros2_humble/install/python_qt_binding/share/python_qt_binding/cmake/sip_configure.py", line 8, in <module>
    import PyQt5
ModuleNotFoundError: No module named 'PyQt5'

但是pip install pyqt5的时候又显示已有PyQt5,但是还卸载不了PyQt5(陷入了奇怪的死循环):

Found existing installation: PyQt5

Not uninstalling PyQt5 at /usr/lib/python3/dist-packages, outside environment /usr

Can't uninstall 'PyQt5'. No files were found to uninstall.

此时要到/usr/lib/python3/dist-packages目录下,找到与PyQt5和PyQt5dist两个文件夹,进行rm -rf删除,便能将PyQt5残留的内容删除干净,从而重新安装PyQt5,即pip install pyqt5.

ModuleNotFoundError: No module named 'PyQt5.sip' 或 sip: Usage: sip [-h] [-V] [-a file]

以下报错均为sip没安装或者sip版本没安装对的原因:

c 复制代码
--- stderr: qt_gui_cpp                             
Traceback (most recent call last):
  File "/home/wang/ros2_humble/install/python_qt_binding/share/python_qt_binding/cmake/sip_configure.py",
   line 9, in <module>
    from PyQt5 import QtCore
ModuleNotFoundError: No module named 'PyQt5.sip'
c 复制代码
Starting >>> qt_gui_cpp
--- stderr: qt_gui_cpp                              
sip: Usage: sip [-h] [-V] [-a file] [-b file] [-B tag] [-c dir] [-d file] [-D] [-e] [-f] [-g] [-I dir] [-j #] [-k] 
[-m file] [-o] [-p module] [-P] [-r] [-s suffix] [-t tag] [-w] 
[-x feature] [-X id:file] [-y file] [-z file] [@file] [file]

解决方法:

进入sip安装网站:https://riverbankcomputing.com/software/pyqt/download

找到sip最新版本安装包,随便选一个下载,解压到你Linux任意目录下:

然后进入该文件夹后,依次执行以下命令进行sip安装:

c 复制代码
python configure.py
make
sudo make install

安装完成后,可以发现没有再报以上错误。

QtCoremod.sip:23: syntax error

在安装完sip后报错:

c 复制代码
--- stderr: qt_gui_cpp                              
sip: /home/wang/anaconda3/envs/myenv/lib/python3.8/
site-packages/PyQt5/bindings/QtCore/QtCoremod.sip:23: syntax error

参考 github issue 后得到解决方法:

打开你的环境目录下的文本: /anaconda3/envs/myenv/lib/python3.8/site-packages/PyQt5/bindings/QtCore/QtCoremod.sip 或者

/usr/lib/python3.10/site-packages/PyQt5/bindings/QtCore/QtCoremod.sip

对第23行进行修改: %Module(name=PyQt5.QtCore, call_super_init=True, default_VirtualErrorHandler=PyQt5, keyword_arguments="Optional", use_limited_api=True)

后续再运行就不会报错了。

相关推荐
斗-匕10 分钟前
《代码重构指南:提升代码质量的关键步骤》
开发语言
ZPC821017 分钟前
Pytorch详解-Pytorch核心模块
人工智能·pytorch·python·深度学习·机器学习
985小水博一枚呀20 分钟前
【深度学习基础模型】极限学习机(Extreme Learning Machines, ELM)详细理解并附实现代码。
人工智能·python·深度学习·极限学习机
985小水博一枚呀21 分钟前
【深度学习基础模型】液态状态机(Liquid State Machines, LSM)详细理解并附实现代码。
人工智能·python·rnn·深度学习
Mr_Xuhhh38 分钟前
数据结构阶段测试2的一点小补充
android·开发语言·汇编·数据结构·c++·算法
码农超哥同学1 小时前
Python知识点:如何使用KubeEdge与Python进行容器化边缘计算
开发语言·python·面试·编程·边缘计算
1登峰造极1 小时前
uniapp自定义导航,全端兼容
开发语言·javascript·uni-app
无敌の星仔1 小时前
一个月学会Java 第7天 字符串与键盘输入
java·开发语言·python
心易行者1 小时前
ChatGPT 与 CoT 思维链:如何重塑 AI 的逻辑大脑?
开发语言·python
GGBondlctrl1 小时前
【JavaEE初阶】多线程案列之定时器的使用和内部原码模拟
java·开发语言·定时器·timer的使用·定时器代码模拟