Quantcast
Channel: Active questions tagged numpy - Stack Overflow
Viewing all articles
Browse latest Browse all 5827

How to run python script with its own dependencies in Java independently from JDK environment

$
0
0

I want to run script inside my Java code. More specifically, I want to implement GUI in Java and run python script to get make heavy lifting work that I want to do only with python and get result in java and show it to the user in GUI.

I tried some examples on the internet by creating its own environment for python and connecting it to the python script but it seems like there is problem with architecture.

The error from numpy library:

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy/core/_multiarray_umath.cpython-311-darwin.so, 0x0002): tried: '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy/core/_multiarray_umath.cpython-311-darwin.so' (mach-o file, but is an incompatible architecture (have (arm64), need (x86_64)))ImportError: Error importing numpy: you should not try to import numpy from        its source directory; please exit the numpy source tree, and relaunch        your python interpreter from there.

This is my Java code:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Main {    public static void main(String[] args) {        try {            setupPythonEnvironment();            ProcessBuilder builder = new ProcessBuilder("python3", "/Users/ilhomsoliev/Desktop/WebApplications/PythonInJavaRun/src/python_scripts/main.py");            Process process = builder.start();            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));            BufferedReader readerError = new BufferedReader(new InputStreamReader(process.getErrorStream()));            String lines = null;            while ((lines = reader.readLine()) != null) {                System.out.println(lines);            }            while ((lines = readerError.readLine()) != null) {                System.out.println(lines);            }        } catch (Exception e) {            System.out.println(e.getMessage());        }    }    public static void setupPythonEnvironment(){        try {            // Path to your Python script's directory            String scriptDir = "/Users/ilhomsoliev/Desktop/WebApplications/PythonInJavaRun/src/python_scripts";            String requirementsPath = scriptDir +"/requirements.txt";            // Create a virtual environment            String venvDir = scriptDir +"/venv";            executeCommand("python3 -m venv " + venvDir);            // Install dependencies from requirements.txt            String pipInstallCmd = venvDir +"/bin/pip3 install -r " + requirementsPath; // for Unix-based systems            executeCommand(pipInstallCmd);            System.out.println("Python environment set up successfully.");        } catch (Exception e) {            e.printStackTrace();        }    }    private static void executeCommand(String command) throws IOException, InterruptedException {        ProcessBuilder pb = new ProcessBuilder(command.split(" "));        pb.inheritIO(); // To show the output/error in Java console        Process p = pb.start();        p.waitFor(); // Wait for the process to finish    }}

This is requirments.txt:

pipsite-packagesnumpymoviepypacman

Viewing all articles
Browse latest Browse all 5827


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>