修改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 sys
import json import json
import subprocess import subprocess
import platform
from pathlib import Path from pathlib import Path
def check_dependencies(project_root: Path): def check_dependencies(project_root: Path):
""" """
检查并安装项目依赖 检查并安装项目依赖(使用虚拟环境)
Args: Args:
project_root: 项目根目录路径 project_root: 项目根目录路径
@@ -25,10 +26,24 @@ def check_dependencies(project_root: Path):
print(f"警告: 未找到 {requirements_file}") print(f"警告: 未找到 {requirements_file}")
return 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: try:
# 使用 pip list 检查已安装的包 # 使用虚拟环境中的 pip 检查已安装的包
result = subprocess.run( result = subprocess.run(
[sys.executable, "-m", "pip", "list", "--format=json"], [str(venv_python), "-m", "pip", "list", "--format=json"],
capture_output=True, capture_output=True,
text=True, text=True,
check=True check=True
@@ -54,14 +69,18 @@ def check_dependencies(project_root: Path):
] ]
if missing_packages: if missing_packages:
print(f"发现 {len(missing_packages)} 个未安装的依赖,正在安装...") print(f"发现 {len(missing_packages)} 个未安装的依赖,正在使用虚拟环境安装...")
subprocess.run( 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 check=True
) )
print("✓ 依赖安装完成") print("✓ 依赖安装完成(虚拟环境)")
else: else:
print("✓ 所有依赖已安装") print("✓ 所有依赖已安装(虚拟环境)")
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print(f"✗ 依赖检查/安装失败: {e}") print(f"✗ 依赖检查/安装失败: {e}")
+3 -3
View File
@@ -29,12 +29,12 @@ def check_environment():
# 解析版本号 # 解析版本号
major, minor, *_ = map(int, python_version.split('.')) major, minor, *_ = map(int, python_version.split('.'))
if major == 3 and minor == 13: if major == 3 and minor == 10:
return True return True
else: else:
print(f"✗ Python版本不符合要求!") print(f"✗ Python版本不符合要求!")
print(f" 当前版本: {python_version}") print(f" 当前版本: {python_version}")
print(f" 要求版本: 3.13.x") print(f" 要求版本: 3.10.x")
print(f"\n请使用 Python 3.13 版本运行此项目") print(f"\n请使用 Python 3.10 版本运行此项目")
print(f"下载地址: https://www.python.org/downloads/") print(f"下载地址: https://www.python.org/downloads/")
return False return False
+26
View File
@@ -37,6 +37,32 @@ class AppLauncher:
# 检查安装依赖 # 检查安装依赖
check_dependencies(self.project_root) 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("\n" + "=" * 50)
print("✓ 所有检查通过,准备启动应用...") print("✓ 所有检查通过,准备启动应用...")
+6 -6
View File
@@ -1,10 +1,10 @@
dynaconf == 3.2.13 dynaconf == 3.2.13
psycopg2-binary == 2.9.12 psycopg2-binary == 2.9.12
redis == 7.4.0 redis == 8.0.0
numpy == 2.4.4 numpy == 2.2.6
scipy == 1.17.1 scipy == 1.15.3
matplotlib == 3.10.0 matplotlib == 3.10.9
Pillow == 12.2.0 Pillow == 12.2.0
pyyaml == 6.0.2 pyyaml == 6.0.3
fastapi == 0.136.3 fastapi == 0.136.3
uvicorn[standard] == 0.49.0 uvicorn[standard] == 0.48.0