Paolo
Paolo

Reputation: 21056

Import issues - is my Jython installed correctly?

I'm starting using Jython. I noted that sys.path is completely different when Jython is executed from the command line than from Eclipse.

Command line

tk:~$ jython 
Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36) 
[Java HotSpot(TM) Server VM (Oracle Corporation)] on java1.7.0_10
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys                      
>>> print sys.path
['',
 '/jproject/extras/2.5.3/Lib',
 '__classpath__',
 '__pyclasspath__/',
 '/jproject/extras/2.5.3/Lib/site-packages']

It's OK, I can import everything from there (import pdb, import csv, etc).

Eclipse

I added jython.jar in Eclipse to the Java Build Path using the "Add JARs" button. I wrote a simple Java class (excerpt):

PythonInterpreter pi = new PythonInterpreter();
pi.exec("import sys");
pi.exec("print sys.path");

Output:

['/jproject/projects/foobar/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MyProject/WEB-INF/lib/Lib',
 '__classpath__',
 '__pyclasspath__/']

From Python scripts created in the Java project I can't import anything. I always get ImportError: No module named ...

So I created a file called .jython in my home directory containing this:

python.path=/jproject/extras/2.5.3/Lib:/jproject/extras/2.5.3/Lib/site-packages

It seems to fix the import issue. But, is this the right way to proceed?

I think I don't have to do the last step manually and probably I installed Jython badly.

Upvotes: 2

Views: 402

Answers (1)

vvladymyrov
vvladymyrov

Reputation: 5793

Looks like I have same issue described in my question here:

The solution I've come up is workaround that doesn't require any actions from user to set python.path: Basically I added code that sets python.path to application work directory (user.path) before initializing jython environment.

Upvotes: 1

Related Questions