Skip to content Skip to sidebar Skip to footer

Calling Python Script From Java Code Using Runtime.exec() : Importerror: No Module Named Sklearn

I Have a Java program that calls a python script. That python script has the following imports import numpy as np from sklearn import metrics On my mac, i already have python 2

Solution 1:

Try using 'python3' :

Process p = Runtime.getRuntime().exec("python3 /mydir/report.py");

Or alternatively, you can try:

ProcessBuilderpb=newProcessBuilder("python", "/mydir/report.py"); /*Or python3*/Processprocess= pb.start();

For detailed usage of ProcessBuilder : https://examples.javacodegeeks.com/core-java/lang/processbuilder/java-lang-processbuilder-example/

Solution 2:

Make sure that you are using the correct version of python. In some OS's, python3.x is called on the command line as python3 and therefore you will need to change your exec code to "python3 /mydir/report.py".

If you are using python3, pip will be shipped with the install, and therefore, you may also need to run pip install -U scikit-learn.

Solution 3:

The answer is in your question: No module named sklearn. Try to install sklearn :

pip install -U scikit-learn

Post a Comment for "Calling Python Script From Java Code Using Runtime.exec() : Importerror: No Module Named Sklearn"