Erreur Interface Lazarus-Python-mpmath
Bonjour,
J'essaie d'interfacer Python avec Lazarus, essentiellement dans le but d'utiliser la librairie mpmath pour le calcul en multi-précision
J'utilise la dernière version 3.8. Apparemment, Lazarus refuse la commande " GetPythonEngine.Initialize; "
J'ai effectué les deux essais décrits ci-dessous sans succès.
Pouvez-vous m'aider ? Merci
Python4Delphi et mpmath ont été téléchargés et installés et ajoutés au projet Lazarus
PythonEngine apparaît correctement dans la liste des fonctions de l'éditeur de source
J'ai ajouté dans la fonction principale "Calcul"
Code:
1 2 3 4 5 6 7 8 9 10
| uses
PythonEngine, VarPyth;
{ TForm1 }
TForm1 = class(TForm)
...
PythonEngine: TPythonEngine;
...
End; |
Premier essai
***********
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| procedure TForm1.FormCreate(Sender: TObject);
Var
a:String;
Py:Variant;
begin
GetPythonEngine.Initialize;
Py:=Import('mpmath');
Py.mp.dps:=50;
a:=Py.str(Py.mpf(2)**Py.mpf('0.5'));
Form1.Memo2.Lines.Add(a);
end; |
Citation:
Envoyé par Erreur
calcul.pas(151,20) Error: identifier idents no member "Initialize"
Deuxième essai
**************
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| procedure TForm1.FormCreate(Sender: TObject);
Var
a:String;
Py:Variant;
begin
with GetPythonEngine do
begin
// GetPythonEngine.Initialize;
Py:=Import('mpmath');
Py.mp.dps:=50;
a:=Py.str(Py.mpf(2)**Py.mpf('0.5'));
Form1.Memo2.Lines.Add(a);
end;
end; |
Citation:
Envoyé par Erreur
Le projet project a levé une classe d'exception "Exception" avec le message :
No Python engine was created
dans le fichier "..\..\Source\PythonEngine.pas à la ligne 9764
Code:
1 2 3 4 5 6 7 8
| function GetPythonEngine : TPythonEngine;
begin
if not Assigned( gPythonEngine ) then
raise Exception.Create(SCannotCreatePythonEngine); // <== (9764)
if not gPythonEngine.Finalizing and not gPythonEngine.Initialized then
raise Exception.Create(SCannotInitPythonEngine);
Result := gPythonEngine;
end; |
Interface Lazarus Python mpmath
Bonsoir
Merci Jurassic Pork pour ta réponse. J'ai suivi les instructions et reproduit le script (voir ci-dessous).
Tout va bien à la compilation. Juste un petit problème : le résultat ne s'affiche pas, GetPythonEngine.ExecStrings(script);
ne fournit pas une variable string. Comment récupérer le résultat et l'écrire dans Memo1 ?
C'est probablement évident mais je ne trouve pas.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| procedure TForm1.Button1Click(Sender: TObject);
Var
script:TStringList;
begin
script:= TStringList.Create();
Memo1.Clear;
Memo1.Append('Calculate 2**0.5');
script.Add('from mpmath import *');
script.Add('mp.dps = 50');
script.Add('print(mpf(2)**mpf(0.5))');
GetPythonEngine.ExecStrings(script);
script.Free;
end; |
Interface Lazarus Python mpmath
Bonsoir
Désolé, je viens de trouver ce qui manquait pour écrire le résultat dans un mémo.
Il suffisait d'ajouter la propriété Output de PythonGUIInputOutput1 = Memo1.
Pour moi, le problème reste le même : Je voudrais vraiment obtenir la réponse de mpf(2)**mpf(0.5)
sous forme d'une variable de type string
Interface Lazarus Python mpmath
Un grand merci JP, c'est parfait