修改适配Linux
This commit is contained in:
+17
-17
@@ -39,26 +39,26 @@ class AppLauncher:
|
||||
check_virtualenv(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:
|
||||
# sys.prefix != sys.base_prefix 是 Python 检测 venv 的标准方式
|
||||
# 不依赖路径解析,Windows/Linux 均适用
|
||||
in_venv = hasattr(sys, 'real_prefix') or (
|
||||
hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix
|
||||
)
|
||||
|
||||
if not in_venv:
|
||||
import platform
|
||||
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"
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("检测到未使用虚拟环境,正在切换到虚拟环境...")
|
||||
print("=" * 50)
|
||||
|
||||
# 使用虚拟环境的Python重新启动应用(不传递参数避免重复检查)
|
||||
|
||||
import subprocess
|
||||
cmd = [str(venv_python)] + sys.argv
|
||||
subprocess.run(cmd, check=True)
|
||||
|
||||
Reference in New Issue
Block a user