Simple example using runScript

1,255 views
Skip to first unread message

bostock83

unread,
Feb 15, 2017, 11:15:47 AM2/15/17
to Jep Project
Hi,

I am trying to use Jep to call a method in a python script, passing some data across and receiving the result in java.

Here is my simple test code:

multiplier.py:

def multiply(val):
result = val * 2
return result

Main.Java:

package jep.testing;

import jep.Jep;
import jep.JepException;

public class Main {
public static void main(String[] args) {
try(Jep jep = new Jep(false, "<path to multiplier.py>"))
{
jep.eval("import multiplier");
jep.eval("result = multiplier.multiply(2)");
Object result = jep.getValue("result");

System.out.println("Result from eval: " + result);

jep.runScript("multiplier.py");
Object ret = jep.invoke("multiply", 2);
System.out.println("Result from runscript and invoke: " + ret);
} catch (JepException e) {
e.printStackTrace();
}
}
}

multiplier.py is at the same location where the java program is ran so no need to give the full path (or is there?)

When I run this I get the following output:
Result from eval: 4

Process finished with exit code -1073740777 (0xC0000417)

You can see the eval version is working fine but the runscript version seems to not do anything for a while and then I get the exit code.

Is there something I am doing wrong?

Any help you can provide would be greatly appreciated.

Also what is the syntax for invoking a method in a class using the runscript example? i.e. if the multiply method was in a class called Multiplier?

Regards,
Mike

Mike

unread,
Feb 16, 2017, 9:49:53 AM2/16/17
to Jep Project
It turns out I had not built Jep correctly. I had python 3.4 and when I installed Jep via pip install it was complaining about missing vcvarsall.dat for visual c++ 2010. I thought I could just point it to visual studio c++ 2015 which seemed to build it fine.
I have just upgraded to python 3.5 and built with visual studio c++ 2015 and it now gives output for both ways.

I need to now test it with more complex types i.e. passing a struct/class to python.

Nathan Jensen

unread,
Feb 17, 2017, 11:40:11 AM2/17/17
to Jep Project
Hi.  Yeah your first attempt was crashing on the runScript() or invoke() call, I'm not sure which.  I'm curious as to how you got it to build with Visual Studio C++ 2015 as the current build instructions on the wiki page require older versions of MSVC.  I'm sure others would find it useful to know how to build with the 2015 version, but it sounds like it worked with Python 3.5 and not 3.4?

The include paths for Jep just add directories to Python's sys.path for searching for modules for Python imports.  Since you didn't need to include the path to multiplier.py, if I remember correctly then by default "." is included in sys.path and that's where it found multiplier.py.

runScript("multiplier.py") is roughly equivalent to eval("import multiplier") except the runScript would put the multiply method in the global namespace whereas importing multiplier would keep the multiply method in the multiplier module.

For invoking a method on a class, I would use eval(String).  You will need to instantiate the class.  Something like:

jep.eval("from multiply import Multiplier");  // import Multiplier class
jep.eval("m = Multiplier()");  // instantiate Python class
jep.eval("result = m.multiply(2)");  // call instance method
Object result = jep.getValue("result"); // get result


--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.
To post to this group, send email to jep-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/300caa63-7a38-41fb-8eb7-68ae2630f0ff%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Mike

unread,
Feb 20, 2017, 5:26:55 AM2/20/17
to Jep Project
Hi,

I managed to build with visual studio 2015 by setting the environment variable VS100COMNTOOLS to the location of Visual studio 2015/Common7/Tools.
I am a bit new to the python world and installing using pip etc. so when I got the "missing vcvarsall.dat" message I did a bit of googling and found a post about doing this. This obviously caused the issue I got when I tried to run jep (with python 3.4) so it's probably not recommended to follow my example. 

Switching to python 3.5 and then installing via pip seemed to have no issues building with 2015.

I managed to work out that I probably needed to call eval when invoking a method on a class after a bit of playing. I guess this can't be done with the invoke because you need the instance of the class to call it on.

Thanks for your help, after playing with Jep a bit I'm starting to understand how to use it.

Mike
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project...@googlegroups.com.

To post to this group, send email to jep-p...@googlegroups.com.

Nathan Jensen

unread,
May 30, 2017, 7:06:34 PM5/30/17
to Jep Project
Hi Mike, Python changed the version of MSVC they use to build with Python 3.5.  I was unaware of that until last week.  Since you got Python 3.5 working with Jep on Windows, do you have any insights to share?  It's proving a problem to get it working on Windows.  The Jep wiki is out-of-date since it does not account for Python 3.5+ using a newer MSVC.

If you have any insights, if you could share them here on the mailing list or on the following ticket it would be appreciated: https://github.com/mrj0/jep/issues/80

Mike

unread,
Jun 1, 2017, 4:41:28 AM6/1/17
to Jep Project
Hi Nathan,

I don't remember doing anything special to build with 2015 (or MSVC 14.0 just to confuse things). Looking at my jep.dll in dependency walker it is dependent on vcruntime140.dll which does suggest it has been built with the correct compiler.
I am on windows 7 and I got a vanilla windows python installer from python.org (version 3.5.3). I then installed jep 3.7 from source as I needed the numpy shared memory feature and had no issues building.

Regards,
Mike


On Wednesday, 15 February 2017 16:15:47 UTC, Mike wrote:

Nathan Jensen

unread,
Jun 2, 2017, 10:17:36 AM6/2/17
to Jep Project
Thanks for the feedback.  At the very least I need to update the wiki with instructions about the different compiler for python 3.5+.  One of these weekends I will try to get python 3.5 on Windows 7 and write down each step.

--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.

To post to this group, send email to jep-p...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages