Skip to content

Commit

Permalink
use getCondaPath, getSystemPythonPath
Browse files Browse the repository at this point in the history
  • Loading branch information
mbektas committed Jan 23, 2024
1 parent a44eef8 commit 2061a97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
26 changes: 17 additions & 9 deletions src/main/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import { app } from 'electron';
import {
condaEnvPathForCondaExePath,
getCondaChannels,
getCondaPath,
getPythonEnvsDirectory,
getSystemPythonPath,
ICommandRunCallbacks,
runCommandInEnvironment,
validateCondaPath,
Expand Down Expand Up @@ -192,6 +194,7 @@ export async function handleEnvInfoCommand(argv: any) {
fs.existsSync(bundledPythonPath) &&
(fs.statSync(bundledPythonPath).isFile() ||
fs.statSync(bundledPythonPath).isSymbolicLink());
// TODO: move this logic to getJupyterLabPythonPath or similar
let defaultPythonPath = userSettings.getValue(SettingType.pythonPath);
if (!defaultPythonPath) {
defaultPythonPath = getBundledPythonPath();
Expand All @@ -203,11 +206,11 @@ export async function handleEnvInfoCommand(argv: any) {
fs.existsSync(defaultPythonPath) &&
(fs.statSync(defaultPythonPath).isFile() ||
fs.statSync(defaultPythonPath).isSymbolicLink());
const condaPath = appData.condaPath;
const condaPath = getCondaPath();
const condaChannels = userSettings.getValue(SettingType.condaChannels);
const condaPathExists =
condaPath && fs.existsSync(condaPath) && fs.statSync(condaPath).isFile();
const systemPythonPath = appData.systemPythonPath;
const systemPythonPath = getSystemPythonPath();
const systemPythonPathExists =
systemPythonPath &&
fs.existsSync(systemPythonPath) &&
Expand Down Expand Up @@ -362,7 +365,8 @@ async function installAdditionalCondaPackagesToEnv(
channelList?: string[],
callbacks?: ICommandRunCallbacks
) {
const baseCondaEnvPath = condaEnvPathForCondaExePath(appData.condaPath);
const baseCondaPath = getCondaPath();
const baseCondaEnvPath = condaEnvPathForCondaExePath(baseCondaPath);
const condaBaseEnvExists = isBaseCondaEnv(baseCondaEnvPath);

if (!condaBaseEnvExists) {
Expand Down Expand Up @@ -434,7 +438,8 @@ export async function createPythonEnvironment(
sourceType
} = options;
const isConda = envType === 'conda';
const baseCondaEnvPath = condaEnvPathForCondaExePath(appData.condaPath);
const baseCondaPath = getCondaPath();
const baseCondaEnvPath = condaEnvPathForCondaExePath(baseCondaPath);
const condaBaseEnvExists = isBaseCondaEnv(baseCondaEnvPath);
const packages = packageList ? packageList.join(' ') : '';

Expand Down Expand Up @@ -491,11 +496,12 @@ export async function createPythonEnvironment(
await runCommandInEnvironment(baseCondaEnvPath, createCommand, callbacks);
}
} else {
const systemPythonPath = getSystemPythonPath();
if (condaBaseEnvExists) {
const createCommand = `python -m venv create ${envPath}`;
await runCommandInEnvironment(baseCondaEnvPath, createCommand, callbacks);
} else if (fs.existsSync(appData.systemPythonPath)) {
execFileSync(appData.systemPythonPath, ['-m', 'venv', 'create', envPath]);
} else if (fs.existsSync(systemPythonPath)) {
execFileSync(systemPythonPath, ['-m', 'venv', 'create', envPath]);
} else {
throw new Error(
'Failed to create Python environment. Python executable not found.'
Expand Down Expand Up @@ -591,7 +597,8 @@ export async function handleEnvCreateCommand(argv: any) {
sourceType === 'conda-pack' ||
sourceType === 'conda-lock-file' ||
sourceType === 'conda-env-file';
const condaEnvPath = condaEnvPathForCondaExePath(appData.condaPath);
const baseCondaPath = getCondaPath();
const condaEnvPath = condaEnvPathForCondaExePath(baseCondaPath);
const condaBaseEnvExists = isBaseCondaEnv(condaEnvPath);

const packageList: string[] = argv._.slice(1);
Expand Down Expand Up @@ -751,8 +758,9 @@ export async function launchCLIinEnvironment(
const isWin = process.platform === 'win32';
envPath = envPath || getBundledPythonEnvPath();

const condaEnvPath = condaEnvPathForCondaExePath(appData.condaPath);
const activateCommand = createCommandScriptInEnv(envPath, condaEnvPath);
const baseCondaPath = getCondaPath();
const baseCondaEnvPath = condaEnvPathForCondaExePath(baseCondaPath);
const activateCommand = createCommandScriptInEnv(envPath, baseCondaEnvPath);
const ext = isWin ? 'bat' : 'sh';
const activateFilePath = createTempFile(`activate.${ext}`, activateCommand);

Expand Down
3 changes: 2 additions & 1 deletion src/main/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ export async function runCommandInEnvironment(
callbacks?: ICommandRunCallbacks
) {
const isWin = process.platform === 'win32';
const condaEnvPath = condaEnvPathForCondaExePath(appData.condaPath);
const baseCondaPath = getCondaPath();
const condaEnvPath = condaEnvPathForCondaExePath(baseCondaPath);
const commandScript = createCommandScriptInEnv(
envPath,
condaEnvPath,
Expand Down

0 comments on commit 2061a97

Please sign in to comment.