修改python版本

This commit is contained in:
wzy-warehouse
2026-06-06 14:18:07 +08:00
parent a8737f1ec8
commit e906f0aeed
4 changed files with 61 additions and 16 deletions
+26 -7
View File
@@ -5,12 +5,13 @@
import sys
import json
import subprocess
import platform
from pathlib import Path
def check_dependencies(project_root: Path):
"""
检查并安装项目依赖
检查并安装项目依赖(使用虚拟环境)
Args:
project_root: 项目根目录路径
@@ -25,10 +26,24 @@ def check_dependencies(project_root: Path):
print(f"警告: 未找到 {requirements_file}")
return
# 获取虚拟环境中的 Python 可执行文件路径
venv_path = project_root / ".venv"
os_name = platform.system()
if os_name == 'Windows':
venv_python = venv_path / "Scripts" / "python.exe"
else: # Linux/Mac
venv_python = venv_path / "bin" / "python3"
if not venv_python.exists():
print(f"错误: 虚拟环境Python不存在: {venv_python}")
print("请先运行虚拟环境检查")
sys.exit(1)
try:
# 使用 pip list 检查已安装的包
# 使用虚拟环境中的 pip 检查已安装的包
result = subprocess.run(
[sys.executable, "-m", "pip", "list", "--format=json"],
[str(venv_python), "-m", "pip", "list", "--format=json"],
capture_output=True,
text=True,
check=True
@@ -54,14 +69,18 @@ def check_dependencies(project_root: Path):
]
if missing_packages:
print(f"发现 {len(missing_packages)} 个未安装的依赖,正在安装...")
print(f"发现 {len(missing_packages)} 个未安装的依赖,正在使用虚拟环境安装...")
subprocess.run(
[sys.executable, "-m", "pip", "install", "-r", str(requirements_file)],
[
str(venv_python), "-m", "pip", "install",
"--trusted-host", "mirrors.aliyun.com",
"-r", str(requirements_file)
],
check=True
)
print("✓ 依赖安装完成")
print("✓ 依赖安装完成(虚拟环境)")
else:
print("✓ 所有依赖已安装")
print("✓ 所有依赖已安装(虚拟环境)")
except subprocess.CalledProcessError as e:
print(f"✗ 依赖检查/安装失败: {e}")
+3 -3
View File
@@ -29,12 +29,12 @@ def check_environment():
# 解析版本号
major, minor, *_ = map(int, python_version.split('.'))
if major == 3 and minor == 13:
if major == 3 and minor == 10:
return True
else:
print(f"✗ Python版本不符合要求!")
print(f" 当前版本: {python_version}")
print(f" 要求版本: 3.13.x")
print(f"\n请使用 Python 3.13 版本运行此项目")
print(f" 要求版本: 3.10.x")
print(f"\n请使用 Python 3.10 版本运行此项目")
print(f"下载地址: https://www.python.org/downloads/")
return False
+26
View File
@@ -37,6 +37,32 @@ class AppLauncher:
# 检查安装依赖
check_dependencies(self.project_root)
# 检查是否正在使用虚拟环境运行
import platform
import sys
venv_path = self.project_root / ".venv"
os_name = platform.system()
if os_name == 'Windows':
venv_python = venv_path / "Scripts" / "python.exe"
else: # Linux/Mac
venv_python = venv_path / "bin" / "python3"
# 如果当前不是使用虚拟环境的Python,则重新启动
current_python = Path(sys.executable).resolve()
venv_python_resolved = venv_python.resolve()
if current_python != venv_python_resolved:
print("\n" + "=" * 50)
print("检测到未使用虚拟环境,正在切换到虚拟环境...")
print("=" * 50)
# 使用虚拟环境的Python重新启动应用
import subprocess
cmd = [str(venv_python)] + sys.argv
subprocess.run(cmd, check=True)
return
# 启动应用
print("\n" + "=" * 50)
print("✓ 所有检查通过,准备启动应用...")