前言
Pycharm是Python的集成IDLE工具。但是,由于Pycharm的版本一直在更新,而Python的版本也在更新。往往更新后需要重新配置环境,尤其配置Python的使用版本。
1 问题的引出:
搭建Pyqt5的Python项目的时候,明明Pyqt5的安装已经顺利了:
H:\work\project\python\pythonCom>pip install pyqt5 pyqt5-tools
Requirement already satisfied: pyqt5 in d:\programs\python\python39\lib\site-packages (5.15.4)
Requirement already satisfied: pyqt5-tools in d:\programs\python\python39\lib\site-packages (5.15.4.3.2)
Requirement already satisfied: PyQt5-Qt5>=5.15 in d:\programs\python\python39\lib\site-packages (from pyqt5) (5.15.2)
Requirement already satisfied: PyQt5-sip<13,>=12.8 in d:\programs\python\python39\lib\site-packages (from pyqt5) (12.9.0)
Requirement already satisfied: pyqt5-plugins<5.15.4.3,>=5.15.4.2.2 in d:\programs\python\python39\lib\site-packages (from pyqt5-tools) (5.15.4.2.2)
Requirement already satisfied: click in d:\programs\python\python39\lib\site-packages (from pyqt5-tools) (7.1.2)
Requirement already satisfied: python-dotenv in d:\programs\python\python39\lib\site-packages (from pyqt5-tools) (0.18.0)
Requirement already satisfied: qt5-tools<5.15.2.2,>=5.15.2.1.2 in d:\programs\python\python39\lib\site-packages (from pyqt5-plugins<5.15.4.3,>=5.15.4.2.2->pyqt5-tools) (5.15.2.1.2)
Requirement already satisfied: qt5-applications<5.15.2.3,>=5.15.2.2.2 in d:\programs\python\python39\lib\site-packages (from qt5-tools<5.15.2.2,>=5.15.2.1.2->pyqt5-plugins<5.15.4.3,>=5.15.4.2.2->pyqt5-tools) (5.15.2.2.2)
WARNING: You are using pip version 21.1.2; however, version 21.1.3 is available.
You should consider upgrading via the 'd:\programs\python\python39\python.exe -m pip install --upgrade pip' command.
可是,当运行标准验证程序的时候:
import sys
from PyQt5 import QtWidgets, QtCore
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
widget.resize(400, 200)
widget.setWindowTitle("This is PyQt Widget example")
widget.show()
exit(app.exec_())
始终报错:
"D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python.exe" H:/work/project/python/pythonCom/PyQtTest.py
Traceback (most recent call last):
File "H:/work/project/python/pythonCom/PyQtTest.py", line 2, in <module>
from PyQt5 import QtWidgets, QtCore
ModuleNotFoundError: No module named 'PyQt5'
Process finished with exit code 1
总是报错找不到PyQt5
2 报错找不到PyQt5的原因
网上给出了各种原因分析:
2.1 模组安装不到位,不匹配
(7条消息) 解决 No module named 'PyQt5.QtWebEngineWidgets'_jenas999的博客-CSDN博客
2.2 老项目下,虚拟环境配置没有加入默认安装的PyQt5的路径
(7条消息) PyQt5安装及ModuleNotFoundError: No module named 'PyQt5'问题解决_SunnyFish-ty的博客-CSDN博客
上面这个例子又引申了各种配置问题,例如,Venv的配置
venv — Creation of virtual environments — Python 3.9.6 documentation
【小结】以上原因都不是我的原因。
我的原因我解释一下,因为我之前的项目是3.7的python的配置,而我后来安装pycharm的时候,是3.9的python,于是,这两个编译环境似乎在IDLE中没有好好管理。
我在pycharm用pip安装 pyQt5的时候,全部默认安装到3.9的lib目录下面了,而之前python的项目没有用pyQt,于是,编译器又找不到,但是,提示已经装好了,如此,循环。
用:
file -> settings
我们看到我的项目python编译器版本是3.7配置
然后,在Package下面根本没有安装好了的pyQt5
3 正题来了:配置Pycharm的Python版本
用:
file -> settings
注意点击【‘show all’】
然后点击 + 号
这里输入: “PyQT"
找到你要修改的python版本,这里是3.9的版本
选让所有的项目都符合这个配置
然后,之前在python3.7下没有的已经安装的模组都出现了
再运行一次pyQt5的测试程序,结果可以运行了。
PyQT6 的版本例子:
参考: