日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Jython调用不包含第三方库的python脚本

發(fā)布時(shí)間:2024/1/23 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Jython调用不包含第三方库的python脚本 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、本地環(huán)境安裝的是Python 2.7.11

2、用maven下載jython依賴

  • <pre?name="code"?class="html"><dependency>??
  • ????<groupId>org.python</groupId>??
  • ????<artifactId>jython</artifactId>??
  • ????<version>2.7.0</version>??
  • </dependency>??
  • 3、python腳本編寫
  • #coding:utf-8??
  • ??
  • def?adder(a,?b):????
  • ???return?a?+?b???
  • ??
  • def?mytest(str2):??
  • ????print?str2??
  • ????return?'call?success?!!!'??

  • 4、Java調(diào)用Python?
  • <pre?name="code"?class="java">package?test1;??
  • ??
  • import?java.util.Properties;??
  • ??
  • import?org.python.core.PyFunction;??
  • import?org.python.core.PyInteger;??
  • import?org.python.core.PyObject;??
  • import?org.python.core.PyString;??
  • import?org.python.util.PythonInterpreter;??
  • ??
  • public?class?Java2Python?{??
  • ????public?static?void?main(String?args[])?{??
  • ????????Properties?props?=?new?Properties();??
  • ????????props.put("python.home","D:/Python27/Lib");??
  • ????????props.put("python.console.encoding",?"UTF-8");?//?Used?to?prevent:?console:?Failed?to?install?'':?java.nio.charset.UnsupportedCharsetException:?cp0.??
  • ????????props.put("python.security.respectJavaAccessibility",?"false");?//don't?respect?java?accessibility,?so?that?we?can?access?protected?members?on?subclasses??
  • ????????props.put("python.import.site","false");??
  • ????????Properties?preprops?=?System.getProperties();??
  • ????????PythonInterpreter.initialize(preprops,?props,?new?String[0]);??
  • ??????????
  • ????????PythonInterpreter?interpreter?=?new?PythonInterpreter();??
  • ??
  • ????????interpreter.execfile("E:/workspace3/test1/src/main/java/test1/my_utils.py");??
  • ????????PyFunction?adder?=?(PyFunction)?interpreter.get("adder",?PyFunction.class);??
  • ??
  • ????????int?a?=?30,?b?=?50;??
  • ????????PyObject?pyobj?=?adder.__call__(new?PyInteger(a),?new?PyInteger(b));??
  • ????????System.out.println("anwser?=?"?+?pyobj.toString());??
  • ????????PyFunction?mytest?=?(PyFunction)?interpreter.get("mytest",?PyFunction.class);??
  • ????????PyObject?pyobj2?=?mytest.__call__(new?PyString("this?is?java?project!!!"));??
  • ????????System.out.println(pyobj2.toString());??
  • ????????interpreter.close();??
  • ??
  • ????}??
  • }??
  • 以上方式可以實(shí)現(xiàn)Java調(diào)用Python,但是在python 腳本中只能有python的原生api,如果在在腳本中有引入pandas,numpy之類的第三方擴(kuò)展包,還是會(huì)是會(huì)找不到,這個(gè)問(wèn)題正在查找是什么原因..

    numpy , scipy 都是 c python 的第三方模塊,是用 c (部分 c++, 和 fortran )寫的,必然不支持

    總結(jié)

    以上是生活随笔為你收集整理的Jython调用不包含第三方库的python脚本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。