Importing python module from python class

View: New views
4 Messages — Rating Filter:   Alert me  

Importing python module from python class

by Ryan Crumley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

All,

I have a piece of java code that uses a concept similar to
JythonFactory to load a python file, instantiate a python class and
return it to java so I can call methods on its interface. This is
working perfectly. Now I would like to enhance my python class by
using a python module provided by a 3rd party so I put this module in
the root of my classpath (lets call it 'chardet',
http://chardet.feedparser.org/) and add 'import chardet' to the top of
my python file.

As you may have guessed jython can not locate the module:

Exception in thread "main" Traceback (most recent call last):
  File "<iostream>", line 11, in <module>
ImportError: No module named chardet

Examining sys.path I see:

/Users/ryan/jython/2.5.0/Lib
/Users/ryan/jython/2.5.0/jython-2.5.0.jar/Lib
__classpath__
__pyclasspath__/

The reason I am loading the python file from an iostream and why I
would like the python module to live in the classpath is because I
need to ship this java program as a single self contained jar. Can
anyone give me advice on how best to set this up?

Thanks,

Ryan

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users

Re: Importing python module from python class

by David Handy-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> -----Original Message-----
> From: Ryan Crumley [mailto:crumley@...]
> Sent: Thursday, September 24, 2009 1:38 PM
> ...
> I have a piece of java code that uses a concept similar to
> JythonFactory to load a python file, instantiate a python class and
> return it to java so I can call methods on its interface. This is
> working perfectly. Now I would like to enhance my python class by
> using a python module provided by a 3rd party so I put this module in
> the root of my classpath (lets call it 'chardet',
> http://chardet.feedparser.org/) and add 'import chardet' to the top of
> my python file.
>
> As you may have guessed jython can not locate the module:
>
> ...
>
> The reason I am loading the python file from an iostream and why I
> would like the python module to live in the classpath is because I
> need to ship this java program as a single self contained jar. Can
> anyone give me advice on how best to set this up?

So you want to ship a Java program as a self-contained jar, and you have both Java and Jython code. It also sounds like you have control over what goes in this program.

If it were me, I'd seriously consider using the stand-alone jython jar and add your java classes to it. Put your main entry code in a __run__.py file in the root of the jar file. Then, instead of using a JythonFactory and calling Jython from Java, go the other direction and have your main python code instantiate Java classes as needed and call methods on them. If you have that much control over this program.

http://wiki.python.org/jython/InstallingJython#Standalonemode - how to get a stand-alone jython jar

http://wiki.python.org/jython/UserGuide#invoking-the-jython-interpreter - how to start __run__.py file in a jar

But, you probably want to make your current approach work, and that I can't help with much. Maybe someone else on the list can.

David H


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users

Re: Importing python module from python class

by Ryan Crumley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Unfortunately switching the direction from python->java is not an
option. Although even then I wonder how jython would resolve an import
statement from the __run__.py that is contained in the jar. What makes
that situation different than what I am trying to do now? Seems like I
am missing an important part of how jython molds python code into the
java classpath.

Ryan

On Thu, Sep 24, 2009 at 1:39 PM, David Handy <David.Handy@...> wrote:

>> -----Original Message-----
>> From: Ryan Crumley [mailto:crumley@...]
>> Sent: Thursday, September 24, 2009 1:38 PM
>> ...
>> I have a piece of java code that uses a concept similar to
>> JythonFactory to load a python file, instantiate a python class and
>> return it to java so I can call methods on its interface. This is
>> working perfectly. Now I would like to enhance my python class by
>> using a python module provided by a 3rd party so I put this module in
>> the root of my classpath (lets call it 'chardet',
>> http://chardet.feedparser.org/) and add 'import chardet' to the top of
>> my python file.
>>
>> As you may have guessed jython can not locate the module:
>>
>> ...
>>
>> The reason I am loading the python file from an iostream and why I
>> would like the python module to live in the classpath is because I
>> need to ship this java program as a single self contained jar. Can
>> anyone give me advice on how best to set this up?
>
> So you want to ship a Java program as a self-contained jar, and you have both Java and Jython code. It also sounds like you have control over what goes in this program.
>
> If it were me, I'd seriously consider using the stand-alone jython jar and add your java classes to it. Put your main entry code in a __run__.py file in the root of the jar file. Then, instead of using a JythonFactory and calling Jython from Java, go the other direction and have your main python code instantiate Java classes as needed and call methods on them. If you have that much control over this program.
>
> http://wiki.python.org/jython/InstallingJython#Standalonemode - how to get a stand-alone jython jar
>
> http://wiki.python.org/jython/UserGuide#invoking-the-jython-interpreter - how to start __run__.py file in a jar
>
> But, you probably want to make your current approach work, and that I can't help with much. Maybe someone else on the list can.
>
> David H
>
>

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users

Re: Importing python module from python class

by Ryan Crumley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I ended up using a java version of the same package and calling that
from python:

http://code.google.com/p/juniversalchardet/

Its not the most ideal setup however now I don't need to figure out
how to get jython to pick up the python module.

Ryan

On Thu, Sep 24, 2009 at 9:32 PM, Ryan Crumley <crumley@...> wrote:

> Unfortunately switching the direction from python->java is not an
> option. Although even then I wonder how jython would resolve an import
> statement from the __run__.py that is contained in the jar. What makes
> that situation different than what I am trying to do now? Seems like I
> am missing an important part of how jython molds python code into the
> java classpath.
>
> Ryan
>
> On Thu, Sep 24, 2009 at 1:39 PM, David Handy <David.Handy@...> wrote:
>>> -----Original Message-----
>>> From: Ryan Crumley [mailto:crumley@...]
>>> Sent: Thursday, September 24, 2009 1:38 PM
>>> ...
>>> I have a piece of java code that uses a concept similar to
>>> JythonFactory to load a python file, instantiate a python class and
>>> return it to java so I can call methods on its interface. This is
>>> working perfectly. Now I would like to enhance my python class by
>>> using a python module provided by a 3rd party so I put this module in
>>> the root of my classpath (lets call it 'chardet',
>>> http://chardet.feedparser.org/) and add 'import chardet' to the top of
>>> my python file.
>>>
>>> As you may have guessed jython can not locate the module:
>>>
>>> ...
>>>
>>> The reason I am loading the python file from an iostream and why I
>>> would like the python module to live in the classpath is because I
>>> need to ship this java program as a single self contained jar. Can
>>> anyone give me advice on how best to set this up?
>>
>> So you want to ship a Java program as a self-contained jar, and you have both Java and Jython code. It also sounds like you have control over what goes in this program.
>>
>> If it were me, I'd seriously consider using the stand-alone jython jar and add your java classes to it. Put your main entry code in a __run__.py file in the root of the jar file. Then, instead of using a JythonFactory and calling Jython from Java, go the other direction and have your main python code instantiate Java classes as needed and call methods on them. If you have that much control over this program.
>>
>> http://wiki.python.org/jython/InstallingJython#Standalonemode - how to get a stand-alone jython jar
>>
>> http://wiki.python.org/jython/UserGuide#invoking-the-jython-interpreter - how to start __run__.py file in a jar
>>
>> But, you probably want to make your current approach work, and that I can't help with much. Maybe someone else on the list can.
>>
>> David H
>>
>>
>

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users