当前位置: 首页 > news >正文

.net 与 Pythonnet库的使用心得

python脚本使用.net

准备工作

安装pythonnet库

pip install pythonnet

查看是否安装了clr库

pip list | findstr clr

如果报错 module 'clr' has no attribute 'AddReference'

卸载clr

pip uninstall clr

测试脚本

import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import Form, Button

def on_click(sender, event):
    print("Button clicked!")

form = Form()

button = Button()
button.Text = "click me"
button.Click += on_click

form.Controls.Add(button)

form.ShowDialog()

.Net Core使用Pythonnet

Python运行时程序集定义了许多公共类,这些类提供了Python C-API提供的功能的子集。

如 PyObject, PyList, PyDict, PyTuple 等。

初始化

Runtime.PythonDLL = dllPath;
PythonEngine.Initialize();
//允许跨线程访问
PythonEngine.BeginAllowThreads();

注意:dllPath 指示python库的位置

配置python库路径
windows

查找python 安装路径

where python

这里我们配置dll的路径,我本地安装的3.8的版本,这里配置成xxxxxx/python38.dll

linux

查看python版本

python --version

查看动态位置,这里命令是找usr目录下的3.11版本的位置

find /usr -name "libpython3.11.so"

如上,配置成上面其中一个即可

配置脚本或库的路径

这里配置下常用库的python文件的路径

var pythonDll = PythonDLLPath;
var path = Path.GetDirectoryName(pythonDll);
var pythonDlls = Path.Combine(path, "DLLs");
var lib = Path.Combine(path, "lib");
var sitePackages = Path.Combine(path, "Lib", "site-packages");
if (!string.IsNullOrWhiteSpace(PythonLibPath))
    sitePackages = PythonLibPath;


PythonEngine.PythonPath += $@";{path};{pythonDlls};{sitePackages};{lib};";
加载执行脚本
var fullPath = Path.GetFullPath(pythonPath);
var dir = System.IO.Path.GetDirectoryName(fullPath);
var modulName = Path.GetFileNameWithoutExtension(fullPath);

using (Py.GIL())
{

    var pModule = Py.CreateScope();
    if (!PythonEngine.PythonPath.Contains(dir))
    {
        PythonEngine.PythonPath = $"{dir};{PythonEngine.PythonPath}";
    }

    dir = dir.Replace('\\', '/') + "/"; //python 路径最后要有/
    StringBuilder importsb = new StringBuilder();
    importsb.Append("import sys").AppendLine();
    importsb.Append($"sys.path.append('{dir}')").AppendLine();
    var pSubModule = pModule.Exec(importsb.ToString());
    
    dynamic np = pSubModule.Import(modulName);
   
    pSubModule.Dispose();
    pModule.Dispose();
}
执行方法

执行方法前需要获取解释器锁,不然多线程情况会异常

官方解释:

Before interacting with any of the objects or APIs provided by the Python.Runtime namespace, calling code must have acquired the Python global interpreter lock by using'' ``Py.GIL(). The only exception to this rule is the PythonEngine.Initialize method, which may be called at startup without having acquired the GIL. The GIL is released again by disposing the return value of Py.GIL():

The Py.GIL()'' object is a thin wrapper over the unmanaged ``PyGILState_Ensure (on construction) and PyGILState_Release (on disposal) functions from the Python API, and the documentation for those APIs applies to the managed versions.

在与Python提供的任何对象或API交互之前。运行时命名空间,调用代码必须使用Py.GIL()获取Python全局解释器锁。这个规则的唯一例外是PythonEngine。初始化方法,可以在启动时调用,而无需获取GIL。通过处理Py.GIL()的返回值再次释放GIL:
Py.GIL()“”对象是Python API中非托管“PyGILState_Ensure(构造时)”和“PyGILState_Release(处置时)”函数的精简包装,这些API的文档适用于托管版本。

try
{
     
    using (Py.GIL())
    {
        //这里调用python脚本中的方法
        var res = (bool)np.init();
    }
}
catch (Exception ex)
{
   
}
处理结果

python脚本中返回类型为 {data: bytes, width: 640, height : 480  }

C# 端接收需转成对应结构即可

 var frame = np.getframe();
 int width = ((PyObject)frame).GetItem("width").As<int>();
 int height = ((PyObject)frame).GetItem("height").As<int>();
 var frameBytes = ((PyObject)frame).GetItem("data").As<byte[]>();

http://www.dtcms.com/a/56768.html

相关文章:

  • idea 创建springboot 项目,连接数据库,后台接口实现
  • ajax之生成一个ajax的demo示例
  • QSplitter.setStretchFactor无效
  • 分析TCP三次握手与四次挥手
  • 前沿科技展望未来发展趋势
  • 【linux网络编程】套接字socket
  • 近三年图像超分辨率研究进展综述(轻量化方向)
  • 介绍高性能的HTTP和反向代理服务器Nginx
  • hbase-05 namespace、数据的确界TTL
  • python加载动态网站内容Playwright使用介绍
  • 小程序 wxml 语法 —— 39 简单双向数据绑定
  • 力扣1463. 摘樱桃 II
  • 存量思维和增量思维
  • Python代码调试方法集锦
  • 用DEEPSEEK做数据看板:高效、实用与创新的融合
  • android paging使用教程
  • open-webui+deepseek api实现deepseek自由
  • AI×电商数据API接口:深度融合,引领未来电商行业浪潮
  • Vulnhub-Node
  • leetcode69.x 的平方根
  • 《Python实战进阶》No16: Plotly 交互式图表制作指南
  • Python3 爬虫 爬虫中间件
  • AI系统架构
  • JS如何实现全选以及联动效果
  • Linux常见指令
  • leetcode-sql数据库面试题冲刺(高频SQL五十题)
  • MySQL第一次作业
  • ubuntu24.04-系统重装
  • fastapi+angular停车管理系统可跨域
  • MaxKB结合DeepSeek快速构建客服企业知识库