Re: Using Python Module (pygments) with Java
My experience is that if you put the stand-alone jython.jar in your WEB-INF/lib directory, you don't have
to set python.home. That jar *is* your python home.
And, if you use the modjy and its default settings, you don't have to set sys.path either. Your main
application .py file is by default searched for in your servlet context root directory, and therefore
that directory gets added to sys.path when your application runs. That makes configuration very simple.
So, for my Jython webapp that uses modjy and the stand-alone Jython jar, the directory structure looks like this:
mywebapp/
WEB-INF/
web.xml
lib/
jython.jar (stand-alone version)
modjy.jar
Other Java jar files as needed
dispatch.py
Other Python files and packages as needed
*.html, *.ini, etc.
And my web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>mywebapp</display-name>
<description>
My Jython/Java Web application
</description>
<servlet>
<servlet-name>modjy</servlet-name>
<servlet-class>com.xhaus.modjy.ModjyJServlet</servlet-class>
<init-param>
<param-name>app_filename</param-name>
<param-value>dispatcher.py</param-value>
</init-param>
<init-param>
<param-name>cache_callables</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>exc_handler</param-name>
<!--<param-value>testing</param-value>-->
<param-value>standard</param-value>
</init-param>
<init-param>
<param-name>reload_on_mod</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>log_level</param-name>
<param-value>debug</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>modjy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
And inside dispatcher.py:
def handler(environ, start_response):
"""The WSGI handler "callable object". See:
http://www.xhaus.com/modjy/locating.html#locating_callables
WSGI spec:
http: http://www.python.org/dev/peps/pep-0333/
"""
Works great for me...
David H
> -----Original Message-----
> From: jython-users-bounces <at> lists.sourceforge.net [mailto:jython-users-
> bounces <at> lists.sourceforge.net] On Behalf Of Raymi
> Sent: Wednesday, July 30, 2008 9:01 AM
> To: jython-users <at> lists.sourceforge.net
> Subject: Re: [Jython-users] Using Python Module (pygments) with Java
>
> Hi,
>
> I see your problem. I'm not sure whether it would help, if one uses the
> following lines to set python.path instead of setting the system
> property directly:
>
> Properties props = new Properties();
> props.put("python.home", pythonPath);
> PythonInterpreter.initialize(System.getProperties(), props, new
> String[]
> {});
>
> However I don't know how PythonInterpreter.initialize is implemented
> and
> whether it will have side effects to other web applications if you do
> it.
>
> What I have also seen on the internet when investiagting this topic was
> code that added the application's lib directory to sys.path with python
> (i.e. pythonInterpreter.exec("import sys") and so on) and removed it
> explicitly after the script was executed. But that looks like a real
> workaround for me.
>
> Cheers,
>
> raymi
>
> Stefan Eischet schrieb:
> > Hello all,
> >
> > a followup question about this:
> >
> > System.setProperty("python.home", sc.getRealPath("/WEB-INF/lib"));
> >
> > That's a global property, right? What will happen when multiple
> > applications running on the same server set this property to
> different
> > values? Especially, what will happen to sys.path?
> >
> > I'm asking because I sometimes run into trouble with Jython module
> > search paths when multiple applications (packaged as zipped .war
> > files, and usually including the Jython 2.2 standalone .jar) run on
> > the same application server. To me it looks like only the first app
> to
> > be deployed has a chance to set the Python home, and all apps
> deployed
> > later will use that home folder and sys.path. Which means that, if I
> > put app-specific modules into /WEB-INF/lib/Lib/..., only the first
> app
> > can find its modules. What I usually do to work around this is to
> > create an extra folder for the app's modules and add that folder to
> > sys.path explicitly in every application, but this feels really
> > tedious and error-prone. Also, I have to be very careful not to have
> > two modules with the same name in two different apps, because then
> > again the order of deployment can become an issue.
> >
> > If you fully control the app server, you can always work around this,
> > but let's assume a mutual customer of Raymi and me puts his app and
> > mine on the same app server and expects both to just work. If my app
> > loads first, his app won't find its modules, and vice versa, right?
> >
> > Any thoughts on this?
> >
> > Regards
> > Stefan Eischet
> >
> >
> > On 2008-07-30, at 11:46 , Raymi wrote:
> >
> >> Hi all,
> >>
> >> thanks for your hint Andy. I copied the jython Lib folder to
> >> /WEB-INF/lib/, copied the pygment module there as well and set the
> >> python.home path with System.setProperty and - WOW! - it works.
> >> Thanks a
> >> lot for all your help.
> >>
> >> There were (and are) some issues left that are not related to the
> >> original question but to compatiblity issues between the jython
> >> version
> >> and the pygments version (I had to use jython 2.5 alpha and am not
> >> able
> >> to parse python code with pygments due to a strange unicode error).
> >> But
> >> it's not that bad at the moment.
> >>
> >> Thanks again all of you for helping me with this thing.
> >> Cheers,
> >>
> >> raymi
> >> Michael Chisholm schrieb:
> >>> Where "sc" is the servlet context object. Forgot that bit :)
> >>>
> >>> Andy
> >>>
> >>> Michael Chisholm wrote:
> >>>> We use jython from a webapp, but its use is embedded within the
> >>>> servlet,
> >>>> rather than being the servlet itself (so we don't have a mapping
> >>>> from
> >>>> *.py to a PyServlet). It's been awhile since I had to get this
> >>>> working,
> >>>> but I had to add some code to our ContextListener to point jython
> >>>> in the
> >>>> right place when the servlet starts up. Our directory structure
> >>>> is very
> >>>> similar to Sean's. In contextInitialized():
> >>>>
> >>>> System.setProperty("python.home", sc.getRealPath("/WEB-INF/
> >>>> lib"));
> >>>>
> >>>> We also run our own scripts, which are bundled with the .war
> >>>> file... I
> >>>> also added:
> >>>>
> >>>> System.setProperty("python.path",
> >>>> sc.getRealPath("/WEB-INF/lib/scripts"));
> >>>>
> >>>> so that it could find them.
> >>>>
> >>>> Andy
> >>>>
> >>>> Moore, Greg wrote:
> >>>>
> >>>>> Hi Dominik,
> >>>>>
> >>>>> What I as referring to was to put the files _inside_ the
> >>>>> Jython.jar. But
> >>>>> I'm not sure if that's good advice in an app server environment
> >>>>> but it
> >>>>> worked for my standalone app. Sorry but I don't have a lot of
> >>>>> experience
> >>>>> using Jython with web apps. Maybe this would be of more help
> >>>>> http://seanmcgrath.blogspot.com/JythonWebAppTutorial.html
> >>>>>
> >>>>> Greg.
> >>>>>
> >>>>>
> >>>>> -----Original Message-----
> >>>>> From: jython-users-bounces <at> lists.sourceforge.net
> >>>>> [mailto:jython-users-bounces <at> lists.sourceforge.net] On Behalf Of
> >>>>> Raymi
> >>>>> Sent: Tuesday, July 29, 2008 2:09 AM
> >>>>> To: jython-users <at> lists.sourceforge.net
> >>>>> Subject: Re: [Jython-users] Using Python Module (pygments) with
> >>>>> Java
> >>>>>
> >>>>> Greg,
> >>>>>
> >>>>> thanks for your reply. I tried to copy the pygments folder to my
> >>>>> lib
> >>>>> directroy, however the module wasn't found ("from pygments import
> >>>>> highlight" failed). Can you explain how the directory structure
> >>>>> would
> >>>>> look like, i.e. which folder of the pygments module I will have
> >>>>> to copy
> >>>>> to what location? My application is a web application, so the
> >>>>> structure
> >>>>> looks as follows:
> >>>>>
> >>>>> /WEB-INF/lib
> >>>>> jython.jar
> >>>>>
> >>>>> Thanks for your help,
> >>>>>
> >>>>> Dominik
> >>>>>
> >>>>> Moore, Greg schrieb:
> >>>>>
> >>>>>
> >>>>>> Have you tried putting the pigment files in the Lib directory of
> >>>>>> the
> >>>>>>
> >>>>>>
> >>>>> jar
> >>>>>
> >>>>>
> >>>>>> file?
> >>>>>>
> >>>>>> Greg.
> >>>>>>
> >>>>>>
> >>>>>> -----Original Message-----
> >>>>>> From: jython-users-bounces <at> lists.sourceforge.net
> >>>>>> [mailto:jython-users-bounces <at> lists.sourceforge.net] On Behalf Of
> >>>>>> Raymi
> >>>>>> Sent: Saturday, July 26, 2008 7:42 AM
> >>>>>> To: jython-users <at> lists.sourceforge.net
> >>>>>> Subject: [Jython-users] Using Python Module (pygments) with Java
> >>>>>>
> >>>>>> Hi,
> >>>>>>
> >>>>>> I'd like to use the pygments syntax highlighting module from
> >>>>>> within a
> >>>>>> JAVA application. I understand how I can use the
> >>>>>> PythonInterpreter to
> >>>>>> execute single python statements or whole files. However I don't
> >>>>>> understand how I can load the whole pygments module and use it.
> In
> >>>>>>
> >>>>>>
> >>>>> order
> >>>>>
> >>>>>
> >>>>>> to deploy the application easily I don't want to install Jython
> >>>>>> on the
> >>>>>>
> >>>>>>
> >>>>>
> >>>>>> system and then call pygment's installer from the jython shell.
> >>>>>> I just
> >>>>>>
> >>>>>>
> >>>>>
> >>>>>> want to deploy the jython jar archive and the pygments module
> >>>>>> (as egg
> >>>>>> file) and then load the module once during startup. Is this
> >>>>>> possible?
> >>>>>>
> >>>>>> Thanks in advance for your help.
> >>>>>> Cheers,
> >>>>>>
> >>>>>> raymi
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>> -----------------------------------------------------------------
> -------
> >>>>>
> >>>>>
> >>>>>> -
> >>>>>> This SF.Net email is sponsored by the Moblin Your Move
> Developer's
> >>>>>> challenge
> >>>>>> Build the coolest Linux based applications with Moblin SDK & win
> >>>>>> great
> >>>>>> prizes
> >>>>>> Grand prize is a trip for two to an Open Source event anywhere
> >>>>>> in the
> >>>>>> world
> >>>>>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> >>>>>> _______________________________________________
> >>>>>> Jython-users mailing list
> >>>>>> Jython-users <at> lists.sourceforge.net
> >>>>>> https://lists.sourceforge.net/lists/listinfo/jython-users
> >>>>>>
> >>>>>>
> >>>>>> This message and any attachments are intended only for the use
> >>>>>> of the
> >>>>>>
> >>>>>>
> >>>>> addressee and may contain information that is privileged and
> >>>>> confidential. If the reader of the message is not the intended
> >>>>> recipient
> >>>>> or an authorized representative of the intended recipient, you
> are
> >>>>> hereby notified that any dissemination of this communication is
> >>>>> strictly
> >>>>> prohibited. If you have received this communication in error,
> >>>>> please
> >>>>> notify us immediately by e-mail and delete the message and any
> >>>>> attachments from your system.
> >>>>>
> >>>>>
> >>>>>>
> >>>>> -----------------------------------------------------------------
> -------
> >>>>> -
> >>>>>
> >>>>>
> >>>>>> This SF.Net email is sponsored by the Moblin Your Move
> Developer's
> >>>>>>
> >>>>>>
> >>>>> challenge
> >>>>>
> >>>>>
> >>>>>> Build the coolest Linux based applications with Moblin SDK & win
> >>>>>> great
> >>>>>>
> >>>>>>
> >>>>> prizes
> >>>>>
> >>>>>
> >>>>>> Grand prize is a trip for two to an Open Source event anywhere
> >>>>>> in the
> >>>>>>
> >>>>>>
> >>>>> world
> >>>>>
> >>>>>
> >>>>>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> >>>>>> _______________________________________________
> >>>>>> Jython-users mailing list
> >>>>>> Jython-users <at> lists.sourceforge.net
> >>>>>> https://lists.sourceforge.net/lists/listinfo/jython-users
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>> -----------------------------------------------------------------
> -------
> >>>>> -
> >>>>> This SF.Net email is sponsored by the Moblin Your Move
> Developer's
> >>>>> challenge
> >>>>> Build the coolest Linux based applications with Moblin SDK & win
> >>>>> great
> >>>>> prizes
> >>>>> Grand prize is a trip for two to an Open Source event anywhere in
> >>>>> the
> >>>>> world
> >>>>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> >>>>> _______________________________________________
> >>>>> Jython-users mailing list
> >>>>> Jython-users <at> lists.sourceforge.net
> >>>>> https://lists.sourceforge.net/lists/listinfo/jython-users
> >>>>>
> >>>>>
> >>>>> This message and any attachments are intended only for the use of
> >>>>> the addressee and may contain information that is privileged and
> >>>>> confidential. If the reader of the message is not the intended
> >>>>> recipient or an authorized representative of the intended
> >>>>> recipient, you are hereby notified that any dissemination of this
> >>>>> communication is strictly prohibited. If you have received this
> >>>>> communication in error, please notify us immediately by e-mail
> >>>>> and delete the message and any attachments from your system.
> >>>>>
> >>>>> -----------------------------------------------------------------
> --------
> >>>>> This SF.Net email is sponsored by the Moblin Your Move
> >>>>> Developer's challenge
> >>>>> Build the coolest Linux based applications with Moblin SDK & win
> >>>>> great prizes
> >>>>> Grand prize is a trip for two to an Open Source event anywhere in
> >>>>> the world
> >>>>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> >>>>> _______________________________________________
> >>>>> Jython-users mailing list
> >>>>> Jython-users <at> lists.sourceforge.net
> >>>>> https://lists.sourceforge.net/lists/listinfo/jython-users
> >>>>>
> >>>>>
> >>>>>
> >>>>
> >>>> ------------------------------------------------------------------
> -------
> >>>> This SF.Net email is sponsored by the Moblin Your Move Developer's
> >>>> challenge
> >>>> Build the coolest Linux based applications with Moblin SDK & win
> >>>> great prizes
> >>>> Grand prize is a trip for two to an Open Source event anywhere in
> >>>> the world
> >>>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> >>>> _______________________________________________
> >>>> Jython-users mailing list
> >>>> Jython-users <at> lists.sourceforge.net
> >>>> https://lists.sourceforge.net/lists/listinfo/jython-users
> >>>>
> >>>>
> >>>>
> >>>
> >>>
> >>> -------------------------------------------------------------------
> ------
> >>> This SF.Net email is sponsored by the Moblin Your Move Developer's
> >>> challenge
> >>> Build the coolest Linux based applications with Moblin SDK & win
> >>> great prizes
> >>> Grand prize is a trip for two to an Open Source event anywhere in
> >>> the world
> >>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> >>> _______________________________________________
> >>> Jython-users mailing list
> >>> Jython-users <at> lists.sourceforge.net
> >>> https://lists.sourceforge.net/lists/listinfo/jython-users
> >>>
> >> --------------------------------------------------------------------
> -----
> >> This SF.Net email is sponsored by the Moblin Your Move Developer's
> >> challenge
> >> Build the coolest Linux based applications with Moblin SDK & win
> >> great prizes
> >> Grand prize is a trip for two to an Open Source event anywhere in
> >> the world
> >> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> >> _______________________________________________
> >> Jython-users mailing list
> >> Jython-users <at> lists.sourceforge.net
> >> https://lists.sourceforge.net/lists/listinfo/jython-users
> >
> >
> > ---------------------------------------------------------------------
> ----
> > This SF.Net email is sponsored by the Moblin Your Move Developer's
> challenge
> > Build the coolest Linux based applications with Moblin SDK & win
> great prizes
> > Grand prize is a trip for two to an Open Source event anywhere in the
> world
> > http://moblin-contest.org/redirect.php?banner_id=100&url=/
> > _______________________________________________
> > Jython-users mailing list
> > Jython-users <at> lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/jython-users
> >
>
> -----------------------------------------------------------------------
> --
> This SF.Net email is sponsored by the Moblin Your Move Developer's
> challenge
> Build the coolest Linux based applications with Moblin SDK & win great
> prizes
> Grand prize is a trip for two to an Open Source event anywhere in the
> world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> Jython-users mailing list
> Jython-users <at> lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jython-users
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/