diff --git a/src/contributes/commandsBase.ts b/src/contributes/commandsBase.ts index cd119d7..7ffe9f7 100644 --- a/src/contributes/commandsBase.ts +++ b/src/contributes/commandsBase.ts @@ -28,6 +28,23 @@ export function checkDevChatDependency(): boolean { } } +export function getValidPythonCommand(): string | undefined { + try { + runCommand('python3 -V'); + return 'python3'; + } catch (error) { + try { + const version = runCommand('python -V'); + if (version.includes('Python 3')) { + return 'python'; + } + return undefined; + } catch (error) { + return undefined; + } + } +} + export function getPipxEnvironmentPath(): string | null { // Get pipx environment const pipxEnvOutput = runCommand('python3 -m pipx environment').toString(); diff --git a/src/panel/statusBarViewBase.ts b/src/panel/statusBarViewBase.ts index b38d382..ef4a260 100644 --- a/src/panel/statusBarViewBase.ts +++ b/src/panel/statusBarViewBase.ts @@ -4,7 +4,7 @@ import { logger } from "../util/logger"; import { UiUtilWrapper } from "../util/uiUtil"; import { TopicManager } from "../topic/topicManager"; -import { checkDevChatDependency } from "../contributes/commandsBase"; +import { checkDevChatDependency, getValidPythonCommand } from "../contributes/commandsBase"; import { ApiKeyManager } from '../util/apiKey'; @@ -37,7 +37,7 @@ export async function dependencyCheck(): Promise<[string, string]> { // 1. not in a folder // 2. dependence is invalid // 3. ready - if (devchatStatus === '' || devchatStatus === 'waiting install devchat') { + if (devchatStatus === '' || devchatStatus === 'Waiting for devchat installation to complete') { let bOk = true; let devChat: string | undefined = UiUtilWrapper.getConfiguration('DevChat', 'DevChatPath'); if (!devChat) { @@ -60,20 +60,30 @@ export async function dependencyCheck(): Promise<[string, string]> { } } } - if (devchatStatus === 'not ready') { + if (devchatStatus === 'not ready' || devchatStatus === 'Waiting for Python3 installation to complete') { // auto install devchat - UiUtilWrapper.runTerminal('DevChat Install', `python3 ${UiUtilWrapper.extensionPath() + "/tools/install.py"}`); - devchatStatus = 'waiting install devchat'; - isVersionChangeCompare = true; + // check whether python3 exist + const pythonCommand = getValidPythonCommand(); + if (!pythonCommand && devchatStatus === 'not ready') { + UiUtilWrapper.showErrorMessage('Python3 not found.'); + devchatStatus = 'Waiting for Python3 installation to complete'; + isVersionChangeCompare = true; + } else if (!pythonCommand) { + // Waiting for Python3 installation to complete + } else { + UiUtilWrapper.runTerminal('DevChat Install', `${pythonCommand} ${UiUtilWrapper.extensionPath() + "/tools/install.py"}`); + devchatStatus = 'Waiting for devchat installation to complete'; + isVersionChangeCompare = true; + } } // check api key - if (apiKeyStatus === '' || apiKeyStatus === 'please set api key') { + if (apiKeyStatus === '' || apiKeyStatus === 'Please set the API key') { const bOk = await ApiKeyManager.getApiKey(); if (bOk) { apiKeyStatus = 'ready'; } else { - apiKeyStatus = 'please set api key'; + apiKeyStatus = 'Please set the API key'; } } diff --git a/tools/install.py b/tools/install.py index 9ecdff0..d6b5c49 100644 --- a/tools/install.py +++ b/tools/install.py @@ -2,6 +2,10 @@ import os import subprocess import sys +# replace python3 with sys.executable, we will do everything in the same envrionment +pythonCommand = sys.executable + + def check_pipx_installed(): try: subprocess.run(["pipx", "--version"], check=True) @@ -12,7 +16,7 @@ def check_pipx_installed(): def install_pipx(): print("Installing pipx...") try: - subprocess.run(["python3", "-m", "pip", "install", "pipx", "--force"], check=True) + subprocess.run([pythonCommand, "-m", "pip", "install", "pipx", "--force"], check=True) print("pipx installed successfully.") except subprocess.CalledProcessError as e: print("Error installing pipx:", e, file=sys.stderr) @@ -20,8 +24,8 @@ def install_pipx(): def add_pipx_to_path(): print("Adding pipx to PATH...") - subprocess.run(["python3", "-m", "pipx", "ensurepath"], check=True) - result = subprocess.run(["python3", "-m", "pipx", "environment"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + subprocess.run([pythonCommand, "-m", "pipx", "ensurepath"], check=True) + result = subprocess.run([pythonCommand, "-m", "pipx", "environment"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) pipx_path_line = [line for line in result.stdout.splitlines() if "PIPX_BIN_DIR" in line] if pipx_path_line: pipx_path = pipx_path_line[0].split('=')[-1].strip()