0

This the java code i am using

    public void get_Inv_hash_ids(String token ,String url ,String url1) {
        
        try {
            String pythonPath = "/Users/milind.yadav/anaconda3/bin/python"; // Replace with the actual directory path

    
            Properties properties = new Properties();
            properties.setProperty("python.path", "/Users/milind.yadav/anaconda3/bin/python"); // Replace with the actual directory path

            // Initialize PythonInterpreter with the specified properties
//          PythonInterpreter.initialize(System.getProperties(), properties, new String[] { pythonPath });          
             
        PythonInterpreter interpreter = new PythonInterpreter();

        // Execute the Python script
        interpreter.execfile("/Users/milind.yadav/Downloads/batch_id_script.py");
        
        // Get a reference to the Python function
        interpreter.eval("get invoice ids from py script");
        PyObject invoice_ids = interpreter.get("batch_id_fetcher");
        
        // Pass parameters to the Python function and call it
        PyObject result = invoice_ids.__call__(new PyString(token), new PyString(url),new PyString(url1));        
        // Convert the result to a Java integer
        String concatenated_invoice_ids = (String) result.__tojava__(String.class);
        
        // Print the sum
        System.out.println("Sum: " + concatenated_invoice_ids);
        
        // Clean up the interpreter
        interpreter.close();
       
        }
        catch (Exception e) {
            System.out.println("Exception occured ..."+e.getMessage());
            e.printStackTrace();
            
        }
        
        
    }

And Here is the python script i am using

import requests
    import json
    from jsonpath_ng import jsonpath, parse
    def batch_id_fetcher(token,batchurl,url):
        invoice_object_ids=[]
        header = {"Authorization" : "Token "+token}
        batch_response=requests.get(batchurl, headers=header)
        batch_data=json.loads(batch_response.text)
        invoice_object_ids= batch_data["results"][0]["exported_invoice_ids"]
        inv_url = url+"invoices/?object_id="
        header = {"Authorization" : "Token "+token}
        invoice_hash_list=[]
        invoice_hashes=""
        
        for i in invoice_object_ids:
            n_url=inv_url+str(i)
            response = requests.get(n_url, headers=header)
            json_data = json.loads(response.text)
            invoice_hash_list.append(json_data["results"][0]["id"])
        invoice_hashes = ",".join(str(i) for i in invoice_hash_list)
        return invoice_hashes

This is the error I am getting

Exception occured ...ImportError: No module named requests

I have included all the required libraries in java as well as added all the maven dependencies Am I missing something thing in this

5
  • You need to install the requests module into the same environment as your python binary. Commented Jul 4, 2023 at 7:19
  • yes i checked that its already there Commented Jul 4, 2023 at 8:20
  • @ewokx there is no Python binary in the jython implementation Commented Jul 4, 2023 at 11:46
  • @MilindYadav "yes i checked that its already there" there where exactly? Commented Jul 4, 2023 at 11:46
  • Does this answer your question? Importing python modules in jython Commented Jul 4, 2023 at 11:47

0

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.