五個小時過去了,經歷各類問題,我成功了。稍稍記載一下,方便下回再用。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); } } }
(純手打,轉載時請注意做者和出處)