2

I am trying to use a Python program with Java. My Python program is as follows:

import CostCalculatorType
import os
from Evaluate.read_input_data import *

class CostCalculator(CostCalculatorType, object):

    def __init__(self):
        print 'Initializing'
        pass

    def calculateCost(self, chromosome):
        inputData = ReadInputData(chromosome, 'input_data.txt')
        return inputData

I am calling this from a Java interface. I am using the following command as specified on JyNI.org:

java -cp /home/ch/jython.jar:/home/ch/JyNI.jar org.python.util.jython CostCalculator.py

java Main

I get the following error:

File "/home/ch/CostCalculator.py", line 18, in calculateCost
    inputData = ReadInputData(chromosome, 'input_data.txt')
File "/usr/lib/python2.6/site-packages/Evaluate/read_input_data.py", line 21, in __init__
    self.info = infoClass(0, [], [], 0.02)
File "/usr/lib/python2.6/site-packages/Evaluate/infoClass.py", line 7, in __init__
    import numpy
ImportError: No module named numpy

Can NumPy be imported with JyNI at all?

1 Answers1

2

This is kind of a duplicate of How to setup numpy in jython. For convenience I'll repeat my answer from there:

JyNI does state NumPy-support as its main goal, but cannot do it yet, as long as it is still in alpha-state. However until it is mature enough you can use NumPy via

Alternatively you can use a Java numerical library for your computation, e.g. one of these:

Both are Java-libs that do numerical processing natively backed by blas or lapack (i.e. the same backends NumPy uses), so the performance should equal that of NumPy more or less. However they don't feature such a nice multiarray implementation as NumPy does afaik.

If you need NumPy indirectly to fulfill dependencies of some other framework, these solutions won't do it out of the box. If the dependencies are only marginal you can maybe rewrite/substitute the corresponding calls based on one of the named projects. Otherwise you'll have to wait for JyNI...

If you can make some framework running on Jython this way, please consider to make your work publicly available, ideally as a fork of the framework.

Community
  • 1
  • 1
stewori
  • 586
  • 4
  • 12
  • Yes. JEP works well. I tried using it. However, it cannot be executed concurrently if integrating with Apache Spark. – Vitrion Apr 06 '19 at 00:27