从Jython导入罐子 [英] Importing jars from Jython

查看:101
本文介绍了从Jython导入罐子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须承认我对Jython导入逻辑有些困惑.

I must admit that I'm slightly confused by the Jython import logic.

我现在可以手动将一个罐子一个个地添加到sys.path中,但是我有一堆罐子,这很痛苦.

I now that I can manually add the jars one by one to sys.path but I have a whole bunch of them and this is quite painful.

添加包含jar的目录显然不起作用.

Adding the directory containing the jars obviously doesn't work.

做到这一点的最佳方法是什么?

What is the best way to do that?

推荐答案

以下代码应为您解决问题,并限制使用标准jython库时需要进行的键入数量.

The following code should do the trick for you and limit the amount of typing that you need to do while making use of the standard jython library.

import os,glob,sys

directories=['/path/to/jars/','/different/path/to/more/jars/']

for directory in directories:
    for jar in glob.glob(os.path.join(directory,'*.jar')):
        sys.path.append(jar)

更好的答案: 您可以在jython站点程序包中创建一个.pth文件,并在其中包含要包含在该路径中的jar的所有完整路径.这是我添加poi jars(.pth文件的内容)的步骤:

Better answer: You can create a .pth file in the jython site-packages and include within it all of the full paths to the jars you want included on the path. Here is what I did to include poi jars(contents of .pth file):

/home/kris/jython2.5.3/poi-3.11/poi-3.11-20141221.jar
/home/kris/jython2.5.3/poi-3.11/poi-ooxml-3.11-20141221.jar
/home/kris/jython2.5.3/poi-3.11/poi-ooxml-schemas-3.11-20141221.jar
/home/kris/jython2.5.3/poi-3.11/ooxml-lib/xmlbeans-2.6.0.jar

这样做之后,我可以导入而不必附加到路径:

After doing that I can then import without having to append to path:

from org.apache.poi.xssf.usermodel import XSSFWorkbook

这篇关于从Jython导入罐子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆