@@ -72,7 +72,8 @@ TPythonVersion = record
72
72
function GetRegisteredPythonVersions : TPythonVersions;
73
73
(* Returns the highest numbered registered Python version *)
74
74
function GetLatestRegisteredPythonVersion (out PythonVersion: TPythonVersion): Boolean;
75
- function PythonVersionFromPath (const Path: string; out PythonVersion: TPythonVersion): Boolean;
75
+ function PythonVersionFromPath (const Path: string; out PythonVersion: TPythonVersion;
76
+ AcceptVirtualEnvs: Boolean = True): Boolean;
76
77
{ $ENDIF}
77
78
78
79
implementation
@@ -122,6 +123,11 @@ procedure TPythonVersion.AssignTo(PythonEngine: TPersistent);
122
123
TPythonEngine(PythonEngine).DllName := DLLName;
123
124
TPythonEngine(PythonEngine).DllPath := DLLPath;
124
125
TPythonEngine(PythonEngine).APIVersion := ApiVersion;
126
+ if Is_venv then begin
127
+ TPythonEngine(PythonEngine).VenvPythonExe := PythonExecutable;
128
+ TPythonEngine(PythonEngine).SetPythonHome(DLLPath);
129
+ end else if not IsRegistered then
130
+ TPythonEngine(PythonEngine).SetPythonHome(InstallPath);
125
131
end ;
126
132
end ;
127
133
@@ -416,7 +422,9 @@ function GetLatestRegisteredPythonVersion(out PythonVersion: TPythonVersion): Bo
416
422
end ;
417
423
end ;
418
424
419
- function PythonVersionFromPath (const Path: string; out PythonVersion: TPythonVersion): Boolean;
425
+ function PythonVersionFromPath (const Path: string; out PythonVersion: TPythonVersion;
426
+ AcceptVirtualEnvs: Boolean = True): Boolean;
427
+
420
428
function FindPythonDLL (APath : string): string;
421
429
Var
422
430
FindFileData: TWIN32FindData;
@@ -436,10 +444,29 @@ function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVer
436
444
Result := DLLFileName;
437
445
end ;
438
446
447
+ function GetVenvBasePrefix (InstallPath: string): string;
448
+ var
449
+ SL : TStringList;
450
+ begin
451
+ SL := TStringList.Create;
452
+ try
453
+ try
454
+ SL.LoadFromFile(IncludeTrailingPathDelimiter(InstallPath)+' pyvenv.cfg' );
455
+ Result := Trim(SL.Values[' home' ]);
456
+ if Result = ' ' then
457
+ Result := Trim(SL.Values[' home ' ]);
458
+ except
459
+ end ;
460
+ finally
461
+ SL.Free;
462
+ end ;
463
+ end ;
464
+
439
465
Var
440
466
DLLFileName: string;
441
467
DLLPath: string;
442
468
SysVersion: string;
469
+ BasePrefix: string;
443
470
I: integer;
444
471
begin
445
472
Result := False;
@@ -450,11 +477,24 @@ function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVer
450
477
PythonVersion.InstallPath := DLLPath;
451
478
452
479
DLLFileName := FindPythonDLL(DLLPath);
453
- if DLLFileName = ' ' then begin
480
+
481
+ if (DLLFileName = ' ' ) and AcceptVirtualEnvs then begin
454
482
DLLPath := DLLPath + ' \Scripts' ;
455
483
DLLFileName := FindPythonDLL(DLLPath);
456
484
end ;
457
- if DLLFileName = ' ' then Exit;
485
+ if DLLFileName = ' ' then begin
486
+ if AcceptVirtualEnvs and PythonVersion.Is_venv then
487
+ begin
488
+ BasePrefix := GetVenvBasePrefix(PythonVersion.InstallPath);
489
+ if (BasePrefix <> ' ' ) and PythonVersionFromPath(BasePrefix, PythonVersion, False) then
490
+ begin
491
+ // Install path points to venv but the rest of the info is from base_prefix
492
+ PythonVersion.InstallPath := ExcludeTrailingPathDelimiter(Path);
493
+ Result := True;
494
+ end ;
495
+ end ;
496
+ Exit;
497
+ end ;
458
498
459
499
// check if same platform
460
500
try
0 commit comments