Jump to content
Registration disabled at the moment Read more... ×
Jacek Laskowski

TypeError: 'PythonDelphiVar' object cannot be interpreted as an integer"

Recommended Posts

My code in Delphi:


 

procedure TForm1.btnRunClick(Sender: TObject);
var
  PyEngine: TPythonEngine;
  PyHeight: Integer;
  PyIO: TPythonGUIInputOutput;
  PyModule: TPythonModule;
  PyWidth: Integer;
  VarHeight: TPythonDelphiVar;
  VarWidth: TPythonDelphiVar;
begin
  PyWidth := 80;
  PyHeight := 40;

  PyEngine := TPythonEngine.Create(nil);
  PyModule := TPythonModule.Create(nil);
  PyIO := TPythonGUIInputOutput.Create(nil);
  try
    PyIO.Output := mmoLog;
    PyEngine.IO := PyIO;
    PyEngine.DllName := 'python38.dll';
    PyEngine.DllPath := ExtractFilePath(Application.ExeName) + 'python\';
    PyEngine.UseLastKnownVersion := False;
    PyEngine.AutoLoad := False;
    PyEngine.AutoUnload := False;
    PyModule.Engine := PyEngine;
    PyModule.ModuleName := '__main__';
    try
     PyEngine.LoadDll;
    except
      on E: Exception do
      begin
        ShowMessage('Error at load Python: ' + E.Message);
        Exit;
      end;
    end;

    PyModule.Initialize;
    PyEngine.PyRun_SimpleString('import builtins; __builtins__ = builtins');

    VarWidth := TPythonDelphiVar.Create(nil);
    VarWidth.Engine := PyEngine;
    VarWidth.Module := '__main__';
    VarWidth.VarName := 'WIDTH';
    VarWidth.Initialize;
    VarWidth.Value := PyWidth;

    VarHeight := TPythonDelphiVar.Create(nil);
    VarHeight.Engine := PyEngine;
    VarHeight.Module := '__main__';
    VarHeight.VarName := 'HEIGHT';
    VarHeight.Initialize;
    VarHeight.Value := PyHeight;

    PyEngine.ExecStrings(mmoSource.Lines, '__main__');

  finally
    VarWidth.Free;
    VarHeight.Free;
    PyIO.Free;
    PyModule.Free;
    PyEngine.Free;
  end;
end;

And python script:

 

canvas = [[' ' for _ in range(WIDTH)] for _ in range(HEIGHT)]

 

After run this I get an error:

 

Traceback (most recent call last):
  File "__main__", line 1, in <module>
TypeError: 'PythonDelphiVar' object cannot be interpreted as an integer

 

I want to pass two variables WIDTH and HEIGHT to the script. What am I doing incorrectly?

 

 

Share this post


Link to post
31 minutes ago, Jacek Laskowski said:

VarWidth.Initialize;

VarWidth.Value := PyWidth;

Does reversing the order help?

 

    VarWidth.Value := PyWidth;
    VarWidth.Initialize;

 

Share this post


Link to post
14 minutes ago, pyscripter said:

Does reversing the order help? 

 


    VarWidth.Value := PyWidth;
    VarWidth.Initialize;

 

No, then I get an error on assignment line:

 

VarWidth.Value := PyWidth; <-- here I get an error
VarWidth.Initialize;

 

Error message:

 

ThreadId=4800
ProcessId=38
ThreadName=""
ExceptionMessage="No variable was created"
ExceptionName="Exception"
ExceptionDisplayName="Exception"
ExceptionAddress=751DC5AF
FileName=<not available>
LineNumber=<not available>
ExceptionObject=033BE960
Classes=[Exception,TObject]

 

Error is raised here:

 

procedure TPythonDelphiVar.SetValue( const val : Variant );
begin
  if Assigned( FVarObject ) then
    with TPyVar(PythonToDelphi(FVarObject)) do
      SetValueFromVariant(val)
  else
    raise Exception.Create(SVarNotCreated); <-- here
end;

 

 

--- edit

 

When I change the python script to:

 

print(HEIGHT, WIDTH)

 

then all run ok, and in the log I get:

 

<PythonDelphiVar: 40> <PythonDelphiVar: 60>

 

Edited by Jacek Laskowski
adding

Share this post


Link to post
canvas = [[' ' for _ in range(WIDTH)] for _ in range(HEIGHT)]

needs to be

canvas = [[' ' for _ in range(WIDTH.Value)] for _ in range(HEIGHT.Value)]
  • Thanks 1

Share this post


Link to post
10 hours ago, pyscripter said:

canvas = [[' ' for _ in range(WIDTH)] for _ in range(HEIGHT)]

needs to be


canvas = [[' ' for _ in range(WIDTH.Value)] for _ in range(HEIGHT.Value)]

Thanks! This was the cause of the problem.

 

By the way, I have a question. This script in Python is quite a bit bigger, here I have given a minimal causing the error, the script uses WIDTH and HEIGHT variables. Before I passed these variables from Delphi they were defined at the beginning of the script by a simple assignment:

HEIGHT = 40
WIDTH = 60

However, when I added these two variables (TPythonDelphiVar) from the Delphi side, and the module (TPythonModule) object it stopped working many keywords including “import” and only adding a line in the Delphi code:

 

PyEngine.PyRun_SimpleString(‘import builtins; __builtins__ = builtins’);

...resulted in correct working. What is the result of this?

 

Why is it that without variable and module objects, the contents of “builtins” are automatically available, but disappear after adding them?

 

Edited by Jacek Laskowski

Share this post


Link to post

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×