Jump to content
Sign in to follow this  
wuwuxin

How should I do SysPathInit?

Recommended Posts

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

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

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.

 

 

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×