Tag: ironpython

如何使用IronPython将参数传递给Python脚本

我有以下C#代码,我从C#调用python脚本: using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Windows.Forms; using System.Linq; using System.Text; using System.Threading.Tasks; using IronPython.Hosting; using Microsoft.Scripting.Hosting; using IronPython.Runtime; namespace RunPython { class Program { static void Main(string[] args) { ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null); ScriptRuntime runtime = new ScriptRuntime(setup); ScriptEngine engine = Python.GetEngine(runtime); ScriptSource source = engine.CreateScriptSourceFromFile(“HelloWorld.py”); ScriptScope scope = engine.CreateScope(); […]

IronPython – 在C#4.0应用程序中从字符串加载脚本

我有以下代码(只是一个测试): var engine = Python.CreateEngine(); var runtime = engine.Runtime; try { dynamic test = runtime.UseFile(@”d:\test.py”); test.SetVariable(“y”, 4); test.SetVariable(“client”, UISession.ControllerClient); test.Simple(); } catch (Exception ex) { var eo = engine.GetService(); Console.WriteLine(eo.FormatException(ex)); } 但我想从字符串加载脚本。

IronPython ImportException:没有名为logging的模块

我让ironpython在mono上工作正常,但它没有导入logging模块。 执行此代码: ScriptEngine engine = Python.CreateEngine(); dynamic logging = engine.ImportModule(“logging”); 产生以下错误: IronPython.Runtime.Exceptions.ImportException: No module named logging 我包含的IronPython程序集是最新的:IronPython.Modules.dll,Microsoft.Dynamic.dll,Microsoft.Scripting.dll,Microsoft.Scripting.Metadata.dll。 如何在Ironpython中使用日志记录模块?

使用ScriptEngine(.NET 3.5)在C#中执行Selenium pythonunit testing时遇到问题

第一次海报。 我转向关于堆栈溢出的第一个问题,因为我找到了很少的资源来寻找答案。 我正在寻找从C#应用程序执行Selenium python测试。 我不想每次都要编译C#Selenium测试; 我想利用IronPython脚本进行动态selenium测试。 (注意:我没有Python或ScriptEngine等经验。) Selenium以下列forms在python中输出unit testing: from selenium import selenium import unittest class TestBlah(unittest.TestCase): def setUp(self): self.selenium = selenium(…) self.selenium.start() def test_blah(self): sel = self.selenium sel.open(“http://www.google.com/webhp”) sel.type(“q”, “hello world”) sel.click(“btnG”) sel.wait_for_page_to_load(5000) self.assertEqual(“hello world – Google Search”, sel.get_title()) print “done” def tearDown(self): self.selenium.stop() if __name__ == “__main__”: unittest.main() 我可以使用ipy.exe从命令行运行,没问题: ipy test_google.py 我可以看到Selenium Server启动一个firefox浏览器实例并运行测试。 […]

IronPython无法导入模块os

所以我有一个基本的ZIPPED IronPython(2.6或2.6.1),我只需解压缩,启动ipy.exe,输入“import os”并按Enter键。 发生以下输出: Traceback (most recent call last): File “”, line 1, in ImportError: No module named os 即使我先导入clr,它也不起作用。 怎么办? 我用谷歌搜索了这个问题,但没有相关的答案。 最接近的想法是这个(没有用): import clr clr.AddReference(“IronPython”) clr.AddReference(“IronPython.Modules”) import os

从IronPython实例化自定义C#类

有没有办法让一个类可用于IronPython脚本,以便我可以在代码中创建对象? 例如,如果我有一个类,我希望能够从C#代码中定义的名为MyClass的脚本中实例化,如下所示: public class MyClass { string text; public MyClass(string text) { this.text = text; } public void Write() { Console.WriteLine(text); } } 我怎么能在Python脚本中这样做? obj = MyClass(“Hello, World!”) obj.Write() 谢谢!

为什么我会收到此.NET错误 – “TypeError:expected List ,得到List ”

我重构了一些代码,现在我在调用函数时遇到了这个错误。 但是一切似乎都很好,我甚至比较了旧代码和新代码之间的failing_argument.GetType().AssemblyQualifiedName ,它们是相同的。 什么想法可能是错的? 函数的调用是在IronPython代码中,函数是在C#代码中(在重构过程中没有改变的程序集)。 什么样的事情会产生这个错误? 编辑:完整的IronPython回溯: Traceback (most recent call last): File “D:\Work\Framework\python\ide\tab_manager.py”, line 57, in add_chart_tab chart_tab = ChartTab(self.__main_window, self, tab_item, name, chart_descriptor) File “D:\Work\Framework\python\ide\chart_tab.py”, line 64, in __init__ self.__chart = Chart(self, self.__gui_cfg, self.__base_cfg, self.__chart_descriptor, self.__scroll_bar) File “D:\Work\Framework\python\ide\chart.py”, line 57, in __init__ self.update_topology(empty=False) File “D:\Work\Framework\python\ide\chart.py”, line 93, in update_topology self.update_config() File “D:\Work\Framework\python\ide\chart.py”, line […]

将IronPython方法分配给C#委托

我有一个C#类看起来有点像: public class MyClass { private Func processMethod = (ds) => { //default method for the class } public Func ProcessMethod { get{ return processMethod; } set{ processMethod = value; } } /* Other details elided */ } 我有一个IronPython脚本,可以在应用程序中运行 from MyApp import myObj #instance of MyClass def OtherMethod(ds): if ds.Data.Length > 0 : quot = […]

在C#应用程序中集成python

我有以下问题:我有一个用python编写的旧应用程序。 这个应用程序允许用户指定将执行的小python步骤,python步骤基本上是小python脚本,我称之为步骤,因为此应用程序的执行涉及其他步骤,如从命令行执行某些操作。 这些python步骤作为python代码存储在xml文件中。 现在我想使用C#.NET重写应用程序。 做这样的事情是否有最佳实践解决方案? 我不想将python作为外部程序调用并将实际的python步骤(脚本)传递给python解释器 – 我想要内置的东西。我只是遇到了IronPython和.NET python,但我不太确定使用哪一个。 我想为小脚本实现某种调试,这就是为什么调用解释器解决方案是不可接受的。 更重要的是,许多脚本已经存在。 因此,我必须使用python的C#实现,它具有与python相同的语法以及与python的libs相同的内置。 这有可能吗? 谢谢和问候Xeun

找不到IronPython sys._getframe

我目前正在C#中构建一个程序,它将调用提供的python脚本文件中的函数。 其中一些脚本文件在sys调用_getframe() ,这会导致错误: System.MissingMemberException:’module’对象没有属性’_getframe’ (由于IronPython默认没有激活_getframe 。) 我做了很多谷歌搜索,发现你可以通过提供-X:Frames作为命令行选项在ipy.exe激活它,但是这不能解决我的问题,因为我不是直接使用ipy.exe执行python代码。 在这个post中,他们提到使用命令行选项从源代码重建IronPython,我下载了源文件,但不知道如何使用这些选项构建它。 他们还提到选项是在官方安装程序中,我已经多次运行安装程序exe但没有看到那些选项的一瞥。