You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Curriculum_Design/install_pyqt5_safe.py

65 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
"""
安全安装PyQt5的脚本避免平台插件问题
"""
import subprocess
import sys
import os
def install_pyqt5_safely():
"""安全安装PyQt5的方法"""
print("正在安全安装PyQt5...")
# 1. 首先完全卸载现有的PyQt5
print("1. 卸载现有PyQt5...")
subprocess.run([sys.executable, "-m", "pip", "uninstall", "PyQt5", "PyQt5-Qt5", "PyQt5-sip", "-y"],
capture_output=True, text=True)
# 2. 清理可能残留的文件
print("2. 清理残留文件...")
venv_path = os.environ.get('VIRTUAL_ENV', '')
if venv_path:
site_packages = os.path.join(venv_path, 'lib', f'python{sys.version_info.major}.{sys.version_info.minor}', 'site-packages')
if os.path.exists(site_packages):
for item in os.listdir(site_packages):
if 'pyqt5' in item.lower():
item_path = os.path.join(site_packages, item)
if os.path.isdir(item_path):
import shutil
shutil.rmtree(item_path)
print(f" 删除目录: {item}")
# 3. 使用特定版本安装PyQt5
print("3. 安装PyQt5...")
result = subprocess.run([
sys.executable, "-m", "pip", "install",
"PyQt5==5.15.10",
"--no-cache-dir", # 不使用缓存,确保重新下载
"--force-reinstall" # 强制重新安装
], capture_output=True, text=True)
if result.returncode == 0:
print("✅ PyQt5安装成功")
# 4. 验证安装
print("4. 验证安装...")
test_result = subprocess.run([
sys.executable, "-c",
"from PyQt5.QtWidgets import QApplication; print('PyQt5导入成功')"
], capture_output=True, text=True)
if test_result.returncode == 0:
print("✅ PyQt5验证通过")
return True
else:
print("❌ PyQt5验证失败:", test_result.stderr)
return False
else:
print("❌ PyQt5安装失败:", result.stderr)
return False
if __name__ == "__main__":
success = install_pyqt5_safely()
sys.exit(0 if success else 1)