我有Visual Studio 2015,我的主要表单用C#编写,从那里我有不同的用Python编写的类(普通的Python而不是Iron Python).如何从C#代码中调用Python函数?
我知道有很多关于此的主题但是大多数都太旧了,有些解决方案太难或者涉及使用像C++这样的中间语言.
以下是我发现有用的一些链接,但没有提供我正在搜索的答案:
有一个简单的方法还是我还需要一个解决方法?如果我需要一个解决方法,那么最简单的是什么?
我想使用win32com扩展来实现python com服务器.然后从.NET中使用服务器.我使用以下示例来实现com服务器并且它运行没有问题但是当我尝试使用C#消耗它时,我得到了FileNotFoundException并带有以下消息"使用CLSID检索组件的COM类工厂{676E38A6-7FA7-4BFF-9179 -AE959734DEBB}由于以下错误而失败:8007007e." .我也发布了C#代码.我想知道我是否遗漏了一些我会感激任何帮助.
谢谢,莎拉
#PythonCOMServer.py
import pythoncom
class PythonUtilities:
_public_methods_ = [ 'SplitString' ]
_reg_progid_ = "PythonDemos.Utilities"
# NEVER copy the following ID
# Use"print pythoncom.CreateGuid()" to make a new one.
_reg_clsid_ = pythoncom.CreateGuid()
print _reg_clsid_
def SplitString(self, val, item=None):
import string
if item != None: item = str(item)
return string.split(str(val), item)
# Add code so that when this script is run by
# Python.exe,.it self-registers.
if __name__=='__main__':
print 'Registering Com Server'
import win32com.server.register
win32com.server.register.UseCommandLine(PythonUtilities)
// the C# code …Run Code Online (Sandbox Code Playgroud) 我正在尝试让 pythonnet 在我在 Linux 上运行的 .Net Core 应用程序中工作。
我在我的 .Net Core 项目中引用了Python.Runtime.dll(我从nuget 获得)。
我的代码是:
using System;
using Python.Runtime;
namespace pythonnet_w
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start");
using (**Py.GIL()**) {
// blabla
}
Console.WriteLine("End");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此运行时错误:
Unhandled Exception: System.MissingMethodException: Method not found: 'System.Reflection.Emit.AssemblyBuilder
System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'.
at Python.Runtime.CodeGenerator..ctor()
at Python.Runtime.DelegateManager..ctor()
at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args, Boolean setSysArgv)
at Python.Runtime.PythonEngine.Initialize(Boolean setSysArgv)
at Python.Runtime.PythonEngine.Initialize()
at Python.Runtime.Py.GIL()
at pythonnet_w.Program.Main(String[] args) in D:\Development\~.Net libraries (3.part)\phytonnet\.Net Core test (phytonnet)\c#\pythonnet_test\Program.cs:line …Run Code Online (Sandbox Code Playgroud)