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

结果为:

相关推荐
2501_931162435 分钟前
大疆相机:空中影像新境界
python
测试19987 分钟前
Web自动化测试入门
自动化测试·软件测试·python·功能测试·selenium·测试工具·测试用例
予枫的编程笔记9 分钟前
【论文解读】DLF:以语言为核心的多模态情感分析新范式 (AAAI 2025)
人工智能·python·算法·机器学习
lbb 小魔仙25 分钟前
【Python】零基础学 Python 爬虫:从原理到反爬,构建企业级爬虫系统
开发语言·爬虫·python
黄河里的小鲤鱼30 分钟前
拯救草台班子-战略
人工智能·python·信息可视化
Dr.Alex Wang33 分钟前
Google Firebase 实战教学 - Streamlit、Bucket、Firebase
数据库·python·安全·googlecloud
小二·34 分钟前
Python Web 全栈开发实战教程:基于 Flask 与 Layui 的待办事项系统
前端·python·flask
万物得其道者成43 分钟前
用 Python + MySQL + Web 打造我的私有 Apple 设备监控面板
前端·python·mysql
vyuvyucd1 小时前
手机自动化控制:Python+uiautomator2教程
python
love_summer1 小时前
深入理解Python控制流:for/while循环的底层逻辑与最佳实践
python