C# 有没有办法将嵌入式python脚本作为python模块导入IronPython?

C# 有没有办法将嵌入式python脚本作为python模块导入IronPython?,c#,python,ironpython,C#,Python,Ironpython,我的dll中有一个嵌入式资源,它是一个python脚本。我想让python引擎可以使用该资源中的类和函数,这样我就可以执行外部.py文件(如\uuuuu main\uuuuuu),它可以执行以下操作 import embedded_lib # where embedded_lib is an embedded python script 有没有办法做到这一点?我希望会出现某种类型的IronPython.ImportModule('module_name',source),所以我查看了IronP

我的dll中有一个嵌入式资源,它是一个python脚本。我想让python引擎可以使用该资源中的类和函数,这样我就可以执行外部.py文件(如
\uuuuu main\uuuuuu
),它可以执行以下操作

import embedded_lib # where embedded_lib is an embedded python script

有没有办法做到这一点?我希望会出现某种类型的
IronPython.ImportModule('module_name',source)
,所以我查看了IronPython文档,但什么都找不到,但我希望我只是不善于查看。。也许有某种方法可以拦截对导入的调用,并以这种方式加载我的脚本?

这是可能的。只需将搜索路径添加到ScriptEngine对象,如下所示:

var paths = engine.GetSearchPaths();
paths.Add(yourLibsPath); // add directory to search
foreach (var keyValuePair in scope.GetItems())
{
    if(keyValuePair.Value != null)
        anotherScope.SetVariable(keyValuePair.Key, keyValuePair.Value);
}

然后您可以使用目录中的任何模块,您可以添加:

import pyFileName # without extension .py
更新 好啊如果要使用模块等嵌入式资源字符串,可以使用以下代码:

var scope = engine.CreateScope(); // Create ScriptScope to use it like a module
engine.Execute("import clr\n" +
                "clr.AddReference(\"System.Windows.Forms\")\n" +
                "import System.Windows.Forms\n" + 
                "def Hello():\n" +
                "\tSystem.Windows.Forms.MessageBox.Show(\"Hello World!\")", scope); // Execute code from string in scope. 
现在您有了一个ScriptScope对象(代码中的scope),其中包含所有执行的函数。您可以将它们插入另一个作用域,如下所示:

var paths = engine.GetSearchPaths();
paths.Add(yourLibsPath); // add directory to search
foreach (var keyValuePair in scope.GetItems())
{
    if(keyValuePair.Value != null)
        anotherScope.SetVariable(keyValuePair.Key, keyValuePair.Value);
}
或者,您也可以在此脚本作用域中执行脚本:

dynamic executed = engine.ExecuteFile("Filename.py", scope);
executed.SomeFuncInFilename();
在本脚本中,您可以使用所有函数,而无需导入:

def SomeFuncInFilename():
    Hello() # uses function from your scope

这是可能的。只需将搜索路径添加到ScriptEngine对象,如下所示:

var paths = engine.GetSearchPaths();
paths.Add(yourLibsPath); // add directory to search
foreach (var keyValuePair in scope.GetItems())
{
    if(keyValuePair.Value != null)
        anotherScope.SetVariable(keyValuePair.Key, keyValuePair.Value);
}

然后您可以使用目录中的任何模块,您可以添加:

import pyFileName # without extension .py
更新 好啊如果要使用模块等嵌入式资源字符串,可以使用以下代码:

var scope = engine.CreateScope(); // Create ScriptScope to use it like a module
engine.Execute("import clr\n" +
                "clr.AddReference(\"System.Windows.Forms\")\n" +
                "import System.Windows.Forms\n" + 
                "def Hello():\n" +
                "\tSystem.Windows.Forms.MessageBox.Show(\"Hello World!\")", scope); // Execute code from string in scope. 
现在您有了一个ScriptScope对象(代码中的scope),其中包含所有执行的函数。您可以将它们插入另一个作用域,如下所示:

var paths = engine.GetSearchPaths();
paths.Add(yourLibsPath); // add directory to search
foreach (var keyValuePair in scope.GetItems())
{
    if(keyValuePair.Value != null)
        anotherScope.SetVariable(keyValuePair.Key, keyValuePair.Value);
}
或者,您也可以在此脚本作用域中执行脚本:

dynamic executed = engine.ExecuteFile("Filename.py", scope);
executed.SomeFuncInFilename();
在本脚本中,您可以使用所有函数,而无需导入:

def SomeFuncInFilename():
    Hello() # uses function from your scope

哦,对不起,您不能使用嵌入式资源中的模块。。。然后,我认为您可以尝试使用engine.execute(string,ScriptScope)执行嵌入式资源模块,然后在另一个执行脚本或文件中使用它的ScriptScope。或者,您也可以使用资源文本创建临时文件,并使用搜索路径:)如果您认为答案是正确的,请相应地更新答案。如果需要的话,添加代码来解释它。到目前为止似乎正在工作,谢谢!(比我想象的要容易!)真的可以将其作为模块使用吗(我的意思是使用import关键字?)哦,对不起,您不需要使用嵌入式资源中的模块。。。然后,我认为您可以尝试使用engine.execute(string,ScriptScope)执行嵌入式资源模块,然后在另一个执行脚本或文件中使用它的ScriptScope。或者,您也可以使用资源文本创建临时文件,并使用搜索路径:)如果您认为答案是正确的,请相应地更新答案。如果需要的话,添加代码来解释它。到目前为止似乎正在工作,谢谢!(比我想象的要容易!)真的可以把它作为一个模块使用吗(我是说用import关键字?)