前言
编译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版本没安装对的原因:
--- 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'
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安装:
python configure.py
make
sudo make install
安装完成后,可以发现没有再报以上错误。
QtCoremod.sip:23: syntax error
在安装完sip后报错:
--- 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)
后续再运行就不会报错了。