You need to "Add framework support" / Facets
As long as you already have python installed, just follow this.
Right-click the directory/module/project where you have your python scripts, right at the top you will see "Add framework Support..."
https://www.jetbrains.com/help/idea/adding-support-for-frameworks-and-technologies.html#disable-framework-auto-detection
Answer from Miguel Armenta on Stack OverflowYou need to "Add framework support" / Facets
As long as you already have python installed, just follow this.
Right-click the directory/module/project where you have your python scripts, right at the top you will see "Add framework Support..."
https://www.jetbrains.com/help/idea/adding-support-for-frameworks-and-technologies.html#disable-framework-auto-detection
What worked better for me was to go to
Project Structure > Project Settings > Modules
then select my module, on the right-hand side pane choose Dependencies,
and from there, I set the Python SDK via the Module SDK: drop down

Is there a way to use python and java in the same program? - Stack Overflow
Integration of Java and Python Code in One Eclipse Project - Stack Overflow
can both Java and Python be used at the same time on App Engine? - Stack Overflow
Java Python Integration - Stack Overflow
Videos
You need to "Add framework support" / Facets
As long as you already have python installed, just follow this.
Right-click the directory/module/project where you have your python scripts, right at the top you will see "Add framework Support..."
https://www.jetbrains.com/help/idea/adding-support-for-frameworks-and-technologies.html#disable-framework-auto-detection
Answer from Miguel Armenta on Stack OverflowMy 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.
If I'm interpreting correctly, you want to use to use both interchangeably in the same file, so you'd end up with code like:
def main():
int x = 5;
print(x)
This is impossible, because there would be ambiguity when trying to interpret code if you allowed constructs from both languages. For example, "X" + 1 is allowed in java, and would give you the string "X1". In python, it would give you an error because you can't add an int to a string. This would mean that there would be no way to know what your code should do because it's runnable in both languages.
This is a problem that all of us face, where we like some parts of some languages and other parts of other languages. The solution is pretty much just to decide what's most important, choose one language based on that, and then put up with the parts you don't like.
You can use Jython, which is a Python implementation based on the JVM/JDK. This allows calling between Java and Python code in both directions.
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 had exactly the same need a week ago or so, and found a solution: Can Java and Python coexist in the same app?
EDIT: I accepted the first answer, but it happens that the second answer gave me best results, as i explain on the comments there.
EDIT 2: Well, it might not be exactly the same need, as i only needed to access the same datastore. If you want Java and Python running side-by-side, then i think Jython would be the best solution, as larsmans mentioned above.
Use py4j to communicate java and python and you need only secret id or user id to connect with google app engine .But if you need standalone application for both then you will need yaml file and their configuration.
from py4j.java_gateway import JavaGateway
gateway = JavaGateway() # connect to the JVM
gateway.jvm.java.lang.System.out.println('Hello World!')
Alternatively you can use google app engine plugin for eclipse (if you are using) and deploy java project using run->run as-> Google Web Application and integrate python using Jython. https://developers.google.com/eclipse/docs/creating_new_webapp?hl=en
Why not use Jython? The only downside I can immediately think of is if your library uses CPython native extensions.
EDIT: If you can use Jython now but think you may have problems with a later version of the library, I suggest you try to isolate the library from your app (e.g. some sort of adapter interface). Go with the simplest thing that works for the moment, then consider JNI/CPython/etc if and when you ever need to. There's little to be gained by going the (painful) JNI route unless you really have to.
Frankly most ways to somehow run Python directly from within JVM don't work. They are either not-quite-compatible (new release of your third party library can use python 2.6 features and will not work with Jython 2.5) or hacky (it will break with cryptic JVM stacktrace not really leading to solution).
My preferred way to integrate the two would use RPC. XML RPC is not a bad choice here, if you have moderate amounts of data. It is pretty well supported โ Python has it in its standard library. Java libraries are also easy to find. Now depending on your setup either Java or Python part would be a server accepting connection from other language.
A less popular but worth considering alternative way to do RPCs is Google protobuffers, which have 2/3 of support for nice rpc. You just need to provide your transport layer. Not that much work and the convenience of writing is reasonable.
Another option is to write a C wrapper around that pieces of Python functionality that you need to expose to Java and use it via JVM native plugins. You can ease the pain by going with SWIG SWIG.
Essentially in your case it works like that:
- Create a SWIG interface for all method calls from Java to C++.
- Create C/C++ code that will receive your calls and internally call python interpreter with right params.
- Convert response you get from python and send it via swig back to your Java code.
This solution is fairly complex, a bit of an overkill in most cases. Still it is worth doing if you (for some reason) cannot afford RPCs. RPC still would be my preferred choice, though.