最近更新 :16年3月10日更

首先你在用JAVA,你需要运行Python,于是你找了Jython,我不介绍什么是Jythyon了

如何在Java中调用Python的方法,一百度一大堆,

如下:是一种简单的调用  

PythonInterpreter interpreter = new PythonInterpreter();
InputStream filepy = new FileInputStream("D:\\demo.py");
interpreter.execfile(filepy); ///执行python py文件
filepy.close();
如果你一不小心遇到了这个错误,那你就来对地方了。

  Determine if the following attributes are correct:
  * sys.path: ['/Users/test/.gradle/caches/modules-2/files-2.1/org.python/jython/2.7.0/576ac65d2cfca511f82f3a65a3568fefd91884d/Lib', '__classpath__', '__pyclasspath__/']
  This attribute might be including the wrong directories, such as from CPython
  * sys.prefix: /Users/test/.gradle/caches/modules-2/files-2.1/org.python/jython/2.7.0/576ac65d2cfca511f82f3a65a3568fefd91884d
  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
  ] with root cause

先说下什么错误意义:

  直译:请确认下面的属性是正确的:

  sys.路径:…………………………(省略了路径)

  这个属性包含了错误的目录,比如CPython

  sys.前缀(其实就是路径的前面一部分):…………(省略了路径)

  这个属性可以通过系统属性python.home设置,通常Jython jar路径路径自动搞定了这个。

  你可以使用-S命令或者设置python.import.site=false设置不引用设定值(根本问题)……好吧,最后这句我理解的不是很好。

然后说一下参考页 面:http://bugs.jython.org/issue2355 对,又是英文

核心是这段话:

hi Soha,
嗨Soha,
I did like this. If you find a better way please share it.
我不喜欢这个方法,如果你有更好的解决办法,求分享。


1- Explode the jython jar: get the jython standalone jar, change the .jar to .zip, unzip it into a folder. Inside that unzipped folder you will find a subfolder Lib.
1-找到Jython的jar 包,获取单独的Jython的Jar包,将扩展名.jar改成.zip,解压到文件夹,你能在解压后的文件夹里面找到一个Lib文件夹;


2- Copy that folder Lib into your eclipse project.

2- 复制这个Lib到你的Eclipse(编译器) 项目 3- In you java code, when you initialize jython, point to that Lib folder as python.home, for example:
3- 在你的java代码中华,当你初始化Jython的时候,将python.home设置为这个Lib文件夹,如下 Properties props = new Properties();
代码没得译
props.put("python.home","path to the Lib folder(你的lib 路径)");
代码没得译
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 interp = new PythonInterpreter(); That's it! now it should work!
然后就可以工作了

然而参考这个并不是全面的,因为并没有Lib(如果你有,我没话说), 解决办法差不多,这个错误的原因,很明确,其实就是找不到Python.home,so代码不变,但是python路径的来源需要你下功夫,上面走的通最好,要么还可以是系统的,也活着干脆你网上下一个随便找个位置解压。OK搞定!!

16-3-2:

如果你实现了上面的步骤,那么恭喜你,基本的运行搞定了,你可以跑简单的python脚本,但是,一个简单的脚本貌似你也没必要大费周章的如此。so,如果你在自己的py中import了系统的或者其他的py,那么,恭喜你炸了!!!!!!

你基本会看到类似下面错误

Exception in thread "main" Traceback (most recent call last):
  File "路径\Test.py", line 4, in <module>
    import odbchelper
ImportError: No module named odbchelper

于是,经过我不断地努力,我想告诉你!!上面的解决办法不够好,之所以出现上面的种种问题,原因在于包上,jython.jar和jython-standalone.jar,目前我尚不清楚这两个包的区别,但是 后者的确会让你避免各种错误,原因我会继续查看并持续更新。

附jar包地址:http://mvnrepository.com/artifact/org.python/jython/2.7.0,包比较大,留下网络地址,你们自己搞吧。

最新更新问题:

使用上面的方法,你的代码应该可以跑了。但是,当你打包的时候,你又会遇到相同的问题,因为,打的包里面它默认又会找之前的路径,但是之前的路径是jar包,于是又炸了

多次探索,最后还是回归,按照上面的提示,使用哪个英文地址的方式,引入一个外面的环境,也就是扔一个Lib放在哪里就好,有问题可以留言,不过最好自己去哪个网址看看,再来一次网址:http://bugs.jython.org/issue2355

 

最新文章

  1. .NET 学习书籍推荐
  2. 大理石在哪里UVa 10474
  3. 让script的type属性等于text/html
  4. Linux资源监控命令/工具(综合)
  5. Swift - defer关键字(推迟执行)
  6. Median of Two Sorted Arrays 解答
  7. nodejs中EventEmitter
  8. Word Ladder II 2015年6月4日
  9. 【转】GLONASS全球卫星导航系统
  10. ios项目开发汇总
  11. 将1~n个整数按照字典序进行排序
  12. window配置右键菜单
  13. hdu6363 bookshelf 容斥+数列+数论gcd定理(也可以M&#246;bius)
  14. luogu4389 付公主的背包
  15. 【洛谷p1507】NASA的食物计划
  16. Java-Runoob-高级教程-实例-时间处理:04. Java 实例 - 时间戳转换成时间
  17. android开发之一如何升级SDK
  18. MySQL自成一派的查询提示
  19. 1.hive开窗函数,分析函数
  20. 2017 ACM Amman Collegiate Programming Contest 题解

热门文章

  1. Memcache及telnent命令详解--转
  2. 1个小时学会ReactiveCocoa基本使用
  3. Android开发之显示进度对话框
  4. ci 中使用 pdo 连接 mysql
  5. 文件权限和目录权限详解(rwx)
  6. JS实例(二)
  7. 转-C# 操作 Excel 常见问题收集和整理
  8. 反射+javacsv+scv文件构建资源获取
  9. ios专题 - CocoaPods - 初次体验
  10. 倒置字符串s中各字符的位置