C#使用pythonnet调用python代码过程

2 篇文章 0 订阅
1 篇文章 0 订阅

版本对应关系

pythonnet 版本3.0.1,python版本3.8.

  1. 如果使用python3.10,会出现报错python310.dll. ---> System.ComponentModel.Win32Exception: 找不到指定的模块

  1. Net程序使用64位,如果使用anyCPU或者32位,会出现Python.Runtime.BadPythonDllException:“Runtime.PythonDLL was not set or does not point to a supported Python runtime DLL. See https://github.com/pythonnet/pythonnet#embedding-python-in-net

使用流程


            string dllPath = @"C:\Program Files\Python38\python38.dll";            
            string pythonHomePath = @"C:\Program Files\Python38";
          
            // 对应python内的重要路径
            string[] py_paths = {"DLLs", "lib", "lib\\site-packages", "lib\\site-packages\\win32"
                , "lib\\site-packages\\win32/lib", "lib\\site-packages\\Pythonwin" };
            string pySearchPath = $"{pythonHomePath};";
            foreach (string p in py_paths)
            {
                pySearchPath += $"{pythonHomePath}\\{p};";
            }

            // 此处解决BadPythonDllException报错
            Runtime.PythonDLL = dllPath;
            Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", dllPath);
            // 配置python环境搜索路径解决PythonEngine.Initialize() 崩溃
            PythonEngine.PythonHome = pythonHomePath;
            PythonEngine.PythonPath = pySearchPath;

            PythonEngine.Initialize();

            
            string position = "";
            using (Py.GIL())
            {
                string code = File.ReadAllText(@"E:\main.py");
                var scriptCompiled = PythonEngine.Compile(code, "");                
                dynamic test = Py.CreateScope();
                test.Execute(scriptCompiled);              
                dynamic r1 = test.replace("Init Python"); //OK
                // Console.WriteLine(r1);
                Debug.LogError("Init Python");
                position = (string) r1;
            }
            
            PythonEngine.Shutdown();

数据类型的对应

string[] mylist = (string[])pyList;

//or

 

List<string> mylist = ((string[])pyList).ToList<string>();

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
cshape使用pythonnet调用python 1. 安装Python.NET 在使用Python.NET之前,需要先安装Python.NET。可以在官方网站下载Python.NET:https://github.com/pythonnet/pythonnet/releases。 安装Python.NET时,需要选择与当前使用Python版本相对应的Python.NET版本。 2. 在C#调用PythonC#调用Python需要使用Python.Runtime.dll。可以在Python.NET下载的文件中找到Python.Runtime.dll使用Python.Runtime.dll时,需要在C#代码中添加以下引用: using Python.Runtime; 以下是一个简单的示例代码: ``` using System; using Python.Runtime; namespace PythonDemo { class Program { static void Main(string[] args) { using (Py.GIL()) { dynamic np = Py.Import("numpy"); dynamic sin = np.sin; Console.WriteLine(sin(5)); } } } } ``` 该代码中,使用using语句创建了一个Py.GIL()的上下文管理器,使得Python解释器可以在C#中运行。 然后使用Py.Import()函数导入Python模块,并使用动态类型变量np引用该模块。接着,使用np.sin函数计算sin(5),并将结果输出。 3. 在Python调用C# 要在Python调用C#,需要使用Python.Runtime.dll提供的ClrModule模块。 以下是一个简单的示例代码: ``` import clr clr.AddReference("MyLib") from MyLib import MyType obj = MyType() result = obj.MyMethod(5) print(result) ``` 该代码中,使用clr.AddReference()函数导入C#程序集。然后,使用from语句导入C#类MyType,并创建MyType的实例obj。最后,调用MyType的MyMethod方法,并将结果输出。 需要注意的是,C#程序集中的类必须使用public修饰符才能在Python中访问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值