-1

I am trying to call python script from java code.

public class PythonTest {
    public static void main(String[] args) throws IOException, ScriptException, InterruptedException {
        ProcessBuilder pb = new ProcessBuilder().inheritIO().command("C:/Users/emon/Anaconda3/python.exe",
                "forecast.py");
        Process process = pb.start();
        process.waitFor();
        InputStream inputStream = process.getInputStream();

        int i = 0;
        StringBuilder stringBuffer = new StringBuilder();
        while ((i = inputStream.read()) != -1) {
            stringBuffer.append((char) i);
        }
        System.out.println(stringBuffer.toString());
      }
}

Basically, I used python script for forecasting some given time series data. I used facebook prophet to do that. In addition, I used Anaconda. So, in the command parenthesis, I used C:/Users/emon/Anaconda3/python.exe.

from fbprophet import Prophet
import numpy as np
import pandas as pd
# import matplotlib.pyplot as plt

# plt.rcParams['figure.figsize'] = (20, 10)
# plt.style.use('ggplot')

sales_df = pd.read_csv('multiTimeline.csv')
sales_df['y_orig'] = sales_df['y']
sales_df['y'] = np.log(sales_df['y'])
model = Prophet()
model.fit(sales_df)
future_data = model.make_future_dataframe(periods=50, freq='w')
forecast_data = model.predict(future_data)
forecast_data[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
model.plot(forecast_data)
print(forecast_data)

# for index, row in forecast_data.iterrows():
#     print(
#         str(index) + " " + str(row['ds']) + " " + str(np.exp(row['yhat'])) + " " + str(
#             np.exp(row['yhat_lower'])) + " " + str(np.exp(row['yhat_upper'])))

# plt.show()

When I run this python script separately, I get the expected output. But when I run it from java code it gives this error.

Traceback (most recent call last):
  File "google-trends-service/src/main/java/com/emon/forecast.py", line 1, in <module>
    from fbprophet import Prophet
  File "C:\Users\emon\Anaconda3\lib\site-packages\fbprophet\__init__.py", line 8, in <module>
    from fbprophet.forecaster import Prophet
  File "C:\Users\emon\Anaconda3\lib\site-packages\fbprophet\forecaster.py", line 15, in <module>
    import numpy as np
  File "C:\Users\emon\Anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
    from . import _distributor_init
  File "C:\Users\emon\Anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
    from. import _mklinit
ImportError: DLL load failed: The specified module could not be found.

But if I replace my python script with only

print("Hello world")

This gives the expected output in the java console.

I saw the previous posts but these did not solve my problem. Those are,

Issue while calling Python(Anaconda) from Java

Calling Python script from JAVA MySQLdb imports

3
  • My guess is that this is an environment issue. Your Python script is using a module that has a binary component (DLL) associated with it. When you run your script manually, your environment is somehow set up to find that DLL. But the environment under which Java is running the script is different, and the DLL can't be found. Your "hello world" script runs fine because it is not dependent on any external modules. - If you were on Mac or Linux, I could give you some things to look for. But I don't know Windows, and don't know how my ideas would translate. Commented Jun 26, 2019 at 9:29
  • Yes. You were right. I reinstalled anaconda then it is working fine now. Set the path variable in the installation time which is not recommended by anaconda. Commented Jun 26, 2019 at 17:05
  • Great! I'll make this an answer then so that there is closure on this question. - I'm happy that you figured it out! Commented Jun 26, 2019 at 20:22

1 Answer 1

0

My guess is that this is an environment issue. Your Python script is using a module that has a binary component (DLL) associated with it. When you run your script manually, your environment is somehow set up to find that DLL. But the environment under which Java is running the script is different, and the DLL can't be found. Your "hello world" script runs fine because it is not dependent on any external modules.

I would suggest that you try reinstalling Anaconda.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.