从Jython导入罐子

从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