Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Run ironpython code, no output
#1
hello everyone,
Executing Python code in qm3 will be very simple. I tried the following code, but no output, why? Thanks in advance

Download and unzip Libraries.zip , and put it in the folder C:\program files\qm3\libraries

C# code:
// script "ironpython.cs" /*/ r Libraries\IronPython.dll; r Libraries\Microsoft.Scripting.dll; r Libraries\Microsoft.Dynamic.dll; /*/ //.
using Au; using System; using Microsoft.Scripting.Hosting;
partial class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) { //;;;

ScriptEngine pythonEngine = IronPython.Hosting.Python.CreateEngine();
ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromString("print 'Hello World!'");
pythonScript.Execute();

}
}


Attached Files
.zip   Libraries.zip (Size: 1.52 MB / Downloads: 336)
#2
C# code:
// script "" /*/ r Libraries\IronPython.dll; r Libraries\Microsoft.Scripting.dll; r Libraries\Microsoft.Dynamic.dll; /*/ //.
using Au; using System; using Microsoft.Scripting.Hosting;
partial class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) { //;;;

AOutput.RedirectConsoleOutput=true;

ScriptEngine pythonEngine = IronPython.Hosting.Python.CreateEngine();
ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromString("print 'Hello World!'");
pythonScript.Execute();

}
}
#3
@Gintaras thanks

I have a problem. Where is the script file for currently edited? Huh
In the following code, I need to put two Python files in the same directory


C# code:
// script "ipy_code2.cs" /*/ r Libraries\IronPython.dll; r Libraries\Microsoft.Scripting.dll; r Libraries\Microsoft.Dynamic.dll; /*/ //.
using Au; using System; using Microsoft.Scripting.Hosting;
using System.Collections.Generic;
partial class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) { //;;;
AOutput.RedirectConsoleOutput=true;

ScriptEngine pythonEngine = IronPython.Hosting.Python.CreateEngine();

// Print the default search paths
System.Console.Out.WriteLine("Search paths:");
ICollection<string> searchPaths = pythonEngine.GetSearchPaths();
foreach (string path in searchPaths)
{
    System.Console.Out.WriteLine(path);
}
System.Console.Out.WriteLine();

// Now modify the search paths to include the directory from
// which we execute the script
searchPaths.Add("..\\..");
pythonEngine.SetSearchPaths(searchPaths);

// Execute the script
// We execute this script from Visual Studio so the program will executed from bin\Debug or bin\Release
Microsoft.Scripting.Hosting.ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromFile("..\\..\\HelloWorld.py");
pythonScript.Execute();
}
}

Found a small problem, com lists, if  do not select item, can not close it

[Image: QQ-20210108084757.png]

I found the file path: Smile
C:\Users\Administrator\Documents\Aedit\Main\files


Attached Files
.zip   py files.zip (Size: 409 bytes / Downloads: 278)
#4
The following code, after execution, no result
Support for. Net5,  in the release description, I chose netcoreapp3.1  



[Image: pic.png]
[Image: pic2.png]

C# code:
// script "ipy_code3.cs" /*/ r Libraries\IronPython.dll; r Libraries\Microsoft.Scripting.dll; r Libraries\Microsoft.Dynamic.dll; /*/ //.
using Au; using System; using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using System.Collections.Generic;
using System.Linq;
partial class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) { //;;;
AOutput.RedirectConsoleOutput=true;

ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile(@"C:\Users\Administrator\Desktop\script.py");
ScriptScope scope = engine.CreateScope();

ObjectOperations op = engine.Operations;

source.Execute(scope); // class object created
object classObject = scope.GetVariable("MyClass"); // get the class object
object instance = op.Invoke(classObject); // create the instance
object method = op.GetMember(instance, "myMethod"); // get a method

List<string> result = ((IList<object>)op.Invoke(method)).Cast<string>().ToList();

Console.WriteLine(result.ToString());
Console.Read();

}
}


Attached Files
.zip   Libraries_netcoreapp3.1.zip (Size: 1.88 MB / Downloads: 306)
.zip   script.py.zip (Size: 234 bytes / Downloads: 391)
#5
With dlls from your last post the script works. Replaced the last 2 lines with AOutput.Write.

C# code:
// script "ipy_code3.cs" /*/ r Libraries\IronPython.dll; r Libraries\Microsoft.Scripting.dll; r Libraries\Microsoft.Dynamic.dll; /*/ //.
using Au; using System; using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using System.Collections.Generic;
using System.Linq;
partial class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) { //;;;
AOutput.RedirectConsoleOutput=true;

ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromString(
@"class MyClass(object):
    def myMethod(self):
        return ['a','list','of','strings']"
);
ScriptScope scope = engine.CreateScope();

ObjectOperations op = engine.Operations;

source.Execute(scope); // class object created
object classObject = scope.GetVariable("MyClass"); // get the class object
object instance = op.Invoke(classObject); // create the instance
object method = op.GetMember(instance, "myMethod"); // get a method

List<string> result = ((IList<object>)op.Invoke(method)).Cast<string>().ToList();

AOutput.Write(result);

}
}
#6
If py file is imported into QM3 workspace with drag&drop (Explorer to QM3) or File menu.

C# code:
// script "ipy_code3.cs" /*/ r Libraries\IronPython.dll; r Libraries\Microsoft.Scripting.dll; r Libraries\Microsoft.Dynamic.dll; /*/ //.
using Au; using System; using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using System.Collections.Generic;
using System.Linq;
partial class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) { //;;;
AOutput.RedirectConsoleOutput=true;

ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile(ScriptFolder() + @"\script.py");
ScriptScope scope = engine.CreateScope();

ObjectOperations op = engine.Operations;

source.Execute(scope); // class object created
object classObject = scope.GetVariable("MyClass"); // get the class object
object instance = op.Invoke(classObject); // create the instance
object method = op.GetMember(instance, "myMethod"); // get a method

List<string> result = ((IList<object>)op.Invoke(method)).Cast<string>().ToList();

AOutput.Write(result);

}


/// <summary>
/// Gets full path of caller's C# source file.
/// </summary>
/// <param name="f">Don't use.</param>
public static string ScriptFile([System.Runtime.CompilerServices.CallerFilePath] string f = null) => f;

/// <summary>
/// Gets filename of caller's C# source file.
/// </summary>
/// <param name="f">Don't use.</param>
public static string ScriptFilename([System.Runtime.CompilerServices.CallerFilePath] string f = null) => APath.GetName(f);

/// <summary>
/// Gets folder path of caller's C# source file.
/// </summary>
/// <param name="f">Don't use.</param>
public static string ScriptFolder([System.Runtime.CompilerServices.CallerFilePath] string f = null) => APath.GetDirectory(f);
}
#7
Also you can add py files to script assembly resources. In Properties dialog.

C# code:
// script "ipy_code3.cs" /*/ r Libraries\IronPython.dll; r Libraries\Microsoft.Scripting.dll; r Libraries\Microsoft.Dynamic.dll; resource script.py /string; /*/ //.
using Au; using System; using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using System.Collections.Generic;
using System.Linq;
partial class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) { //;;;
AOutput.RedirectConsoleOutput=true;

ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromString(Au.Util.AResources.GetString("script.py")); //or "subfolder/script.py"
ScriptScope scope = engine.CreateScope();

ObjectOperations op = engine.Operations;

source.Execute(scope); // class object created
object classObject = scope.GetVariable("MyClass"); // get the class object
object instance = op.Invoke(classObject); // create the instance
object method = op.GetMember(instance, "myMethod"); // get a method

List<string> result = ((IList<object>)op.Invoke(method)).Cast<string>().ToList();

AOutput.Write(result);

}
}
#8
@Gintaras 

Thanks !
Using ironpython to execute Python code is very convenient, Qm3 is very powerful
Smile


Forum Jump:


Users browsing this thread: 2 Guest(s)