【问题标题】:Issue calling a python script from an iron python script (which is called from c# script engine)从 Iron python 脚本(从 c# 脚本引擎调用)调用 python 脚本的问题
【发布时间】:2014-06-25 18:57:11
【问题描述】:

我正在使用 c# 调用一个 Iron python 脚本

        ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
        ScriptRuntime runtime = new ScriptRuntime(setup);
        ScriptEngine engine = Python.GetEngine(runtime);

        ScriptSource source = engine.CreateScriptSourceFromFile("C:/KitGenerator/KitGenerator/LoadKitsFromDatabase.py");
        ScriptScope scope = engine.CreateScope();
        source.Execute(scope);

这工作正常,并且使用打印输出我已经证明代码运行正确。 然而,在 Iron python 脚本内部是对另一个 python 脚本的调用

os.system("ipython C:\KitGenerator\Kit-Generator\GenerateKitImages.py")

(我只用 (ipython "GenerateKitImages.py") 尝试过)

问题: 当我使用 Ipy.exe 运行第一个 Iron python 脚本时,它们都运行得非常好并且按预期运行。 但是,当我使用 c# 脚本引擎运行第一个 Iron python 脚本时,它只运行第一个脚本,而第二个脚本永远不会被调用。

我迷路了。我可以确认这与权限、依赖项无关。

给能解决这个问题的人的金牌和披萨。

【问题讨论】:

    标签: c# python asp.net python-2.7 ironpython


    【解决方案1】:

    首先,os.system 调用应该使用原始 (r"") 字符串:

    os.system(r"ipython C:\KitGenerator\Kit-Generator\GenerateKitImages.py")
    

    否则反斜杠会导致问题。

    但是,既然你说它可以从 ipy.exe 工作,我猜ipython 在第一种情况下是在你的PATH 上,而不是在第二种情况下。您可以设置 PATH environment variable from C#,在 os.system 调用中包含到 ipython 的完整路径,或者确保在调用 C# 程序之前设置 PATH,通过 changing it globally

    【讨论】:

    • 你拿到免费的比萨了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 2018-03-25
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多