jpython的使用(Java調(diào)用python腳本)

1、導(dǎo)入Jar

jar下載地址

找到j(luò)ython-installer-2.7.0.jar 包里面的jpython.jar蚣常,加載到工作目錄

我用的java開發(fā)工具是IDEA市咽、JDK8、Python3.5

2 抵蚊、執(zhí)行python源碼

獲取一個元組里面的元素

import org.python.util.PythonInterpreter;
public class FirstJavaScript {
    public static void main(String args[]) {

        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
        interpreter.exec("print days[1];");

    }// main
}

一般會出現(xiàn)以下異常

Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
  * sys.path: ['C:\\Users\\Administrator\\IdeaProjects\\MyText\\lib\\Lib', '__classpath__', '__pyclasspath__/']
    This attribute might be including the wrong directories, such as from CPython
  * sys.prefix: C:\Users\Administrator\IdeaProjects\MyText\lib
    This attribute is set by the system property python.home, although it can
    be often automatically determined by the location of the Jython jar file

You can use the -S option or python.import.site=false to not import the site module

看錯誤可以理解為一些庫的路徑錯誤施绎,下面也給出了解決方法

我們只需要吧代碼改為以下這個例子就可以順利執(zhí)行了。

Properties props = new Properties();
     props.put("python.home", "path to the Lib folder");
     props.put("python.console.encoding", "UTF-8");
     props.put("python.security.respectJavaAccessibility", "false");
     props.put("python.import.site", "false");
     Properties preprops = System.getProperties();
     PythonInterpreter.initialize(preprops, props, new String[0]);


     PythonInterpreter interpreter = new PythonInterpreter();
     interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
     interpreter.exec("print days[1];");

執(zhí)行結(jié)果是Tue

3贞绳、調(diào)用.py中的方法

test.py的源碼

def add(a, b):
    return a + b

Java源碼

Properties props = new Properties();
     props.put("python.home", "path to the Lib folder");
     props.put("python.console.encoding", "UTF-8");
     props.put("python.security.respectJavaAccessibility", "false");
     props.put("python.import.site", "false");
     Properties preprops = System.getProperties();
     PythonInterpreter.initialize(preprops, props, new String[0]);

     PythonInterpreter interpreter = new PythonInterpreter();
     interpreter.execfile("test.py");
     PyFunction func = (PyFunction) interpreter.get("adder",
             PyFunction.class);

     int a = 100, b = 100;
     PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
     System.out.println("anwser = " + pyobj.toString());

執(zhí)行結(jié)果為 200

4谷醉、執(zhí)行 .py文件

test.py

# -*- coding: utf-8 -*

print ("hello")
ls = [1,2,3,4,5,6]
print(ls)
print('你好')

java代碼


Properties props = new Properties();
    props.put("python.home", "path to the Lib folder");
    props.put("python.console.encoding", "UTF-8");
    props.put("python.security.respectJavaAccessibility", "false");
    props.put("python.import.site", "false");
    Properties preprops = System.getProperties();
    PythonInterpreter.initialize(preprops, props, new String[0]);

    PythonInterpreter interpreter = new PythonInterpreter();
    interpreter.execfile("test.py");


執(zhí)行結(jié)果:

hello
[1, 2, 3, 4, 5, 6]
你好

如果報出如下異常,請在源碼加上
# -- coding: utf-8 -

Exception in thread "main" SyntaxError: Non-ASCII character in file
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市熔酷,隨后出現(xiàn)的幾起案子孤紧,更是在濱河造成了極大的恐慌,老刑警劉巖拒秘,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件号显,死亡現(xiàn)場離奇詭異,居然都是意外死亡躺酒,警方通過查閱死者的電腦和手機押蚤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來羹应,“玉大人揽碘,你說我怎么就攤上這事≡捌ィ” “怎么了雳刺?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長裸违。 經(jīng)常有香客問我掖桦,道長,這世上最難降的妖魔是什么供汛? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任枪汪,我火速辦了婚禮,結(jié)果婚禮上怔昨,老公的妹妹穿的比我還像新娘雀久。我一直安慰自己,他們只是感情好趁舀,可當(dāng)我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布赖捌。 她就那樣靜靜地躺著,像睡著了一般矮烹。 火紅的嫁衣襯著肌膚如雪越庇。 梳的紋絲不亂的頭發(fā)上奋隶,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天,我揣著相機與錄音悦荒,去河邊找鬼。 笑死嘹吨,一個胖子當(dāng)著我的面吹牛搬味,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蟀拷,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼碰纬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了问芬?” 一聲冷哼從身側(cè)響起悦析,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎此衅,沒想到半個月后强戴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡挡鞍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年骑歹,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片墨微。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡道媚,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出翘县,到底是詐尸還是另有隱情最域,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布锈麸,位于F島的核電站镀脂,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏掐隐。R本人自食惡果不足惜狗热,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望虑省。 院中可真熱鬧匿刮,春花似錦、人聲如沸探颈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽伪节。三九已至光羞,卻和暖如春绩鸣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背纱兑。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工呀闻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人潜慎。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓捡多,卻偏偏與公主長得像,于是被迫代替她去往敵國和親铐炫。 傳聞我的和親對象是個殘疾皇子垒手,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 傳送門 解讀阿里Java開發(fā)手冊(v1.1.1) - 異常日志 前言 阿里Java開發(fā)手冊談不上圣經(jīng),但確實是大量...
    kelgon閱讀 4,364評論 4 50
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法倒信,類相關(guān)的語法科贬,內(nèi)部類的語法,繼承相關(guān)的語法鳖悠,異常的語法榜掌,線程的語...
    子非魚_t_閱讀 31,625評論 18 399
  • (一)Java部分 1、列舉出JAVA中6個比較常用的包【天威誠信面試題】 【參考答案】 java.lang;ja...
    獨云閱讀 7,101評論 0 62
  • 80/20 法則 (二八法則) 世界80% 的財富來自于20%的人. 80/20 法則 (二八法則) 是由意大利經(jīng)...
    zhoujia閱讀 415評論 0 0
  • 夕陽歸途斜 游花未盡賞 山無鬧聲靜 溪入村煙明
    凌峰峰行閱讀 182評論 0 3