|
|
#!/usr/bin/env python3
|
|
|
# -*- coding: utf-8 -*-
|
|
|
"""
|
|
|
MagicWord 安装和修复脚本
|
|
|
用于安装依赖并解决Qt平台插件问题
|
|
|
"""
|
|
|
|
|
|
import os
|
|
|
import sys
|
|
|
import subprocess
|
|
|
import platform
|
|
|
import importlib.util
|
|
|
|
|
|
def check_python_version():
|
|
|
"""检查Python版本"""
|
|
|
version = sys.version_info
|
|
|
print(f"当前Python版本: {version.major}.{version.minor}.{version.micro}")
|
|
|
|
|
|
if version.major < 3 or (version.major == 3 and version.minor < 6):
|
|
|
print("错误: 需要Python 3.6或更高版本")
|
|
|
return False
|
|
|
|
|
|
return True
|
|
|
|
|
|
def run_command(command, shell=False):
|
|
|
"""运行命令并返回结果"""
|
|
|
try:
|
|
|
result = subprocess.run(command, shell=shell, capture_output=True, text=True, encoding='utf-8')
|
|
|
return result.returncode, result.stdout, result.stderr
|
|
|
except Exception as e:
|
|
|
return -1, "", str(e)
|
|
|
|
|
|
def install_requirements():
|
|
|
"""安装requirements.txt中的依赖"""
|
|
|
requirements_file = "requirements.txt"
|
|
|
|
|
|
if not os.path.exists(requirements_file):
|
|
|
print(f"错误: 找不到 {requirements_file} 文件")
|
|
|
return False
|
|
|
|
|
|
print("安装项目依赖...")
|
|
|
try:
|
|
|
code, stdout, stderr = run_command([sys.executable, "-m", "pip", "install", "-r", requirements_file])
|
|
|
if code == 0:
|
|
|
print("所有依赖安装成功")
|
|
|
return True
|
|
|
else:
|
|
|
print(f"依赖安装失败: {stderr}")
|
|
|
return False
|
|
|
except Exception as e:
|
|
|
print(f"依赖安装异常: {e}")
|
|
|
return False
|
|
|
|
|
|
def check_pyqt5():
|
|
|
"""检查PyQt5安装"""
|
|
|
print("检查PyQt5安装...")
|
|
|
try:
|
|
|
import PyQt5
|
|
|
pyqt5_path = os.path.dirname(PyQt5.__file__)
|
|
|
print(f"PyQt5已安装,路径: {pyqt5_path}")
|
|
|
return True
|
|
|
except ImportError:
|
|
|
print("PyQt5未安装")
|
|
|
return False
|
|
|
|
|
|
def reinstall_pyqt5():
|
|
|
"""重新安装PyQt5"""
|
|
|
print("重新安装PyQt5...")
|
|
|
try:
|
|
|
# 先卸载
|
|
|
print("卸载PyQt5...")
|
|
|
code, stdout, stderr = run_command([sys.executable, "-m", "pip", "uninstall", "PyQt5", "-y"])
|
|
|
|
|
|
# 重新安装
|
|
|
print("安装PyQt5...")
|
|
|
code, stdout, stderr = run_command([sys.executable, "-m", "pip", "install", "PyQt5"])
|
|
|
if code == 0:
|
|
|
print("PyQt5重新安装成功")
|
|
|
return True
|
|
|
else:
|
|
|
print(f"PyQt5重新安装失败: {stderr}")
|
|
|
return False
|
|
|
except Exception as e:
|
|
|
print(f"PyQt5重新安装异常: {e}")
|
|
|
return False
|
|
|
|
|
|
def create_qt_debug_script():
|
|
|
"""创建Qt调试运行脚本"""
|
|
|
system = platform.system()
|
|
|
|
|
|
if system == "Windows":
|
|
|
script_content = """@echo off
|
|
|
echo 设置Qt调试环境变量...
|
|
|
set QT_DEBUG_PLUGINS=1
|
|
|
echo Qt调试模式已启用
|
|
|
echo.
|
|
|
echo 运行MagicWord应用程序...
|
|
|
python src/main.py
|
|
|
pause
|
|
|
"""
|
|
|
script_name = "run_debug.bat"
|
|
|
else:
|
|
|
script_content = """#!/bin/bash
|
|
|
echo "设置Qt调试环境变量..."
|
|
|
export QT_DEBUG_PLUGINS=1
|
|
|
echo "Qt调试模式已启用"
|
|
|
echo ""
|
|
|
echo "运行MagicWord应用程序..."
|
|
|
python src/main.py
|
|
|
"""
|
|
|
script_name = "run_debug.sh"
|
|
|
|
|
|
try:
|
|
|
with open(script_name, "w", encoding="utf-8") as f:
|
|
|
f.write(script_content)
|
|
|
print(f"调试运行脚本创建完成: {script_name}")
|
|
|
return True
|
|
|
except Exception as e:
|
|
|
print(f"创建调试运行脚本失败: {e}")
|
|
|
return False
|
|
|
|
|
|
def main():
|
|
|
"""主函数"""
|
|
|
print("=" * 60)
|
|
|
print("MagicWord 项目安装和修复脚本")
|
|
|
print("=" * 60)
|
|
|
|
|
|
# 检查Python版本
|
|
|
if not check_python_version():
|
|
|
sys.exit(1)
|
|
|
|
|
|
# 升级pip
|
|
|
print("\n升级pip...")
|
|
|
code, stdout, stderr = run_command([sys.executable, "-m", "pip", "install", "--upgrade", "pip"])
|
|
|
if code != 0:
|
|
|
print(f"pip升级警告: {stderr}")
|
|
|
else:
|
|
|
print("pip升级完成")
|
|
|
|
|
|
# 安装依赖
|
|
|
print("\n安装项目依赖...")
|
|
|
if not install_requirements():
|
|
|
print("依赖安装失败")
|
|
|
sys.exit(1)
|
|
|
|
|
|
# 检查PyQt5
|
|
|
print("\n检查PyQt5...")
|
|
|
if not check_pyqt5():
|
|
|
print("PyQt5未安装,开始安装...")
|
|
|
code, stdout, stderr = run_command([sys.executable, "-m", "pip", "install", "PyQt5"])
|
|
|
if code == 0:
|
|
|
print("PyQt5安装成功")
|
|
|
else:
|
|
|
print(f"PyQt5安装失败: {stderr}")
|
|
|
# 尝试重新安装
|
|
|
if not reinstall_pyqt5():
|
|
|
print("PyQt5重新安装也失败了")
|
|
|
|
|
|
# 再次检查PyQt5
|
|
|
if check_pyqt5():
|
|
|
# 检查PyQt5插件路径
|
|
|
try:
|
|
|
import PyQt5
|
|
|
pyqt5_path = os.path.dirname(PyQt5.__file__)
|
|
|
plugin_paths = [
|
|
|
os.path.join(pyqt5_path, "Qt5", "plugins"),
|
|
|
os.path.join(pyqt5_path, "plugins"),
|
|
|
]
|
|
|
|
|
|
plugin_path = None
|
|
|
for path in plugin_paths:
|
|
|
if os.path.exists(path):
|
|
|
plugin_path = path
|
|
|
break
|
|
|
|
|
|
if plugin_path:
|
|
|
print(f"PyQt5插件路径: {plugin_path}")
|
|
|
platforms_path = os.path.join(plugin_path, "platforms")
|
|
|
if os.path.exists(platforms_path):
|
|
|
print(f"Platform插件路径: {platforms_path}")
|
|
|
else:
|
|
|
print("未找到platforms目录")
|
|
|
else:
|
|
|
print("未找到PyQt5插件目录")
|
|
|
except Exception as e:
|
|
|
print(f"检查PyQt5插件时出错: {e}")
|
|
|
|
|
|
# 创建调试脚本
|
|
|
print("\n创建调试运行脚本...")
|
|
|
create_qt_debug_script()
|
|
|
|
|
|
print("\n" + "=" * 60)
|
|
|
print("安装和修复完成!")
|
|
|
print("如果问题仍然存在,请尝试以下方法:")
|
|
|
print("1. 运行创建的调试脚本查看详细错误信息")
|
|
|
print("2. 检查是否安装了Visual C++运行库")
|
|
|
print("3. 确保使用的是Python 3.6或更高版本")
|
|
|
print("=" * 60)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
main() |