本文介绍了引用Python的“导入" C#中从IronPython调用时的汇编程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于IronPython,我是一个完全菜鸟.我需要从ASP.NET网站调用py脚本,并具有以下代码:

I'm a complete noob when it comes to IronPython. I need to call a py script from an ASP.NET website, and have the following code:

var ipy = IronPython.Hosting.Python.CreateRuntime();
dynamic test = ipy.UseFile(Server.MapPath(@"~\python\test.py"));
test.DoWork();

我正在使用的IronPython版本是2.7.

The version of IronPython I'm using is 2.7.

我需要调用的第三方python文件具有以下导入指令:

The 3rd party python file I need to call has the following import directives:

import sys
from array import *
from subprocess import *

我收到错误没有名为子进程的模块".我已经从IronPython安装Lib目录中复制了subprocess.py文件,但是我认为我需要在C#代码中链接到该文件?

I'm receiving the error "No module named subprocess". I've copied the subprocess.py file from the IronPython installation Lib directory, but I assume I need to link to it in the C# code?

谢谢.

我找到了解决方法:

ScriptEngine scriptEngine = Python.CreateEngine();

var sp = scriptEngine.GetSearchPaths();
sp.Add(Server.MapPath(@"~\python\lib"));
scriptEngine.SetSearchPaths(sp);

var scope = scriptEngine.Runtime.ExecuteFile(Server.MapPath(@"~\python\test.py"));

如果任何人有任何评论/改进,请随意说明一下,因为我对所有这些Python/IronPython恶意软件都处于新的状态.

If anyone has any comments/improvements please feel free to elaborate seeing as I'm in new ground with all this Python/IronPython malarky.

推荐答案

另一种方法:

dynamic sys = scriptEngine.ImportModule("sys");            
sys.path.append(somePath);

这篇关于引用Python的“导入" C#中从IronPython调用时的汇编程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 10:36