通过资源将Python嵌入C# [英] Embedding Python in C# via resources
问题描述
我已经解决了一段时间,但似乎无法解决,因此我需要一些帮助!问题是我正在用C#编写程序,但是我需要我创建的Python文件中的函数.这本身没有问题:
I have been working on a problem for a while now which I cannot seem to resolve so I need some help! The problem is that I am writing a program in C# but I require a function from a Python file I created. This in itself is no problem:
...Usual Stuff
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
namespace Program
{
public partial class Form1 : Form
{
Microsoft.Scripting.Hosting.ScriptEngine py;
Microsoft.Scripting.Hosting.ScriptScope s;
public Form1()
{
InitializeComponent();
py = Python.CreateEngine(); // allow us to run ironpython programs
s = py.CreateScope(); // you need this to get the variables
}
private void doPython()
{
//Step 1:
//Creating a new script runtime
var ironPythonRuntime = Python.CreateRuntime();
//Step 2:
//Load the Iron Python file/script into the memory
//Should be resolve at runtime
dynamic loadIPython = ironPythonRuntime.;
//Step 3:
//Invoke the method and print the result
double n = loadIPython.add(100, 200);
numericUpDown1.Value = (decimal)n;
}
}
}
但是,这要求文件"first.py"必须位于程序一旦编译的位置.因此,如果我想共享程序,则必须同时发送可执行文件和python文件,这非常不方便.我想解决此问题的一种方法是,将"first.py"文件添加到资源中并从那里运行...但是我不知道该怎么做,即使可能也是这样.
However, this requires for the file 'first.py' to be wherever the program is once compiled. So if I wanted to share my program I would have to send both the executable and the python files which is very inconvenient. One way I thought to resolve this is by adding the 'first.py' file to the resources and running from there... but I don't know how to do this or even if it is possible.
自然,上面的代码对此不起作用,因为.UseFile方法采用字符串参数而不是byte [].有人知道我会如何进步吗?
Naturally the above code will not work for this as .UseFile method takes string arguments not byte[]. Does anyone know how I may progress?
推荐答案
让我们从可能可行的最简单的事情开始,您将获得一些类似于以下内容的代码:
Lets start with the simplest thing that could possibly work, you've got some code that looks a little like the following:
// ...
py = Python.CreateEngine(); // allow us to run ironpython programs
s = py.CreateScope(); // you need this to get the variables
var ironPythonRuntime = Python.CreateRuntime();
var x = py.CreateScriptSourceFromFile("SomeCode.py");
x.Execute(s);
var myFoo = s.GetVariable("myFoo");
var n = (double)myFoo.add(100, 200);
// ...
,我们想用其他方式替换 var x = py.CreateScriptSourceFromFile(...
行;如果我们可以将嵌入式资源作为字符串获取,则可以使用ScriptingEngine.CreateScriptSourceFromString()
.
and we'd like to replace the line var x = py.CreateScriptSourceFromFile(...
with something else; If we could get the embedded resource as a string, we could use ScriptingEngine.CreateScriptSourceFromString()
.
得到这个好答案,我们可以得到一些看起来像的东西像这样:
Cribbing this fine answer, we can get something that looks a bit like this:
string pySrc;
var resourceName = "ConsoleApplication1.SomeCode.py";
using (var stream = System.Reflection.Assembly.GetExecutingAssembly()
.GetManifestResourceStream(resourceName))
using (var reader = new System.IO.StreamReader(stream))
{
pySrc = reader.ReadToEnd();
}
var x = py.CreateScriptSourceFromString(pySrc);
这篇关于通过资源将Python嵌入C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!