FranzB 0 Posted March 14, 2021 (edited) based on demo code 34 I wrote this code with the following new features a) create Pythonengine and PythonInOut at run time b) config pythonengine from an INI file c) find/exchange config parameter for different python installations , UNIX systems here in this forum d) set path to python libs via this INI file, no need for recompile Issues with this code a) OS=WINDOWS : text output of python is not shown in the memo. Guess this is due to the fact that I create the PythonInOut at run time and the is no property defined ( memo, using FMX or VCL ???) b) OS=LINUX : text output of python is not shown in the memo but in the calling CMD Window of this app. Guess a fix of a) will also help here c) pls. share addition setting to python libs like numpy etc. , what settings worked on your computer , how to find our the correct parameter on my computer etc. ....... i added the complete project, pls. share your improvement ideas 🙂 the used config file for windows did not have any problem calling a few NumPy statements [PythonEngine] DLLName=python39.dll DLLPath= AutoFinalize=0 AutoLoad=0 AutoUnload=0 RedirectIO=0 UseLastKnownVersion=0 UseWindowsConsole=0 the used config file for LINUX, simple code runs well, but I failed to set the path to the NumPy lib on the target system [PythonEngine] DLLName=libpython3.6m.so DLLPath=/usr/lib/x86_64-linux-gnu/ AutoFinalize=0 AutoLoad=0 AutoUnload=0 RedirectIO=0 UseLastKnownVersion=0 Config_linux .ini Config_windows.ini P4Ddemo34Config.dpr demopython34config.fmx demopython34config.pas Edited March 14, 2021 by FranzB Share this post Link to post
pyscripter 689 Posted March 15, 2021 Regarding numpy on Linux if you use the default python 3 installation it would suffice to install numpy via pip: pip3 install numpy Share this post Link to post
FranzB 0 Posted March 16, 2021 (edited) yes , here comes a numpy short demo on windows .... execution is perfect I installed numpy via pip , did not need to set a DLL path see screen dump (on LINUX i don't have admin rights to install software using pip, will report results later - need other LINUX computer ) # demo script import matplotlib.pyplot as plt import numpy as np x = np.arange(0,8*np.pi,0.1) # start,stop,step y = np.cos(x) *1/(1+x) plt.plot(x,y) plt.show() Edited March 16, 2021 by FranzB Share this post Link to post
FranzB 0 Posted March 16, 2021 (edited) I still need a helping hand on one failure of the demo34Extend sample : Q : why is python text output not written to the output memo in Delphi app, python engine for sure is running, see post above procedure TForm_P4Deval.btn_CreatePythonEnviromentClick(Sender: TObject); begin // run this function prior to execute python code FPythonEngine := TPythonEngine.Create(nil); FPythonInOut := TPythonInputOutput.Create(nil); try FPythonInOut.UnicodeIO := True; FPythonInOut.RawOutput := True; FPythonInOut.OnSendUniData := PythonInputOutputSendUniData; {$IFDEF MSWINDOWS} FPyVersions := GetRegisteredPythonVersions; FPyVersions[0].AssignTo(FPythonEngine); {$ENDIF} FPythonEngineConfig.SetPythonengineValues(FPythonEngine); FPythonEngine.Loaddll; FPythonEngine.IO := FPythonInOut; { TPythonModule } FPythonModule1 := TPythonModule.Create(Self); FPythonModule1.Name := 'PythonModule1'; FPythonModule1.Engine := FPythonEngine; FPythonModule1.ModuleName := 'spam'; with FPythonModule1.Errors.Add do begin Name := 'PointError'; ErrorType := etClass; end; with FPythonModule1.Errors.Add do begin Name := 'EBadPoint'; ErrorType := etClass; ParentClass.Name := 'PointError'; end; except on E: Exception do writeln(E.ClassName, ': ', E.Message); end; end; procedure TForm_P4Deval.btn_FreePythonEnviromentClick(Sender: TObject); begin FPythonEngine.Free; FPythonInOut.Free; end; Config_windows.ini demopython34config.fmx demopython34config.pas P4Ddemo34Config.dpr Edited March 16, 2021 by FranzB add source files again Share this post Link to post
pyscripter 689 Posted March 16, 2021 Can't you debug? Is PythonInputOutputSendUniData called? If yes why it does not produce output? Share this post Link to post
pyscripter 689 Posted March 16, 2021 (edited) @FranzBHave you tried debugging? Edited March 16, 2021 by pyscripter Share this post Link to post
FranzB 0 Posted March 16, 2021 (edited) yes, tried debugging function TForm_P4Deval.PythonInputOutputSendUniData(Sender: TObject); is never called , even I assigned this function during the create components function for data output. I'm using FMX framework and lastest Delphi 10.4.2 , full working code is attached below FPythonInOut.UnicodeIO := True; FPythonInOut.RawOutput := True; FPythonInOut.OnSendUniData := PythonInputOutputSendUniData; {$IFDEF MSWINDOWS} FPyVersions := GetRegisteredPythonVersions; FPyVersions[0].AssignTo(FPythonEngine); {$ENDIF} FPythonEngineConfig.SetPythonengineValues(FPythonEngine); FPythonEngine.Loaddll; FPythonEngine.IO := FPythonInOut; Config_windows.ini demopython34config.fmx demopython34config.pas P4Ddemo34Config.dpr Edited March 16, 2021 by FranzB Share this post Link to post
Joseph MItzen 251 Posted March 16, 2021 20 hours ago, pyscripter said: Regarding numpy on Linux if you use the default python 3 installation it would suffice to install numpy via pip: pip3 install numpy If you're using the version of Python bundled with your Linux distro, you should either install NumPy from the Linux distro's package manager, use the "--user" option to install the package in the user's home directory (making sure the path is adjusted accordingly) or install into a virtual environment. Otherwise you could have problems with the distro package manager and pip conflicting. The distro package manager won't know pip installed NumPy to the system directory and could end up overwriting it in the future during an install or update. Share this post Link to post
pyscripter 689 Posted March 16, 2021 @FranzB Your need to assign the IO before loading the python dll. Otherwise the output is not redirected. FPythonEngine.IO := FPythonInOut; FPythonEngine.Loaddll; Share this post Link to post
FranzB 0 Posted March 17, 2021 (edited) good news : changed the code according your help and also created new *.ini file -> text is written to output file now 🙂 bad news : its difficult to find the correct parameter settings on a client computer system - there are to many options where python might be installed ...... https://github.com/pyscripter/python4delphi/wiki/FindingPython action : improve finding and setting of correct parameters ..... otherwise an app won't run on a different computer procedure TPythonEngineConfig.SetPythonengineValues(PythonEngine : TPythonEngine); var FPyVersions: TPythonVersions; begin /// /// convert this https://github.com/pyscripter/python4delphi/wiki/FindingPython /// into a small automatic setting of parameter /// PythonEngine.UseLastKnownVersion := Self.UseLastKnownVersion; if Self.UseLastKnownVersion then begin {$IFDEF MSWINDOWS} FPyVersions := GetRegisteredPythonVersions; FPyVersions[0].AssignTo(PythonEngine); {$ENDIF} {$IFDEF LINUX} /// see https://en.delphipraxis.net/topic/4700-getregisteredpythonversions-for-linux/ {$ENDIF} end else begin PythonEngine.DllName := Self.DllName; PythonEngine.DllPath := Self.DllPath; PythonEngine.AutoFinalize := Self.AutoFinalize; PythonEngine.AutoLoad := Self.AutoLoad; PythonEngine.AutoUnload := Self.AutoUnload; PythonEngine.RedirectIO := Self.RedirectIO; PythonEngine.UseWindowsConsole := Self.UseWindowsConsole; end; end; demo project : demopython34config.fmx demopython34config.pas P4Ddemo34Config.dpr screen dumps. Edited March 17, 2021 by FranzB Share this post Link to post