@@ -72,7 +72,8 @@ TPythonVersion = record
7272 function GetRegisteredPythonVersions : TPythonVersions;
7373 (* Returns the highest numbered registered Python version *)
7474 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;
7677 { $ENDIF}
7778
7879implementation
@@ -122,6 +123,11 @@ procedure TPythonVersion.AssignTo(PythonEngine: TPersistent);
122123 TPythonEngine(PythonEngine).DllName := DLLName;
123124 TPythonEngine(PythonEngine).DllPath := DLLPath;
124125 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);
125131 end ;
126132end ;
127133
@@ -416,7 +422,9 @@ function GetLatestRegisteredPythonVersion(out PythonVersion: TPythonVersion): Bo
416422 end ;
417423end ;
418424
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+
420428 function FindPythonDLL (APath : string): string;
421429 Var
422430 FindFileData: TWIN32FindData;
@@ -436,10 +444,29 @@ function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVer
436444 Result := DLLFileName;
437445 end ;
438446
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+
439465Var
440466 DLLFileName: string;
441467 DLLPath: string;
442468 SysVersion: string;
469+ BasePrefix: string;
443470 I: integer;
444471begin
445472 Result := False;
@@ -450,11 +477,24 @@ function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVer
450477 PythonVersion.InstallPath := DLLPath;
451478
452479 DLLFileName := FindPythonDLL(DLLPath);
453- if DLLFileName = ' ' then begin
480+
481+ if (DLLFileName = ' ' ) and AcceptVirtualEnvs then begin
454482 DLLPath := DLLPath + ' \Scripts' ;
455483 DLLFileName := FindPythonDLL(DLLPath);
456484 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 ;
458498
459499 // check if same platform
460500 try
0 commit comments