wuwuxin 28 Posted June 15, 2021 procedure TForm1.PythonEngine1SysPathInit(Sender: TObject; PathList: PPyObject); begin var v := PythonEngine1.PyObjectAsVariant(PathList); // Then what to do????? end; What is the correct way to add to SysPath? Share this post Link to post
wuwuxin 28 Posted June 15, 2021 OK. I found that in order to use Python4Delphi effectively, some knowledge of Python C API is needed, or at least you need to know where to look for answers. I didn't find useful information from this Forum but I googled and googled here is what I found out, in order to add a new entry to SysPath: procedure TForm1.PythonEngine1SysPathInit(Sender: TObject; PathList: PPyObject); begin var _folder: AnsiString := ExtractFilePath(Application.ExeName) + 'scripts'; var v := PythonEngine1.PyList_Append(PathList, PythonEngine1.Py_BuildValue('s', PAnsiChar(_folder))); end; Share this post Link to post
SwiftExpat 65 Posted June 16, 2021 Another way to solve the problem, when you need to add / modify to path, order is likely important. Just run the command in python: import sys sys.path.insert(0, 'path/to/script') The engine has a property, PythonEngine.InitScript: PythonEngine.InitScript.Add('import sys'); PythonEngine.InitScript.Add('sys.path.insert(0, 'path/to/script')'); The benefit is when you attempt to run / unit test in python and need to modify the path. And if you use PyScripter as your python dev tool the approach will be reusable. 1 Share this post Link to post