473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

[(J)Python] embedding python

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

Jul 18 '05 #1
0 1967

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
23
by: Robey Holderith | last post by:
Anyone know a good way to embed python within python? Now before you tell me that's silly, let me explain what I'd like to do. I'd like to allow user-defined scriptable objects. I'd like to...
4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
2
by: Roose | last post by:
With some googling I have found these resources: http://docs.python.org/ext/win-dlls.html http://www.python.org/doc/faq/windows.html I have a large Win32/MFC/C/C++ application that has an...
2
by: Squirrel Havoc | last post by:
Hello. I am sorry if this has been asked before, but I am new here. If I recall correctly, Python can be used as a scripting language for other programs, as if the program had a builtin Python...
1
by: amit | last post by:
Hello, I am currently studying how to embedd python. I am developing a graphical C++ application. My goal is to embedd python script that will control some kind of animation. I have some...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
0
by: marcell71 | last post by:
I'm trying to get Boost.Python to working using the instructions here: http://www.boost.org/libs/python/doc/building.html. I am on step 3.1.4 and am getting a linking error that I can't figure out....
14
by: Robert Dailey | last post by:
Hi, Is there a C++ version of the C Python API packaged with python 2.5? It would be nice to have a OOP approach to embedding python in C++. It would also be a bonus if this C++ Python API...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.