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);

结果为:

相关推荐
晓风残月淡2 小时前
JVM字节码与类的加载(二):类加载器
jvm·python·php
西柚小萌新4 小时前
【深入浅出PyTorch】--上采样+下采样
人工智能·pytorch·python
CsharpDev-奶豆哥6 小时前
ASP.NET中for和foreach使用指南
windows·microsoft·c#·asp.net·.net
shut up6 小时前
LangChain - 如何使用阿里云百炼平台的Qwen-plus模型构建一个桌面文件查询AI助手 - 超详细
人工智能·python·langchain·智能体
宝贝儿好7 小时前
【python】第五章:python-GUI编程
python·pyqt
闲人编程7 小时前
从多个数据源(CSV, Excel, SQL)自动整合数据
python·mysql·数据分析·csv·存储·数据源·codecapsule
B站_计算机毕业设计之家8 小时前
推荐系统实战:python新能源汽车智能推荐(两种协同过滤+Django 全栈项目 源码)计算机专业✅
大数据·python·django·汽车·推荐系统·新能源·新能源汽车
茯苓gao8 小时前
Django网站开发记录(一)配置Mniconda,Python虚拟环境,配置Django
后端·python·django
Full Stack Developme8 小时前
Python Redis 教程
开发语言·redis·python
码界筑梦坊8 小时前
267-基于Django的携程酒店数据分析推荐系统
python·数据分析·django·毕业设计·echarts