13

注意:我找到了解决方案并回答了自己。不过,我不知道为什么该设置是错误的并导致了问题。我仍然有兴趣对 Jython 导入系统的工作原理进行很好的解释;如果有人想获得赏金,请回答。


我正在处理一个需要在 Python 中进行计算的现有 Java EE 项目。我正处于集成测试的第一阶段,但我已经面临一个问题。我阅读了 Jython 书的第 10 章,但仍然找不到解决方案。我还阅读了第 8 章(代码重用的模块和包),但对我来说还不清楚。

非常感谢您解释 Jython 导入系统的工作原理以及如何配置它。

问题:

$ jython -v
import: 'exceptions' as org.python.core.exceptions in builtin modules
Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:54:35) 
[Java HotSpot(TM) Server VM (Oracle Corporation)] on java1.7.0_10
import: import site # precompiled from /home/me/jython/2.5.3/Lib/site$py.class
import: 'sys' as sys in builtin modules
import: import os # precompiled from /home/me/jython/2.5.3/Lib/os$py.class
import: 'errno' as org.python.modules.errno in builtin modules
import: 'posix' as org.python.modules.posix.PosixModule in builtin modules
import: import posixpath # precompiled from /home/me/jython/2.5.3/Lib/posixpath$py.class
import: import stat # precompiled from /home/me/jython/2.5.3/Lib/stat$py.class
import: 'java' as java package
import: 'File' as java class
import: 'IOException' as java class
import: 'org' as java package
import: 'Py' as java class
Type "help", "copyright", "credits" or "license" for more information.

>>> import pendulum.generator.BuildingType
import: import pendulum # precompiled from /path/to/project/build/classes/pendulum/__init__$py.class
import: import pendulum.generator # precompiled from /path/to/project/build/classes/pendulum/generator/__init__$py.class
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named BuildingType

我的问题是:我做错了什么以及如何解决问题以成功导入 BuildingType 接口?也许问题出在 CLASSPATH 中,但我不知道哪个值可能合适。


代码组织:

$ tree build/classes/pendulum/generator/ src/pendulum/generator/
build/classes/pendulum/generator/
├── BuildingType.class
├── __init__.py
└── __init__$py.class

src/pendulum/generator/
├── BuildingType.java
└── __init__.py

由私有 Jython 注册表文件定义的导入路径:

$ cat ~/.jython 
python.path=\
/path/to/project/build/classes:\
/path/to/project/src:\
/home/me/jdevel/extras/2.5.3/Lib:\
/home/me/jdevel/extras/2.5.3/Lib/site-packages

我确信 Jython 会选择路径,因为我sys.path在 Jython 提示符处检查了它。

建筑类型.java

package pendulum.generator;

public interface BuildingType {
    public String getBuildingName();
    public String getBuildingAddress();
    public String getBuildingId();
}
4

2 回答 2

4

在尝试/捕获方法浪费了很多时间之后,我自己找到了答案。

.jython必须看起来像这样:

python.path=\
/path/to/project/build:\
/path/to/project/src:\
/home/me/jdevel/extras/2.5.3/Lib:\
/home/me/jdevel/extras/2.5.3/Lib/site-packages

不是这个:

python.path=\
/path/to/project/build/classes:\
/path/to/project/src:\
/home/me/jdevel/extras/2.5.3/Lib:\
/home/me/jdevel/extras/2.5.3/Lib/site-packages

特别是,添加/path/to/project/build/classes到导入路径是错误的(即使它反映了文件系统层次结构),而/path/to/project/build正确并解决了问题。

于 2013-02-19T14:04:06.907 回答
4

您需要设置 CLASSPATH 以及 python.path。

使用相同的目录布局,这对我有用:

jython10$ CLASSPATH=build/classes/  jython -v Building.py
import: 'exceptions' as org.python.core.exceptions in builtin modules
import: import site # precompiled from /usr/local/Java/jython2.5.3/Lib/site$py.class
import: 'sys' as sys in builtin modules
import: import os # precompiled from /usr/local/Java/jython2.5.3/Lib/os$py.class
import: 'errno' as org.python.modules.errno in builtin modules
import: 'posix' as org.python.modules.posix.PosixModule in builtin modules
import: import posixpath # precompiled from /usr/local/Java/jython2.5.3/Lib/posixpath$py.class
import: import stat # precompiled from /usr/local/Java/jython2.5.3/Lib/stat$py.class
import: java package as '/Users/sdm7g/jaxp/jython10/build/classes/pendulum'
import: 'pendulum' as java package
import: 'pendulum' as java package
import: java package as '/Users/sdm7g/jaxp/jython10/build/classes/pendulum/generator'
import: 'pendulum.generator' as java package
import: 'BuildingType' as java class

参考:使用 CLASSPATH(Jython Book)。

于 2013-02-15T20:20:41.203 回答