13

Note: I found the solution and answered myself. Though, I have no idea why that setting was wrong and caused the problem. I'm still interested in having a good explanation about how Jython import system works; if anyone cares to gain the bounty please answer that.


I'm working on an existing Java EE project where I need to do computations in Python. I'm at the first stages of integration tests but I'm already facing an issue. I read Chapter 10 of Jython book but still can't find a solution. I also read Chapter 8 (Modules and Packages for Code Reuse) but to me it'is unclear.

An explanation of how Jython import system works and how to configure it would be very appreciated.

The problem:

$ 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

My question is: what am I doing wrong and how do I fix things to import BuildingType interface successfully? Maybe the problem stands in CLASSPATH, but I have no idea which value could be appropriate.


Code organization:

$ 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

Import path defined by a private Jython registry file:

$ 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

I'm sure Jython picks up the paths because I checked that with sys.path at Jython prompt.

BuildingType.java

package pendulum.generator;

public interface BuildingType {
    public String getBuildingName();
    public String getBuildingAddress();
    public String getBuildingId();
}
Paolo
  • 20,112
  • 21
  • 72
  • 113
  • try "from pendulum.generator import BuildingType" – sarwar Feb 14 '13 at 16:43
  • ImportError: cannot import name BuildingType – Paolo Feb 14 '13 at 16:45
  • 1
    Is jython.jar in jre/lib/ext? – Eric Galluzzo Feb 15 '13 at 17:41
  • No, it's in ./.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Myproject/WEB-INF/lib/jython.jar. I'll copy it in jre/lib/ext and test. – Paolo Feb 15 '13 at 17:45
  • [1] Maybe it needs to be on the Java CLASSPATH as well as the Python sys.path (?) [2] Try running jython with the '-v' option to debug the import. – Steven D. Majewski Feb 15 '13 at 17:53
  • @EricGalluzzo I copied the jar in the right ~jre/lib/ext` dir but nothing changed. – Paolo Feb 15 '13 at 18:06
  • @StevenD.Majewski unfortunately configuring the CLASSPATH has no effect. I edited the question (now very different from its initial shape) adding debug info. – Paolo Feb 15 '13 at 18:41
  • OK: I looked at the sources from Chapter 10. Do you have the same 'package' line in your java source ? That could be the problem. ( NEVERMIND -- I see the package line above. SAVE EDITS won't let me discard comments. ) – Steven D. Majewski Feb 15 '13 at 20:04

2 Answers2

4

You need to set CLASSPATH as well as python.path.

With the same directory layout, this works for me:

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

Reference: Working with CLASSPATH (Jython Book).

Paolo
  • 20,112
  • 21
  • 72
  • 113
Steven D. Majewski
  • 2,127
  • 15
  • 16
  • Big thanks. Without setting classpath I was unable to use my classes from command line (`java.lang.ClassNotFoundException`). From within Eclipse all worked fine, though. – Paolo Feb 22 '13 at 21:13
4

After a lot of time wasted in try/catch approach I was able to find the answer myself.

.jython have to look like this:

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

Not this:

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

In particular, adding /path/to/project/build/classes to the import path is wrong (even if it reflects the filesystem hierarchy), while /path/to/project/build is right and solved the issue.

Paolo
  • 20,112
  • 21
  • 72
  • 113