RSG 0 Posted June 24, 2023 I am using Python4Delphi in my app to allow users to incorporate Octave (open-source Matlab) scripts via Oct2Py - very cool capabilities here! While not central to what I am doing, I'm hoping to provide a sort of Python console similar to running an interactive Python session from a shell/command line. This is rather trivial for simple one-line code; simply pass the user-provided string to ExecString() and it works. However, if you try to do something like the following trivial code, passing each line to ExecString, the first two lines work, but the third throws an exception as shown below. pi = 3.1415926 print(pi) if pi > 0.0: print('pi is positive') else: print('pi is negative') It appears that ExecString really expects a single Python statement in one string. I suppose this isn't terribly surprising, so maybe my goal is not realizable without a lot of work, unless I am missing something that can make this possible. Any ideas? Share this post Link to post
pyscripter 694 Posted June 24, 2023 Python code needs to be properly indented (see for instance Python Indentation (w3schools.com)). Otherwise you will get the IndentationError you show above. If you want to built your own interpreter, a good starting point is the InteractiveInterpeter class of the code module. The runsource method will tell you whether the input is incomplete and more input is required. Share this post Link to post
Joseph MItzen 252 Posted June 25, 2023 10 hours ago, RSG said: It appears that ExecString really expects a single Python statement in one string. I suppose this isn't terribly surprising, so maybe my goal is not realizable without a lot of work, unless I am missing something that can make this possible. Any ideas? This would make Python users recoil in horror, but there are ways to turn Python code into single lines! https://jagt.github.io/python-single-line-convert/ I don't know anything about Python4Delphi, but see if exec("""\npi = 3.1415926\nprint(pi)\nif pi > 0.0:\n print('pi is positive')\nelse:\n print('pi is negative')\n""") runs. Maybe it could run without the exec if you turned the \n instances into #13s. I don't know what your app is, but being able to use Octave scripts in it is pretty cool. Share this post Link to post
David Heffernan 2353 Posted June 25, 2023 6 hours ago, Joseph MItzen said: This would make Python users recoil in horror, but there are ways to turn Python code into single lines! Not much use for a REPL Share this post Link to post
David Heffernan 2353 Posted June 25, 2023 Is it really the best way to go delphi to python to octave? Must be possible to go straight to octave but far better to use python rather than octave/matlab. Share this post Link to post
RSG 0 Posted July 15, 2023 On 6/25/2023 at 4:51 AM, David Heffernan said: Is it really the best way to go delphi to python to octave? I don't know if it is or not, but I already use Python scripting elsewhere... On 6/25/2023 at 4:51 AM, David Heffernan said: Must be possible to go straight to octave Maybe, but it isn't possible to go straight from Delphi to Matlab without a C "wrapper" DLL, so I wouldn't bet on it!! On 6/25/2023 at 4:51 AM, David Heffernan said: but far better to use python rather than octave/matlab. True, but I haven't been able to convince my client of that! They are scientists that use Matlab heavily, and are willing to go as far as Octave, but they refuse Python! Share this post Link to post
RSG 0 Posted July 15, 2023 (edited) On 6/24/2023 at 4:02 PM, pyscripter said: Python code needs to be properly indented (see for instance Python Indentation (w3schools.com)). Otherwise you will get the IndentationError you show above. If you want to built your own interpreter, a good starting point is the InteractiveInterpeter class of the code module. The runsource method will tell you whether the input is incomplete and more input is required. Of course, I understood the indentation requirement - sorry if I didn't make that clear. That said, your other paragraph is much more to the point - thanks for the insight! Edited July 15, 2023 by RSG Share this post Link to post