@@ -2011,7 +2011,7 @@ TPythonEngine = class(TPythonInterface)
2011
2011
FAutoFinalize: Boolean;
2012
2012
FProgramName: UnicodeString;
2013
2013
FPythonHome: UnicodeString;
2014
- FPythonPath: WCharTString ;
2014
+ FPythonPath: UnicodeString ;
2015
2015
FOnSysPathInit: TSysPathInitEvent;
2016
2016
FTraceback: TPythonTraceback;
2017
2017
FUseWindowsConsole: Boolean;
@@ -2029,8 +2029,6 @@ TPythonEngine = class(TPythonInterface)
2029
2029
FPyDateTime_TZInfoType: PPyObject;
2030
2030
FPyDateTime_TimeTZType: PPyObject;
2031
2031
FPyDateTime_DateTimeTZType: PPyObject;
2032
- function GetPythonPath : UnicodeString;
2033
- procedure SetPythonPath (const Value : UnicodeString);
2034
2032
2035
2033
protected
2036
2034
procedure Initialize ;
@@ -2150,7 +2148,10 @@ TPythonEngine = class(TPythonInterface)
2150
2148
property IOPythonModule: TObject read FIOPythonModule; { TPythonModule}
2151
2149
property PythonHome: UnicodeString read FPythonHome write SetPythonHome;
2152
2150
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;
2154
2155
published
2155
2156
property AutoFinalize: Boolean read FAutoFinalize write FAutoFinalize default True;
2156
2157
property VenvPythonExe: string read FVenvPythonExe write FVenvPythonExe;
@@ -4686,6 +4687,21 @@ procedure TPythonEngine.Initialize;
4686
4687
FOnSysPathInit(Self, _path);
4687
4688
end ;
4688
4689
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
+
4689
4705
function GetVal (AModule : PPyObject; AVarName : AnsiString) : PPyObject;
4690
4706
begin
4691
4707
Result := PyObject_GetAttrString(AModule, PAnsiChar(AVarName));
@@ -4767,10 +4783,12 @@ procedure TPythonEngine.Initialize;
4767
4783
PyConfig_SetString(Config, @Config.program_name,
4768
4784
PWCharT(StringToWCharTString(FVenvPythonExe)));
4769
4785
4770
- PyConfig_Read(Config);
4771
4786
// Set program arguments (sys.argv)
4772
4787
SetProgramArgs(Config);
4773
4788
4789
+ // PythonPath
4790
+ SetPythonPath(Config);
4791
+
4774
4792
Py_InitializeFromConfig(Config);
4775
4793
finally
4776
4794
PyConfig_Clear(Config);
@@ -4932,18 +4950,6 @@ procedure TPythonEngine.SetPyFlags(const Value: TPythonFlags);
4932
4950
end ; // of if
4933
4951
end ;
4934
4952
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
-
4947
4953
function TPythonEngine.GetSequenceItem (sequence: PPyObject;
4948
4954
idx: Integer): Variant;
4949
4955
var
@@ -4962,15 +4968,6 @@ procedure TPythonEngine.SetPythonHome(const PythonHome: UnicodeString);
4962
4968
FPythonHome := PythonHome;
4963
4969
end ;
4964
4970
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
-
4974
4971
procedure TPythonEngine.SetProgramName (const ProgramName: UnicodeString);
4975
4972
begin
4976
4973
FProgramName := ProgramName;
0 commit comments