My project uses some packages that are available only in Python and heavily rely on C libraries. The project also greatly benefits from Java libraries and the JVM. What's the optimal way to call Python functions from Java?
I tried:
-
Small web-services: overhead to serialize data, start and stop the services. Also debugging is harder and implementing each new function is now double the effort.
-
Jpy: a library that runs an interpreter in the JVM. Spare the service start/stop, but: isn't really feasible for more than a single-liner, data translation between Java and Python is cumbersome, and I also encountered runtime segmentation fault errors.
Any other options?
The project is in the machine-learning domain, so involves exchanging large numeric arrays and text. In some cases the execution switches back and forth between the platforms.
Integration of Java and Python Code in One Eclipse Project - Stack Overflow
How to run a Java Project in Python? - Stack Overflow
Using Python from within Java - Stack Overflow
Looking for project ideas to learn Python coming over from Java.
Videos
You can keep things in separate projects without having to copy the .class files to the Python project provided that you make that project a PyDev project and add the place that contains the .class files (i.e.: bin folder) as a source folder in the PyDev configuration (and reference that project from your Python project).
Take a look at "Project reference for Jython users" (in the end of http://pydev.org/manual_101_project_conf2.html).
I think you could also do what you asked in your question: create a java project, set it as a PyDev project too (right click that project > pydev > set as pydev project) and configure the PyDev project properties setting the PYTHONPATH to the place where you have your Python files and add another entry to the bin folder (i.e.: where the .class files exist). Not sure if it'd be better to keep those as 2 separated projects though.
I think Jython would be ideal here, as the Python code essentially gets translated to Java bytecode and run on the Java virtual machine, making it easy to communicate between the two. Also, pydev itself integrates well with Jython.
I'm aware of the Jython project, but it looks like this represents a way to use Java and its libraries from within Python, rather than the other way round - am I wrong about this?
Yes, you are wrong. You can either call a command line interpreter to run python code using Jyton or use python code from Java. In the past there was also a python-to-Java compiler, but it got discontinued with Jython 2.2
I would write a Python module to handle the text and language processing, and then build a small bridge in jython that your java program can interact with. The jython bridge will be a very simple one, that's really only responsible for forwarding calls to the python module, and return the answer from the python module to the java module. Jython is really easy to use, and setup shouldn't take you more than 15 minutes.
Best of luck!
I'm a CS student and my school's basic programming courses are either offered in Java or Python. I chose Java because I like Minecraft (literally), but it seems like Python is sort-of the language to know right now, so I figure it's the best next one to start learning.
I would consider myself an intermediate Java coder, but compared to an intermediate coder in the working world, I'm probably still a baby.
I'm thinking of a text-based blackjack game for my first project. Good idea or no?
I'm someone who learned a ton of Java-related OOP, in terms of concepts like inheritance, syntax, polymorphism, typing, streams, multithreading, a lot of the tiny nooks and crannies related to Java...but I have no good OOP experience besides the projects I had to do in class, which weren't really applicable (besides one which used multithreading to make a text file about any number of other text files). For example, a project I had to make was a food-web simulator using trees, but the "simulation" was really just allowing tree navigation/addition/deletion via user input, and display via print statements.
On the other hand, I have more experience with Python projects: after all, there are a ton of handy libraries that are already made, it's just a matter of knowing what libraries to use, how to use them, and putting it all together. But my knowledge of python syntax isn't as strong: I'm not sure of all the extra versatility Python provides. For example, I would do all my coding interviews and practice in Java, even though the Python solution might be half as long, because I wouldn't know how to structure my code such that the Python solution is that short.
How would I go about starting Java projects? When should I use Java over Python, what libraries are important or handy?