Interraction C# et Python
Bonjour,
Je suis chargé de découvrir comment fonctionne l'interraction entre le Python et le C# et j'ai quelque petits soucis dans mes tests.
J'ai installé IronPython, PTVS (PythonTool), j'ai également ajouté les références IronPython.dll et Microsoft.Scripting.dll. Pourtant je ne parviens même pas à afficher un simple print Python depuis le code C#.
Voici ce que j'ai fait avec en commentaire les essais non-concluants (merci de votre aide):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IronPython.Hosting;
//using Microsoft.CSharp.RuntimeBinder;
//using Microsoft.Scripting.Hosting.ScriptEngine;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var engine = Python.CreateEngine();
/*var RandPath = Path.GetFullPath(@"random.py");
var RandAsm = Assembly.LoadFile(RandPath);
engine.Runtime.LoadAssembly(RandAsm);
dynamic random = engine.ImportModule("random");*/
//Python.CreateEngine();
//dynamic scope = engine.CreateScope();
//engine.ExecuteFile(@"C:\Users\apprenti\Desktop\random.py",scope);
//engine.ExecuteFile(@"C:\Users\apprenti\Desktop\random.py");
//engine.Execute(WindowsFormsApplication1.Properties.random);
int y=0;
var theScript = @"def Calc(y): #Ceci est le code Python
y=1+1
#print(y)
Calc()";
engine.Execute(theScript);
MessageBox.Show(y.ToString()); /*la valeur retournée est 0
alors que je cherche à obtenir 2*/ |