Tag: ironpython

为什么我不能将我的C#类型导入IronPython?

我在我编写的C#库中有一些类型,例如: namespace SprocGenerator.Generators { public class DeleteGenerator : GeneratorBase { public DeleteGenerator(string databaseName, string tableName) : base(databaseName, tableName) 我想在IronPython脚本中使用它们: import clr import sys clr.AddReferenceToFile(“SprocGenerator.dll”) # problem happens here: from SprocGenerator.Generators import * generator = DeleteGenerator(“a”, “b”) 当评论下面的行发生时,我得到: ImportError: No module named Generators 我已经validation了我正在加载的文件是我期望的重命名它并且在尝试加载程序集时validation脚本会引发错误。 我通过Reflectorvalidation了命名空间在程序集中。 我还尝试指定一个完全限定的类名来解决我的导入问题,例如 generator = SprocGenerator.Generators.DeleteGenerator(“a”, “b”) 但我得到: NameError: name ‘SprocGenerator’ is not […]

集成C#,F#,IronPython和IronRuby

我被告知,由C#和F#源组成的汇编文件可以互操作,因为它们被编译成.NET程序集。 Q1:这是否意味着C#可以调用F#函数就像它们是C#函数一样? Q2:IronPython和IronRuby怎么样? 我没有看到IronPython / IronRuby中的任何程序集dll。 问题3:有没有简单的方法可以使用C#或F#中的IronPython / IronRuby函数? 任何示例代码都会很棒。

IronPython:意外的令牌’来自’

我使用IronPython从.net运行python脚本,下面是我的python脚本 import tensorflow as tf print(‘Tensorflow Imported’) 下面是C#代码 using System; using System.Text; using System.IO; using IronPython.Hosting; using System.Collections.Generic; using Microsoft.Scripting.Hosting; namespace ConsoleApplication1 { class Program { private static void Main() { var py = Python.CreateEngine(); List searchPaths = new List(); searchPaths.Add(@”C:\Users\Admin\AppData\Local\Programs\Python\Python35\Lib)”); searchPaths.Add(@”C:\Users\Admin\AppData\Local\Programs\Python\Python35\Lib\site-packages)”); py.SetSearchPaths(searchPaths); try { py.ExecuteFile(“script.py”); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } […]

使用IronPython在C#中运行特定的Python函数

到目前为止,我有一个简单的类包装python引擎(IronPython)供我使用。 虽然代码看起来很大,但它非常简单,所以我在这里复制它以便更清楚地解决我的问题。 这是代码: public class PythonInstance { ScriptEngine engine; ScriptScope scope; ScriptSource source; public PythonInstance() { engine = Python.CreateEngine(); scope = engine.CreateScope(); } public void LoadCode(string code) { source = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.Statements); source.Compile(); } public void SetVariable(string key, dynamic variable) { scope.SetVariable(key, variable); } public void RunCode() { source.Execute(scope); } public void CallFunction(string function) { […]

使用IronPython简化为C#应用程序编写的DSL

感谢前一个问题的建议,我正忙着尝试使用IronPython,IronRuby和Boo为我的C#应用​​程序创建一个DSL。 第一步是IronPython,因为用户和知识库较大。 如果我能在这里找到合适的东西,我可以停下来。 这是我的问题: 我希望我的IronPython脚本可以访问名为Lib的类中的函数。 现在我可以将程序集添加到IronPython运行时并通过执行我创建的作用域中的语句来导入该类: // load ‘ScriptLib’ assembly Assembly libraryAssembly = Assembly.LoadFile(libraryPath); _runtime.LoadAssembly(libraryAssembly); // import ‘Lib’ class from ‘ScriptLib’ ScriptSource imports = _engine.CreateScriptSourceFromString(“from ScriptLib import Lib”, SourceCodeKind.Statements); imports.Execute(_scope); // run .py script: ScriptSource script = _engine.CreateScriptSourceFromFile(scriptPath); script.Execute(_scope); 如果我想运行Lib :: PrintHello,这只是一个hello world style语句,我的Python脚本包含: Lib.PrintHello() 或(如果它不是静态的): library = new Lib() library.PrintHello() 如何更改我的环境,以便我可以在Python脚本中获得如下的基本语句: PrintHello TurnOnPower VerifyFrequency […]

来自C#(使用SciPy)的IronPython调用因ImportException失败:“没有名为mtrand的模块”

我有一个python库,我试图通过IronPython(v2.7 RC1 [2.7.0.30])从C#应用程序调用。 该库非常广泛地使用NumPy和SciPy,当从命令行使用ipy运行时,它可以与SciPy和NumPy for .NET一起使用,如下所示: ipy.exe -X:Frames file_from_lib_importing_numpy.py 但是,当我使用下面的代码从C#调用IronPython时,会抛出exception: ImportException “No module named mtrand” at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value) at IronPython.Runtime.Operations.PythonOps.ImportStar(CodeContext context, String fullName, Int32 level) at Microsoft.Scripting.Interpreter.ActionCallInstruction3.Run(InterpretedFrame frame) … at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink) at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope) at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteFile(String path) at Microsoft.Scripting.Hosting.ScriptRuntime.ExecuteFile(String path) at Microsoft.Scripting.Hosting.ScriptRuntime.UseFile(String path) … 调用IronPython的C#代码(它的一部分)如下: ScriptEngine _engine; var opts = new Dictionary(); […]

C#4.0:将动态转换为静态

这是一个与我在这里问到的另一个相关的分支问题。 我把它分开是因为它真的是一个子问题: 我很难将类型为dynamic的对象转换为另一个(已知的)静态类型。 我有一个执行此操作的ironPython脚本: import clr clr.AddReference(“System”) from System import * def GetBclUri(): return Uri(“http://google.com”) 请注意,它只是新建了一个BCL System.Uri类型并返回它 。 所以我知道返回对象的静态类型 。 现在在C#land,我正在新建脚本托管的东西,并调用此getter返回Uri对象: dynamic uri = scriptEngine.GetBclUri(); System.Uri u = uri as System.Uri; // casts the dynamic to static fine 没问题。 我现在可以使用强类型的Uri对象,就像它最初静态实例化一样。 然而…. 现在我想定义我自己的C#类,它将在动态范围内进行新建,就像我对Uri一样。 我简单的C#类: namespace Entity { public class TestPy // stupid simple test class of […]

C#/ IronPython与共享C#类库互操作

我正在尝试使用IronPython作为C#GUI和一些C#库之间的中介,以便它可以在编译后编写脚本。 我有一个类库DLL,由GUI和python使用,是这样的: namespace MyLib { public class MyClass { public string Name { get; set; } public MyClass(string name) { this.Name = name; } } } IronPython代码如下: import clr clr.AddReferenceToFile(r”MyLib.dll”) from MyLib import MyClass ReturnObject = MyClass(“Test”) 然后,在C#中我将其称为如下: ScriptEngine engine = Python.CreateEngine(); ScriptScope scope = null; scope = engine.CreateScope(); ScriptSource source = engine.CreateScriptSourceFromFile(“Script.py”); source.Execute(scope); MyClass […]

IronPython DLR; 将参数传递给编译代码?

我目前正在使用DLR创建和执行一个简单的python计算: ScriptRuntime runtime = Python.CreateRuntime(); ScriptEngine engine = runtime.GetEngine(“py”); MemoryStream ms = new MemoryStream(); runtime.IO.SetOutput(ms, new StreamWriter(ms)); ScriptSource ss = engine.CreateScriptSourceFromString(“print 1+1”, SourceCodeKind.InteractiveCode); CompiledCode cc = ss.Compile(); cc.Execute(); int length = (int)ms.Length; Byte[] bytes = new Byte[length]; ms.Seek(0, SeekOrigin.Begin); ms.Read(bytes, 0, (int)ms.Length); string result = Encoding.GetEncoding(“utf-8”).GetString(bytes, 0, (int)ms.Length); Console.WriteLine(result); 其中“2”打印到控制台,但是; 我想得到1 + 1的结果,而不必打印它(因为这似乎是一个昂贵的操作)。 我将cc.Execute()的结果赋给的任何内容都为null。 有没有其他方法可以从Execute()获取结果变量? […]

使用mono调用C#中的IronPython对象

我有以下IronPython代码。 class Hello: def __init__(self): pass def add(self, x, y): return (x+y) 我需要从C#调用它,我想出了以下代码。 using System; using IronPython.Hosting; using IronPython.Runtime; using IronPython; using Microsoft.Scripting.Hosting; using Microsoft.Scripting; class Hello { public static void Main() { ScriptEngine engine = Python.CreateEngine(); ScriptSource script = engine.CreateScriptFromSourceFile(“myPythonScript.py”); ScriptScope scope = engine.CreateScope(); script.Execute(scope); } } 复制IronPython.dll后,我运行以下命令。 (我试图运行gacutil,但是我遇到了一些错误 。 dmcs /r:IronPython.dll callipy.cs 但是我得到了一些错误信息,如下所示。 […]