[(J)Python] embedding python

vincent Salaun vincent.salaun at zslide.com
Fri Mar 19 05:17:58 EST 2004


hi all,

here's my problem :

I've embedded a python interpreter in our java application (based on the 
NetBeans palteforrm) using the Jython API :
http://www.jython.org/docs/javadoc/index.html

So, i've used the PythonInterpreter class to instanciate an interpreter 
and to integrate it but the its environnement doesn't seem to be the 
same as the standard jython interpreter (the one that is gived when you 
install jython on your machine and that you can use in a DOS shell for 
example).

So i've encountered some problems using this embed interpreter : i 
test/import/script some stuff in the 'standard' interpreter but it 
doesn't always work using my 'embed' interpreter. For example :

----------------------------------------------------------------
C:\jython-2.1\jython
 >>> import urllib
 >>>print
urllib.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl').read()
----------------------------------------------------------------

#it displays :
<TITLE>What time is it?</TITLE>
<H2> US Naval Observatory Master Clock Time</H2> <H3>
<BR>Mar. 19, 08:56:01 UTC
<BR>Mar. 19, 03:56:01 AM EST
<BR>Mar. 19, 02:56:01 AM CST
<BR>Mar. 19, 01:56:01 AM MST
<BR>Mar. 19, 12:56:01 AM PST
<BR>Mar. 18, 11:56:01 PM YST
<BR>Mar. 18, 10:56:01 PM AST
</H3></B><P><A HREF="http://tycho.usno.navy.mil" 
<http://tycho.usno.navy.mil%22>;>Time Service Department, US Nav


Now, doing the same thing in my 'embed' :  interpreter :

----------------------------------------------------------
 >>>import urllib
Traceback (innermost last):
File "<string>", line 1, in ?
File "c:\jython-2.1\Lib\urllib.py", line 44, in ?
File "c:\jython-2.1\Lib\javaos.py", line 32, in ?
File "c:\jython-2.1\Lib\re.py", line 7, in ?
File "c:\jython-2.1\Lib\sre.py", line 17, in ?
File "c:\jython-2.1\Lib\sre_compile.py", line 15, in ?
AttributeError: class 'org.python.modules._sre' has no attribute 'MAGIC'
----------------------------------------------------------

It is strange because if i do import again :

--------------------------------
 >>>import urllib
 >>>
--------------------------------

#it doesn't tell me anything anymore but ...

-------------------------------------------------------------------------
 >>>print 
urllib.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl').read()
Traceback (innermost last):
File "<string>", line 1, in ?
AttributeError: module 'urllib' has no attribute 'urlopen'
#it still doesn't work ...
--------------------------------------------------------------------------


I have set 2 environnement values :
DOS> echo %PYTHONPATH%
-->
.;C:\jython-2.1;C:\jython-2.1\Lib;c:\python21;c:\python21\Lib;D:\jython\Lib;d:\jython\Lib\test

(my modules live in 'd:\jython\Lib ')

DOS> echo %PYTHON_HOME%
--> c:\jython-2.1

In my embed interpreter, i couldnt import my own modules from 
'd:\jython\Lib', but i solved the problem using the 
PythonInterpreter.initialize(...) function :

-------------------------------------------------------------------------------------------------------------------------------
//in my java app :
import java.io.Writer;
import org.python.util.PythonInterpreter;
import org.openide.nodes.Node;
import java.util.Properties;
/**
* This class is used to interpret command lines in python/jython
* It always goes with a Python console class
*/
public class ZJythonInterpreter {
    /**
     * Default Constructor
     * creates a new interpreter and redirects its standard output and 
error output on the console's output
     * @param out - console's output
     * */
    public ZJythonInterpreter(Writer out)
    {
    Properties props = new Properties();
    props.setProperty("python.home", "c:\\jython-2.1");
    props.setProperty("python.path", 
"c:\\jython-2.1;c:\\jython-2.1\\Lib;c:\\python21;c:\\python21\\Lib;d:\\jython\\Lib;d:\\jython\\Lib\\test");
    PythonInterpreter.initialize(System.getProperties(),props,new 
String[0]);

        this.interpreter = new PythonInterpreter();
        this.interpreter.setOut(out);
        this.interpreter.setErr(out);
    // ....
}
//...
}
--------------------------------------------------------------------------------------------------------------------------------------


For information, i'm using jython 2.1 with python 2.1.3 and jdk1.4.2 on 
window$ 2000


Any idea will be welcomed... i'm so bored with this problem
thx

vince




More information about the Python-list mailing list