(最简单详细)IronPython下载、安装及简单使用

说实话,对于我这种小白,在网上找个IronPython找的很费劲,学会操作之后,直接整个随笔,供新手参考。前提是现在你应该有VS了

(1)找到IronPython的网站

很多人肯定就按照习惯搜索,IronPython下载,然后在各个网站中跳来跳去的,把人都跳烦了。其实非常简单,直接百度“IronPython”就可以了

 进去之后,直接进Download,不跟你多bb

 然后选择其中的稳定版本,这里以2.7.9为例,直接进去

 说明一下windows用户,直接选msi后缀,mac选pkg,当然也得下载IronPython.2.7.9.zip文件,里面包含需要的库文件。

(2)安装

下载好msi文件之后,直接下一步下一步安装即可,可以不安装到C盘下。

(3)安装好之后,随便建一个winform项目,然后随便加一个button控件,然后将下载的IronPython.2.7.9.zip文件中的net45中的两个dll复制,如下图所示

 然后将其复制到VS项目的debug中,然后添加引用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using IronPython.Hosting;
11 using Microsoft.Scripting.Hosting;
12 
13 namespace TestMyForm
14 {
15     public partial class 测试 : Form
16     {
17         public 测试()
18         {
19             InitializeComponent();
20         }
21 
22         private void 测试_Load(object sender, EventArgs e)
23         {
24 
25         }
26 
27         private void button1_Click(object sender, EventArgs e)
28         {
29             ScriptRuntime pyRuntime = Python.CreateRuntime();
30             dynamic obj = pyRuntime.UseFile("F:\test1.py");
31 
32             string str = obj.Test();
33 
34             MessageBox.Show(str);
35 
36         }
37     }
38 }

新手修改起来也很简单,直接将UseFile中文件路径修改就可以了。

Python代码如下

1 def Test():
2     strOK="First Test"
3     return strOK

运行结果如下图所示:

 不用配置什么路径,直接用就行,也试过引用import,都可以直接使用

原文地址:https://www.cnblogs.com/ligiggy/p/11471071.html