IronPython(.Net中调用python简述)

官网为:IronPython.net /

环境配置

1、使用visual studio2022创建一个控制台项目 2、打开Nuget管理工具,搜索IronPython

3、记得安装vs的python支持,避免编译失败情况 4、在项目中创建一个PythonScripts的文件夹,用来存放python代码

简单调用

我们可以简单编写程序如下

python 复制代码
# main.py
import sys
import uuid

def Test():
    return 'Hello IronPython!'

def SysVersion():
    return sys.version

def CreateUUID():
    return str(uuid.uuid1())

def Print():
    print('Hello World!')

这时在Program.cs中可以输入如下

C# 复制代码
using System;
using IronPython.Hosting;

var engine = Python.CreateEngine();
dynamic py = engine.ExecuteFile(@"PythonScripts/main.py");

Console.WriteLine("Test:");
var data = py.Test();
Console.WriteLine(data);
Console.WriteLine();


Console.WriteLine("Python & .NET Version:");
var version = py.SysVersion();
Console.WriteLine(version);

Console.WriteLine();

// 使用Python的UUID标准库生成基于时间戳的UUID
Console.WriteLine("Create UUID By Python:");
var uuid = py.CreateUUID();
Console.WriteLine(uuid.ToString());

Console.WriteLine();
var print = py.Print();

如何获取main中变量的值并更改

python 复制代码
# main.py
import sys
import uuid

a=10

def Test():
    return 'Hello IronPython!'

def SysVersion():
    return sys.version

def CreateUUID():
    return str(uuid.uuid1())

def Print():
    print('Hello World!')

设置a等于10

C# 复制代码
var a = py.GetVariable("a");
Console.WriteLine("a的值为"+a.ToString());

py.SetVariable("a", 20);
var v = py.GetVariable("a");
Console.WriteLine("a修改后的值为"+v.ToString());

那如何调用函数并传值呢?

python 复制代码
import sys
import uuid

a=10

def Test():
    return 'Hello IronPython!'

def SysVersion():
    return sys.version

def CreateUUID():
    return str(uuid.uuid1())

def Print():
    print('Hello World!')
    
def add_numbers(a,b):
    return a + b
C# 复制代码
using System;
using IronPython.Hosting;

var engine = Python.CreateEngine();
dynamic py = engine.ExecuteFile(@"PythonScripts/main.py");

Console.WriteLine("Test:");
var data = py.Test();
Console.WriteLine(data);
Console.WriteLine();


Console.WriteLine("Python & .NET Version:");
var version = py.SysVersion();
Console.WriteLine(version);

Console.WriteLine();

// 使用Python的UUID标准库生成基于时间戳的UUID
Console.WriteLine("Create UUID By Python:");
var uuid = py.CreateUUID();
Console.WriteLine(uuid.ToString());

Console.WriteLine();
var print = py.Print();

var a = py.GetVariable("a");
Console.WriteLine("a的值为"+a.ToString());

py.SetVariable("a", 20);
var v = py.GetVariable("a");
Console.WriteLine("a修改后的值为"+v.ToString());

var add_function = py.GetVariable("add_numbers");
int a1 = 5;
int b1 = 3;
var res = add_function(a1, b1);
Console.WriteLine(res);

结果为:

相关推荐
天才测试猿9 小时前
2026软件测试面试八股文(含答案+文档)
自动化测试·软件测试·python·功能测试·测试工具·面试·职场和发展
张小凡vip11 小时前
python--爬虫--成熟的爬虫框架Scrapy
爬虫·python·scrapy
Pluchon11 小时前
Java个人综合项目——萌部落社区V2.0
java·python·spring·spring cloud·postman·idea
不瘦80斤不改名12 小时前
01-vibe-coding-起源与本质
人工智能·python
夜雪一千12 小时前
如何对新闻数据进行模糊去重
python
oyguyteggytrrwwwrt14 小时前
3dgs渲染部分详细注释
python
天天爱吃肉821815 小时前
【重磅发布:拿下新超仁达代理权 】
大数据·人工智能·python·功能测试·汽车
神王宝宝 王者小学16 小时前
面向领域驱动架构的查询实现方式
前端·python·架构
ningmengjing_17 小时前
Redis 从入门到实战:Python操作全攻略
数据库·redis·python
️学习的小王17 小时前
智能文档助手:基于RAG的本地化文档问答系统实战指南
人工智能·python·机器学习