3

i have piece of Java code, using Jython to call some Python library, this library rely on external Jython classic libraryies like re for regular expression and others, so i have this kind of code :

    PythonInterpreter interpreter = new PythonInterpreter(null, new PySystemState()); 
    PySystemState sys = Py.getSystemState(); 

    // below: so that my own library find necessary 'batteries included' Python libs
    sys.path.append(new PyString("C:/jython2.5.2/Lib")); // (path1)
    // below: i put my own Python library inside the Java package for clarity
    // and be able to package everything in jar, well, maybe is it no a good idea?
    // also i am wondering, because myfile.py remain inside /src subdirectories 
    // while after compiling Eclipse will put most in /bin subidrectories but not the *.py files
    // thus doing some:
    //    MyJythonFactory..class.getProtectionDomain().getCodeSource().getLocation().toString() 
    // will not help to rebuild the ..myJavaProject/src/my/domain/mypackage
    sys.path.append(new PyString("D:/eclipseWorkspace/myJavaProject/src/my/domain/mypackage")); (path2)

    interpreter.exec("from mylib import myfunc"); //(A)
    PyObject myPyFunction = interpreter.get("myfunc");

BTW, i do not like the fact that i need to change and hard code both path (path1) and (path2)

do you see an elegant way to avoid this and to add some 'whereami' autodetect path features (at least for (path2))?

and looks like i need both (path1) and (path2) => so that (A) works!

i can also reformulated my question as what elegant way would you find to do not have any ImportError: No module named myModule or how to avoid hardcoding something like:

 PySystemState sys = Py.getSystemState(); 
 sys.path.append("where/my/python/libs/live/while/they/are/at/the/same/place/as/my/current/java/class")

best regards

user1340802
  • 1,157
  • 4
  • 17
  • 36
  • You don't need to hardcode `C:/jython2.5.2/Lib`; a properly installed `jython.jar` should know where it lives. – Fred Foo Jul 05 '12 at 09:35
  • well, i had hadded that because it did not work without, please, what are steps that i could have missed so that it is not 'properly installed'? thx – user1340802 Jul 05 '12 at 12:01
  • I just ran the installer script and it worked without any trouble. Did you perhaps move the installation? – Fred Foo Jul 05 '12 at 12:10
  • well actually it works, but as inside `mypythonmodules.py` i have for example an `import re` modules and others, it looks like i need to give Jython.jar the path to these libraries, that is why i ended with adding this path. See: http://stackoverflow.com/questions/471000/jython-and-python-modules – user1340802 Jul 05 '12 at 12:55

1 Answers1

1

make sure you have the jython-standalone.jar not jython.jar in your project classpath because jython-standalone.jar has Python Lib inside the jar so that you don't need append it to sys.path.

if you are using maven, you can add this to pom.xml :

<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython-standalone</artifactId>
    <version>2.7.0</version>
</dependency>

or you can download jar here.

for you own python module put it in the java project classpath, Jython use java classplath as __pyclasspath__, so you avoid hardcoding.

kk17
  • 601
  • 6
  • 14