Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b2a7bd

Browse files
committed
The public PythonPath property can be used to initialize sys.path.
1 parent 603a9f6 commit 5b2a7bd

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

Source/PythonEngine.pas

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,7 +2011,7 @@ TPythonEngine = class(TPythonInterface)
20112011
FAutoFinalize: Boolean;
20122012
FProgramName: UnicodeString;
20132013
FPythonHome: UnicodeString;
2014-
FPythonPath: WCharTString;
2014+
FPythonPath: UnicodeString;
20152015
FOnSysPathInit: TSysPathInitEvent;
20162016
FTraceback: TPythonTraceback;
20172017
FUseWindowsConsole: Boolean;
@@ -2029,8 +2029,6 @@ TPythonEngine = class(TPythonInterface)
20292029
FPyDateTime_TZInfoType: PPyObject;
20302030
FPyDateTime_TimeTZType: PPyObject;
20312031
FPyDateTime_DateTimeTZType: PPyObject;
2032-
function GetPythonPath: UnicodeString;
2033-
procedure SetPythonPath(const Value: UnicodeString);
20342032

20352033
protected
20362034
procedure Initialize;
@@ -2150,7 +2148,10 @@ TPythonEngine = class(TPythonInterface)
21502148
property IOPythonModule: TObject read FIOPythonModule; {TPythonModule}
21512149
property PythonHome: UnicodeString read FPythonHome write SetPythonHome;
21522150
property ProgramName: UnicodeString read FProgramName write SetProgramName;
2153-
property PythonPath: UnicodeString read GetPythonPath write SetPythonPath;
2151+
// List of paths separated with the path delimiter
2152+
// If used with pfNoSite, it completely overwrites the pyhon path on initialization!
2153+
// For adding directories to sys.path use the OnSysPathInit event instead.
2154+
property PythonPath: UnicodeString read FPythonPath write FPythonPath;
21542155
published
21552156
property AutoFinalize: Boolean read FAutoFinalize write FAutoFinalize default True;
21562157
property VenvPythonExe: string read FVenvPythonExe write FVenvPythonExe;
@@ -4686,6 +4687,21 @@ procedure TPythonEngine.Initialize;
46864687
FOnSysPathInit(Self, _path);
46874688
end;
46884689

4690+
procedure SetPythonPath(var Config: PyConfig);
4691+
var
4692+
Paths: TArray<string>;
4693+
I: Integer;
4694+
begin
4695+
if FPythonPath = '' then Exit;
4696+
4697+
Paths := FPythonPath.Split([PathSep], TStringSplitOptions.ExcludeLastEmpty);
4698+
for I := 0 to Length(Paths) - 1 do
4699+
PyWideStringList_Append(Config.module_search_paths,
4700+
PWCharT(StringToWCharTString(Paths[I])));
4701+
if Config.module_search_paths.length > 0 then
4702+
Config.module_search_paths_set := 1;
4703+
end;
4704+
46894705
function GetVal(AModule : PPyObject; AVarName : AnsiString) : PPyObject;
46904706
begin
46914707
Result := PyObject_GetAttrString(AModule, PAnsiChar(AVarName));
@@ -4767,10 +4783,12 @@ procedure TPythonEngine.Initialize;
47674783
PyConfig_SetString(Config, @Config.program_name,
47684784
PWCharT(StringToWCharTString(FVenvPythonExe)));
47694785

4770-
PyConfig_Read(Config);
47714786
// Set program arguments (sys.argv)
47724787
SetProgramArgs(Config);
47734788

4789+
// PythonPath
4790+
SetPythonPath(Config);
4791+
47744792
Py_InitializeFromConfig(Config);
47754793
finally
47764794
PyConfig_Clear(Config);
@@ -4932,18 +4950,6 @@ procedure TPythonEngine.SetPyFlags(const Value: TPythonFlags);
49324950
end; // of if
49334951
end;
49344952

4935-
function TPythonEngine.GetPythonPath: UnicodeString;
4936-
begin
4937-
{$IFDEF POSIX}
4938-
if (Length(FPythonPath) > 0) then
4939-
Result := UCS4StringToUnicodeString(FPythonPath)
4940-
else
4941-
Result := '';
4942-
{$ELSE}
4943-
Result := FPythonPath;
4944-
{$ENDIF}
4945-
end;
4946-
49474953
function TPythonEngine.GetSequenceItem(sequence: PPyObject;
49484954
idx: Integer): Variant;
49494955
var
@@ -4962,15 +4968,6 @@ procedure TPythonEngine.SetPythonHome(const PythonHome: UnicodeString);
49624968
FPythonHome := PythonHome;
49634969
end;
49644970

4965-
procedure TPythonEngine.SetPythonPath(const Value: UnicodeString);
4966-
begin
4967-
{$IFDEF POSIX}
4968-
FPythonPath := UnicodeStringToUCS4String(Value);
4969-
{$ELSE}
4970-
FPythonPath := Value;
4971-
{$ENDIF}
4972-
end;
4973-
49744971
procedure TPythonEngine.SetProgramName(const ProgramName: UnicodeString);
49754972
begin
49764973
FProgramName := ProgramName;

0 commit comments

Comments
 (0)