c#调用python脚本函数_彭世瑜_新浪博客
发布日期:2021-07-01 06:18:27
浏览次数:2
分类:技术文章
本文共 656 字,大约阅读时间需要 2 分钟。
需要在Nuget安装IronPython,
如果报错,需要更新Nuget版本则下载对应版本即可:https://dist.nuget.org/index.html
添加引用:IronPython.dll,Microsoft.Scripting.dll(在IronPython的安装目录中)
c#文件:
- using IronPython.Hosting;
- using Microsoft.Scripting.Hosting;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace PythonDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- ScriptRuntime pyRunTime = Python.CreateRuntime();
- dynamic obj = pyRunTime.UseFile("hello.py");
- Console.WriteLine(obj.welcome("Nick"));
- Console.WriteLine(obj.add(1,3));
- Console.ReadKey();
- }
- }
- }
python文件:
- #hello.py
- def welcome(name):
- return "hello " + name
- def add(a,b):
- return a+b
运行结果:
转载地址:https://pengshiyu.blog.csdn.net/article/details/109661844 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月10日 05时58分32秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【原创】玩博客的意义(不断领悟,不断更新)_Detective_ALong_新浪博客
2019-05-06
【原创】9G10的SMC时序探究_Detective_ALong_新浪博客
2019-05-06
html5星星评分组件,sass双循环写法
2019-05-06
js爬虫拿到的字符串数据转化成数组再转化成json对象
2019-05-06
html5如何利用rem实现自适应布局
2019-05-06
如何在vue中使用sass
2019-05-06
两道前端面试题目:1.变量作用域,2.利用hash将数组去重
2019-05-06
h5锁屏提醒-锁横屏和锁竖屏
2019-05-06
es6 promise对象回调处理详解
2019-05-06
css3 calc()方法详解
2019-05-06
javascript数组对象for循环中突然出现闭包的解决方案
2019-05-06
【leetcode】53. 最大子序和(Maximum Subarray)
2019-05-06
【leetcode】268. 缺失数字( Missing Number )
2019-05-06
【leetcode】283. 移动零( Move Zeroes )
2019-05-06
通过graphviz绘制二叉树,提供demo
2019-05-06
LeetCode | First Missing Positive(第一个缺失的正整数)
2021-07-10
sizeof()那点儿事儿
2021-07-10
一道百度面试题
2021-07-10
百度面试:删除ansi编码中的字母和数字
2021-07-10
strcmp函数实现
2021-07-10