如何使用.py库运行python.Net文件和调用函数。我已经尝试了下面的代码,但它没有运行。
using (Py.GIL()){
var fromFile = PythonEngine.Compile(null, @"C:\PycharmProjects\pythonenet\main.py", RunFlagType.File);
fromFile.InvokeMethod("replace");//
}main.py文件:
import re
def replace():
print(re.sub(r"\s","*","Python is a programming langauge"))当我试图从字符串中运行时,它是工作的。
using (Py.GIL()){
string co = @"def someMethod():
print('This is from static script')";
var fromString = PythonEngine.ModuleFromString("someName", co);
fromString.InvokeMethod("someMethod");
}发布于 2021-12-18 16:11:39
最后,我得到了以下解决方案。
using (Py.GIL()){
dynamic os = Py.Import("os");
dynamic sys = Py.Import("sys");
sys.path.append(os.path.dirname(os.path.expanduser(filePath)));
var fromFile = Py.Import(Path.GetFileNameWithoutExtension(filePath));
fromFile.InvokeMethod("replace");
}https://stackoverflow.com/questions/70401765
复制相似问题