From e906f0aeed77e9cd2e7dd9a37d8cf6e3cbf6712a Mon Sep 17 00:00:00 2001 From: wzy-warehouse <18135009705@163.com> Date: Sat, 6 Jun 2026 14:18:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9python=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/dependency_manager.py | 33 ++++++++++++++++++++++++++------- app/core/env_checker.py | 6 +++--- app/core/launcher.py | 26 ++++++++++++++++++++++++++ requirements.txt | 12 ++++++------ 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/app/core/dependency_manager.py b/app/core/dependency_manager.py index 20c4b9c..c724e8a 100644 --- a/app/core/dependency_manager.py +++ b/app/core/dependency_manager.py @@ -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}") diff --git a/app/core/env_checker.py b/app/core/env_checker.py index c2ce9ef..1bde570 100644 --- a/app/core/env_checker.py +++ b/app/core/env_checker.py @@ -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 diff --git a/app/core/launcher.py b/app/core/launcher.py index 8784446..5fb3633 100644 --- a/app/core/launcher.py +++ b/app/core/launcher.py @@ -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("✓ 所有检查通过,准备启动应用...") diff --git a/requirements.txt b/requirements.txt index 1e843cc..1851c1c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,10 +1,10 @@ dynaconf == 3.2.13 psycopg2-binary == 2.9.12 -redis == 7.4.0 -numpy == 2.4.4 -scipy == 1.17.1 -matplotlib == 3.10.0 +redis == 8.0.0 +numpy == 2.2.6 +scipy == 1.15.3 +matplotlib == 3.10.9 Pillow == 12.2.0 -pyyaml == 6.0.2 +pyyaml == 6.0.3 fastapi == 0.136.3 -uvicorn[standard] == 0.49.0 \ No newline at end of file +uvicorn[standard] == 0.48.0 \ No newline at end of file