Juan C.Cilleruelo 16 Posted January 12, 2023 I'm having the following problem with TPythonDelphiVar component. I use the next method to create one instance of this component on-the-fly: function TForm1.NewVariable(AVarName :string):TPythonDelphiVar; begin Result := TPythonDelphiVar.Create(nil); Result.Engine := PythonEngine; Result.Module := '__main__'; Result.VarName := AVarName; Result.Initialize; end; I have a Private variable declared in the TForm1class, like this: public FVariable :TPythonDelphiVar; And I use the next code to create on-the-fly my variable QUANTITY: FVariable := NewVariable('QUANTITY'); FVariable.Value := 1.0; All seems ok, but when I execute the program, and I try to print the value of QUANTITY: (Phyton script). print(QUANTITY) I get the following message error in a message error window: "NameError: 'QUANTITY' is not defined. " And in the output memo of the PythonEngine, the next one: Traceback (most recent call last): File "<string>", line 13, in <module> NameError: name 'QUANTITY' is not defined I can't understand why creating the TPythonDelphiVar on-the-fly doesn't work while creating the same component in design time works well. Do you understand what the problem is? Share this post Link to post
Juan C.Cilleruelo 16 Posted January 12, 2023 The same problem with the class TPythonModule. If I create in visual form, including it on a form, it works well. If I create it on the fly, the next statements FPythonModule.AddDelphiMethod('ComponentCount', ComponentCount, 'ComponentCount'); FPythonModule.AddDelphiMethod('GetPRICE' , GetPRICE , 'GetPRICE' ); run, but when I try to use ComponentCount in my Python Script, does not exist. what's happening? Share this post Link to post
AndreasSt 0 Posted March 4, 2024 Hello Juan, Do you have fixed this issue or do you have another solution? thanks Regards Andreas Share this post Link to post
pyscripter 728 Posted March 4, 2024 If you create an Engine client (e.g. TPythonDelphiVar TPythonModule) after the engine is initialized, you should call the client Initialize method. If the client is created before the engine is initialized, you should not call Initialize. Share this post Link to post
Paschalis 0 Posted 11 hours ago (edited) Hello @Juan C.Cilleruelo and @AndreasSt, I had the same issue and managed to retrieve the TPythonDelphiVar like this. function TPythonViewModel.GetDelphiVar: string; var PyResult: PPyObject; begin FPythonDelphiVar.Engine := FPythonEngine; FPythonDelphiVar.Module := '__main__'; FPythonDelphiVar.VarName := 'DelphiVar'; FPythonDelphiVar.Initialize; FPythonEngine.ExecString('DelphiVar ="Hello"'); PyResult := FPythonEngine.EvalString('DelphiVar'); if PyResult <> nil then Result := FPythonEngine.PyObjectAsString(PyResult) else Result := 'Error: EvalString returned nil'; end; Edited 11 hours ago by Paschalis Share this post Link to post