|
|
@@ -19,13 +19,19 @@ VENV_PATH = Path(_env_path) if _env_path else SCRIPT_DIR / "env"
|
|
|
ENVIRONMENT_FILE = SCRIPT_DIR / "environment.txt"
|
|
|
REQUIREMENTS_FILE = PROJECT_ROOT / "requirements.txt"
|
|
|
|
|
|
-# 根据操作系统确定虚拟环境的 Python 和 pip 路径
|
|
|
+# 根据操作系统确定虚拟环境的 Python 和 pip 路径(及基座 Python 的 pip/virtualenv)
|
|
|
if platform.system() == "Windows":
|
|
|
VENV_PYTHON = VENV_PATH / "Scripts" / "python.exe"
|
|
|
VENV_PIP = VENV_PATH / "Scripts" / "pip.exe"
|
|
|
+ PY_SCRIPTS = SCRIPT_DIR / "py" / "Scripts"
|
|
|
+ PY_PIP = PY_SCRIPTS / "pip.bat" if (PY_SCRIPTS / "pip.bat").exists() else PY_SCRIPTS / "pip.exe"
|
|
|
+ PY_VIRTUALENV = PY_SCRIPTS / "virtualenv.bat" if (PY_SCRIPTS / "virtualenv.bat").exists() else PY_SCRIPTS / "virtualenv.exe"
|
|
|
else:
|
|
|
VENV_PYTHON = VENV_PATH / "bin" / "python"
|
|
|
VENV_PIP = VENV_PATH / "bin" / "pip"
|
|
|
+ PY_SCRIPTS = SCRIPT_DIR / "py" / "bin"
|
|
|
+ PY_PIP = PY_SCRIPTS / "pip"
|
|
|
+ PY_VIRTUALENV = PY_SCRIPTS / "virtualenv"
|
|
|
|
|
|
|
|
|
def run_command(cmd, check=True, capture_output=True):
|
|
|
@@ -78,13 +84,21 @@ def ensure_venv():
|
|
|
return True
|
|
|
if not VENV_PATH.exists():
|
|
|
print("[WARN] Virtual environment not found, creating...")
|
|
|
- success, _, error = run_command(f'"{sys.executable}" -m venv "{VENV_PATH}"', check=False)
|
|
|
- if not success and "No module named venv" in (error or ""):
|
|
|
- print("[WARN] venv module not found, using virtualenv...")
|
|
|
- run_command(f'"{sys.executable}" -m pip install virtualenv -q', check=False)
|
|
|
- success, _, error = run_command(f'"{sys.executable}" -m virtualenv "{VENV_PATH}"', check=False)
|
|
|
+ success, out, error = run_command(f'"{sys.executable}" -m venv "{VENV_PATH}"', check=False)
|
|
|
+ merged = (error or "") + (out or "")
|
|
|
+ if not success and "No module named venv" in merged:
|
|
|
+ # 方案1:重装 virtualenv 后使用
|
|
|
+ print("[WARN] 标准库 venv 不可用,正在重装 virtualenv...")
|
|
|
+ pip_ok, pip_out, pip_err = run_command(f'"{PY_PIP}" install --force-reinstall virtualenv', check=False)
|
|
|
+ if not pip_ok:
|
|
|
+ print(f"[X] 重装 virtualenv 失败: {(pip_err or '') + (pip_out or '')}")
|
|
|
+ sys.exit(1)
|
|
|
+ print("[OK] virtualenv 已重装")
|
|
|
+ venv_cmd = f'"{sys.executable}" -m virtualenv "{VENV_PATH}"'
|
|
|
+ success, out, error = run_command(venv_cmd, check=False)
|
|
|
+ merged = (error or "") + (out or "")
|
|
|
if not success:
|
|
|
- print(f"[X] Failed to create virtual environment: {error}")
|
|
|
+ print(f"[X] Failed to create virtual environment: {merged.strip() or '(no output)'}")
|
|
|
sys.exit(1)
|
|
|
print("[OK] Virtual environment created successfully")
|
|
|
return True
|