Greg Scott | 27 Aug 12:31

Please explain how to get imports to work for embedded Jython in a .jar

Hello, I have been looking far and wide for an explanation of how to get imports to work as follows:

 

  1. I have a file mypython.py in a package called com.example

  2. I can successfully execute this python file using JSR223, getting the file as a stream etc.

  3. However, if in my python file I try and import another of my files, it always fails, e.g. in the same folder I have a python file myotherfile.py

 

The python code

 

# mypython.py

import myotherfile

 

Produces the error ImportError: no module named myotherfile

 

I am using Netbeans 6.1 if that makes a difference

 

Could someone please explain why this happens and advise how to fix it?

 

Thank you very much indeed.

 

Greg

 

-------------------------------------------------------------------------
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

Re: Please explain how to get imports to work for embedded Jython in a .jar

Hi Greg,
I think you have not set the path correctly. Try setting your 'python.path' to the location where you have your   myotherfile.py script (like below)  or you  can edit your python.path variable in your jython registry. If you need further information , browse the jython wiki - [1] . After you set your path correctly try importing your python module. It would probably work.

Properties props = new Properties();

props.setProperty("python.path", "/home/modules:scripts");

PythonInterpreter.initialize(System.getProperties(), props, new String[] {""});


[1] - http://wiki.python.org/jython/JythonFaq/EmbeddingJython


On Wed, Aug 27, 2008 at 4:04 PM, Greg Scott <gregory.scott99 <at> imperial.ac.uk> wrote:

Hello, I have been looking far and wide for an explanation of how to get imports to work as follows:

 

  1. I have a file mypython.py in a package called com.example
  2. I can successfully execute this python file using JSR223, getting the file as a stream etc.
  3. However, if in my python file I try and import another of my files, it always fails, e.g. in the same folder I have a python file myotherfile.py

 

The python code

 

# mypython.py

import myotherfile

 

Produces the error ImportError: no module named myotherfile

 

I am using Netbeans 6.1 if that makes a difference

 

Could someone please explain why this happens and advise how to fix it?

 

Thank you very much indeed.

 

Greg

 


-------------------------------------------------------------------------
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




--
Regards,
Heshan Suriyaarachchi

http://heshans.blogspot.com/
-------------------------------------------------------------------------
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
Dave Kuhlman | 29 Aug 22:15

Re: Please explain how to get imports to work for embedded Jython in a .jar

> So still it is not working.
> Could you by any chance send me a
> directory structure or jar that you know works on your system?
> Or any other ideas?
> 
> Thank you so much for your help

Greg -

I've attached a simple jar file and I've included (below) the two
scripts which I added to the jar.  I do not know whether this is
the best way to do this, but it seems to work.

Please let me know if this recipe does not work for you, because
that means that my instructions in the Jython Wiki need to be
corrected.  I did test this earlier this morning.

Here are a few
comments:

This is how I built the jar file:

    $ cp ../../../jythonstandalone2.2.1/jython.jar .
    $ mv jython.jar myapplication.jar
    $ zip myapplication.jar __run__.py
    $ zip -r myapplication.jar Lib

A few notes:

- The main script (__run__.py) is in the root in the jar file.

- The imported script is in the Lib directory within the jar file.

- I started with a jar file that I built by using the Jython
  installer and selecting "Standalone (a callable .jar file)" for
  the installation type.

And, here is how I run it:

    $ java -jar myapplication.jar -jar myapplication.jar

Here are the two scripts:

    # __run__.py

    import showstuff

    def test():
        print 'starting myapp'
        showstuff.show()
        print 'leaving myapp'

    test()

    # ------------------------------------------------

    # showstuff.py

    def show():
        print 'hi from function show'

Hope this helps.

- Dave

-------------------------------------------------------------------------
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=/
Dave Kuhlman | 1 Sep 19:35

Re: Please explain how to get imports to work for embedded Jython in a .jar

> From: Greg Scott <gregory.scott99 <at> imperial.ac.uk>
> To: Dave Kuhlman <dkuhlman <at> rexx.com>
> Sent: Sunday, August 31, 2008 1:22:54 PM
> Subject: RE: Please help with embedded Jython with .JAR question
>
> Hi Dave
>
> Thank you so much for this. But remember, my completed project is not of
> this form because the jython.jar is included in the classpath. The program
> remember is executed from a normal .class file and the Jython embedded via
> JS223. So there are 2 or more jars involved. I have tried putting a Lib
> directory in my My.jar but this is nothing to do with the library/jar of
> jython.jar which is included. Are you saying I have to alter the actual
> jython.jar to get this to work? Surely not?
>

Greg -

I believe that I've given you about as much help as I am capable
of.  I'm someone who has been able to come up with a few recipes
that, I hope, handle a reasonable number of common cases, but who
does not really understand the underlying theory well enough to go
much further.

I'm not saying that you *have to* modify the jython.jar.  But,
creating a replacement for it (which is what the stand-alone jar
is) might be one approach to filling your need.

And, if not, here are several questions that might suggest a
solution:

1. Do you really need to have your jython code in a jar?  Perhaps
   you can put your jython modules in a directory and then put that
   directory on your python.path variable in your jython registry
   file.

2. Or, is it that you need to package your jython modules into a
   single file, but not necessarily a jar file?  If so, you might put your
   jython modules in a .zip file, and put that .zip file on your
   python.path variable in your jython registry file.  For more on
   importing from zip files, see this PEP:

       http://www.python.org/dev/peps/pep-0273/

   I just tried this approach with jython 2.2.1.  Importing from a
   zip file seems to work fine.  Here is what I did:

   Created a zip archive containing my module:

       $ zip mylib.zip showstuff.py

   Added my zip file to python.path in my registry file:

       python.path = /path/to/my/library/mylib.zip

   Imported my module in jython:

       >>> import showstuff
       >>> showstuff.show()
       hi from function show

Hope something above suggests a solution to your needs.

- Dave

--

Dave Kuhlman
http://www.rexx.com/~dkuhlman

-------------------------------------------------------------------------
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=/

Gmane