Skip to content

Commit 40dbaaf

Browse files
authored
Ensure venv creation and packages checks for python2 before attempting creation or package operations (#66)
Closes #67
1 parent dc7a462 commit 40dbaaf

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

src/api.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,9 @@ export interface PythonEnvironmentInfo {
193193
readonly iconPath?: IconPath;
194194

195195
/**
196-
* Information on how to execute the Python environment. If not provided, {@link PythonEnvironmentApi.resolveEnvironment} will be
197-
* used to to get the details at later point if needed. The recommendation is to fill this in if known.
196+
* Information on how to execute the Python environment. This is required for executing Python code in the environment.
198197
*/
199-
readonly execInfo?: PythonEnvironmentExecutionInfo;
198+
readonly execInfo: PythonEnvironmentExecutionInfo;
200199

201200
/**
202201
* `sys.prefix` is the path to the base directory of the Python installation. Typically obtained by executing `sys.prefix` in the Python interpreter.

src/features/pythonApi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
116116
};
117117
return new PythonEnvironmentImpl(envId, info);
118118
}
119+
119120
async createEnvironment(scope: CreateEnvironmentScope): Promise<PythonEnvironment | undefined> {
120121
if (scope === 'global' || (!Array.isArray(scope) && scope instanceof Uri)) {
121122
const manager = this.envManagers.getEnvironmentManager(scope === 'global' ? undefined : scope);

src/internal.api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export class PythonEnvironmentImpl implements PythonEnvironment {
266266
public readonly description?: string;
267267
public readonly tooltip?: string | MarkdownString;
268268
public readonly iconPath?: IconPath;
269-
public readonly execInfo?: PythonEnvironmentExecutionInfo;
269+
public readonly execInfo: PythonEnvironmentExecutionInfo;
270270
public readonly sysPrefix: string;
271271

272272
constructor(public readonly envId: PythonEnvironmentId, info: PythonEnvironmentInfo) {

src/managers/sysPython/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ export async function installPackages(
313313
api: PythonEnvironmentApi,
314314
manager: PackageManager,
315315
): Promise<Package[]> {
316+
if (environment.version.startsWith('2.')) {
317+
throw new Error('Python 2.* is not supported (deprecated)');
318+
}
319+
316320
if (environment.execInfo) {
317321
if (packages.length === 0) {
318322
throw new Error('No packages selected to install');
@@ -350,6 +354,10 @@ export async function uninstallPackages(
350354
manager: PackageManager,
351355
packages: string[] | Package[],
352356
): Promise<Package[]> {
357+
if (environment.version.startsWith('2.')) {
358+
throw new Error('Python 2.* is not supported (deprecated)');
359+
}
360+
353361
if (environment.execInfo) {
354362
const remove = [];
355363
for (let pkg of packages) {

src/managers/sysPython/venvUtils.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,35 @@ export async function createPythonVenv(
254254
basePythons: PythonEnvironment[],
255255
venvRoot: Uri,
256256
): Promise<PythonEnvironment | undefined> {
257-
const filtered = basePythons.filter((e) => e.execInfo);
258-
if (filtered.length === 0) {
257+
if (basePythons.length === 0) {
259258
log.error('No base python found');
260259
showErrorMessage('No base python found');
261260
return;
262261
}
263262

263+
const filtered = basePythons.filter((e) => e.version.startsWith('3.'));
264+
if (filtered.length === 0) {
265+
log.error('Did not find any base python 3.*');
266+
showErrorMessage('Did not find any base python 3.*');
267+
basePythons.forEach((e) => {
268+
log.error(`available base python: ${e.version}`);
269+
});
270+
return;
271+
}
272+
264273
const basePython = await pickEnvironmentFrom(sortEnvironments(filtered));
265274
if (!basePython || !basePython.execInfo) {
266275
log.error('No base python selected, cannot create virtual environment.');
267276
showErrorMessage('No base python selected, cannot create virtual environment.');
268277
return;
269278
}
270279

280+
if (basePython.version.startsWith('2.')) {
281+
log.error('Python 2.* is not supported for virtual env creation');
282+
showErrorMessage('Python 2.* is not supported, use Python 3.*');
283+
return;
284+
}
285+
271286
const name = await showInputBox({
272287
prompt: 'Enter name for virtual environment',
273288
value: '.venv',

0 commit comments

Comments
 (0)