After installing PEWD, create a new Delphi project and add the following code:
delphi
program PythonRunner;
{$APPTYPE CONSOLE}
uses
SysUtils,
ActiveX,
ComObj;
function Py_Initialize(): HRESULT; stdcall; external 'pythonXY.dll' name 'Py_Initialize';
function PyRun_SimpleString(Command: PAnsiChar): Integer; stdcall; external 'pythonXY.dll' name 'PyRun_SimpleString';
function Py_Finalize(): Integer; stdcall; external 'pythonXY.dll' name 'Py_Finalize';
var
PythonCode: string;
PythonDLLPath: string;
ResultCode: Integer;
begin
try
// Set the path to the PythonXY.dll file, adjust it according to your installation.
PythonDLLPath := 'C:\PythonXY\pythonXY.dll';
// Initialize the Python interpreter
if Py_Initialize() <> S_OK then
begin
Writeln('Failed to initialize Python interpreter.');
Halt(1);
end;
// Input Python code
Write('Enter Python code: ');
Readln(PythonCode);
// Execute the Python code
ResultCode := PyRun_SimpleString(PAnsiChar(PythonCode));
// Check if the Python code execution was successful
if ResultCode = 0 then
Writeln('Python code executed successfully.')
else
Writeln('Python code execution failed with error code: ', ResultCode);
// Finalize the Python interpreter
Py_Finalize();
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Replace 'C:\PythonXY\pythonXY.dll' with the path to your pythonXY.dll file.
This program initializes the Python interpreter, takes Python code as input, executes it, and then finalizes the interpreter. Note that this method only works for simple Python scripts and has some limitations, but it serves as a starting point for executing Python code from Delphi.
Make sure to set the correct path to the pythonXY.dll file in the PythonDLLPath variable. Compile and run the program, enter some Python code, and see the output in the console.