我尝试使用 IronPython 在 Visual Studio 中运行 python 脚本,一切进展顺利,直到我尝试使用此代码在 python 脚本中使用任何库。
var ipy = Python.CreateRuntime();
dynamic test = ipy.UseFile("C:/Python/Test.py");
var whatever = test.python_string();
MessageBox.Show(whatever);
Test.py 看起来像这样。
def python_string():
return "Okay"
一旦我尝试添加
from moviepy.editor import *
或 Visual Studio 程序不会执行的 Python 代码的任何其他库导入语句。
我正在使用 Visual Studio 2015,并且我按照其网站上所示的方式设置了 IronPython。我猜我必须将该库添加到 Visual Studio 中的某个位置,但我不知道在哪里或如何添加。
希望我说清楚了。提前致谢。
最佳答案
我在使用 SharpDevelop 时遇到了同样的问题。按照说明操作here并为外部模块/包创建类库 (DLL)。将输出 DLL 添加到您的项目引用中。然后在 C# main() 中,加载这些程序集,如下所示:
// make sure to include "using System.IO"
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "serial.dll");
engine.Runtime.LoadAssembly(Assembly.LoadFile(path));
其中 engine
指的是您的 Python ScriptEngine
。
我使用它为 pyserial
模块构建了一个程序集,并将它们包含在 C#/IronPython 项目中。我不知道是否有更好的方法,但经过几个小时的挫折后,这对我有用。
关于c# - 无法在 Visual Studio 中使用 IronPython 运行带有库的 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39841429/