问题从铁python脚本调用python脚本(从c#脚本引擎调用)

时间:2022-12-31 01:13:18

I'm calling an iron python script from c# using

我正在使用c#调用铁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);

This works fine and using print outs i've proven that the code runs correctly. However inside the iron python script is a call to another python script

这工作正常,并使用打印输出,我已经证明代码运行正常。但是在铁python脚本中是对另一个python脚本的调用

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

(i have tried it with just (ipython "GenerateKitImages.py")

(我已经尝试过它(ipython“GenerateKitImages.py”)

THE ISSUE: When i run the first iron python script using Ipy.exe both run perfectly fine and do as expected. HOWEVER when i run the first iron python script using the c# script engine it only runs the first script and the second script is never called.

问题:当我使用Ipy.exe运行第一个铁python脚本时,两者运行完全正常并按预期执行。然而,当我使用c#脚本引擎运行第一个铁python脚本时,它只运行第一个脚本,并且永远不会调用第二个脚本。

i'm lost on this one. I can confirm it is nothing to do with : permissions, dependencies.

我迷失在这一个。我可以确认它与:权限,依赖关系无关。

a gold medal and pizza for the man who can fix this problem.

为可以解决这个问题的人提供金牌和比萨饼。

1 个解决方案

#1


2  

First, the os.system call should use a raw (r"") string:

首先,os.system调用应该使用raw(r“”)字符串:

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

Otherwise the backslashes will cause issues.

否则反斜杠会引起问题。

However, since you said it works from ipy.exe, my guess is that ipython is on your PATH in the first case and not the second case. You can set the PATH environment variable from C#, include the complete path to ipython in the os.system call, or ensure that the PATH is set before calling the C# program, by changing it globally.

但是,既然你说它可以从ipy.exe运行,我的猜测是ipython在你的PATH中是第一种情况,而不是第二种情况。您可以从C#设置PATH环境变量,在os.system调用中包含ipython的完整路径,或者在调用C#程序之前确保已设置PATH,方法是全局更改它。

#1


2  

First, the os.system call should use a raw (r"") string:

首先,os.system调用应该使用raw(r“”)字符串:

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

Otherwise the backslashes will cause issues.

否则反斜杠会引起问题。

However, since you said it works from ipy.exe, my guess is that ipython is on your PATH in the first case and not the second case. You can set the PATH environment variable from C#, include the complete path to ipython in the os.system call, or ensure that the PATH is set before calling the C# program, by changing it globally.

但是,既然你说它可以从ipy.exe运行,我的猜测是ipython在你的PATH中是第一种情况,而不是第二种情况。您可以从C#设置PATH环境变量,在os.system调用中包含ipython的完整路径,或者在调用C#程序之前确保已设置PATH,方法是全局更改它。