五个小时过去了,经历各类问题,我成功了。稍稍记载一下,方便下回再用。php
下载最新版应该是IronPython-2.7.x.msi
http://ironpython.net/python
在IronPython安装目录中找到以下两项dllc#
写一个最简单的.py文件待调用windows
# -*- coding: utf-8 -*- def text(): return "我是返回结果"
导入c#新建的winformapp
调用前添加两个引用再调用python2.7
using IronPython.Hosting; using Microsoft.Scripting.Hosting;
要调用的地方以下:函数
ScriptRuntime pyRuntime = Python.CreateRuntime(); dynamic py = pyRuntime.UseFile("mainwindows.py"); string a = py.text(); textBox1.AppendText(a);
看到编辑框添加的返回文本即成功,剩下不表。学习
到这一切都很愉快,画风一转:测试
过程没有截图,无非就是写好函数调用,函数中调用了三方类库requests,而后出错,其余类库也有,出错问题每每是'module' object has no XXXX。ui
应该是path的问题,按照度娘出的答案各类添加path结果无效,检查路径神都对,依然不行。
按照cmd下,python,添加打印的全部,这样保险。
import sys print(sys.path)
发现本机安装的Python3.5,ironPython默认python2.7,只好再次安装Python2.7共存
安装Python2.7
注意在Python2.7安装目录,把Python.exe不变;在Python3.5安装目录,把Python.exe重命名为
python3.exe。cmd下看Python 和 Python3 弹出版本是否为2.7和3.5。
而后配置Python2和3的环境变量
x:\xxx\Python35 x:\xxx\Python35\Scripts x:\xxx\Python27 x:\xxx\Python27\Scripts
不要用这个pip install xxxx,用下面的:
下载源码,解压 cmd cd进入目录 python setup.py install
此时添加的完整path有效,在pycharm上测试经过:
# -*- coding: utf-8 -*- import sys sys.path.append(r'C:\Python27') sys.path.append(r'C:\Python27\Lib\DLLs') sys.path.append(r"C:\Python27\Lib") sys.path.append(r"C:\Python27\Lib\site-packages") sys.path.append(r'C:\Python27\Lib\site-packages\requests-2.8.1-py2.7.egg') import requests
注意切换pycharm调用的Python版本,file--settings--project---project interpreter,选版本。
添加的完整path完成后发现依旧报错** 'module' object has no attribute '_getframe',貌似好多人都是卡在了这一步过不去。**
换用Google,外国大神找到方法,http://stackoverflow.com/questions/6997832/ironpython-sys-getframe-not-found
总而言之就是当建立PythonEngine时添加字典类型参数options:
var options = new Dictionary<string, object>(); options["Frames"] = true; options["FullFrames"] = true; ScriptEngine engine = Python.CreateEngine(options);
试验一下,OK大功告成
.py文件
# -*- coding: utf-8 -*- #添加path import sys sys.path.append(r'C:\Python27') sys.path.append(r'C:\Python27\Lib\DLLs') sys.path.append(r"C:\Python27\Lib") sys.path.append(r"C:\Python27\Lib\site-packages") sys.path.append(r'C:\Python27\Lib\site-packages\requests-2.8.1-py2.7.egg') import requests def text(): return "我是返回结果" def getPageIps(url="http://www.66ip.cn/mo.php?sxb=&tqsl=1000&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea="): headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64)"} r = requests.get(url,headers=headers) return r.text
c#文件:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using IronPython.Hosting; using Microsoft.Scripting.Hosting; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ScriptRuntime pyRuntime = Python.CreateRuntime(); dynamic py = pyRuntime.UseFile("mainwindows.py"); string a = py.text(); textBox1.AppendText(a); } private void button2_Click(object sender, EventArgs e) { //初始化,并添加参数 var options = new Dictionary<string, object>(); options["Frames"] = true; options["FullFrames"] = true; ScriptRuntime pyRuntime = Python.CreateRuntime(options); dynamic py = pyRuntime.UseFile("mainwindows.py"); string a = py.getPageIps(); textBox1.AppendText(a); } } }
(纯手打,转载时请注意做者和出处)