问题
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:
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
- JEP (https://github.com/mrj0/jep) or
- JPY (https://github.com/bcdev/jpy).
Alternatively you can use a Java numerical library for your computation, e.g. one of these:
- https://github.com/mikiobraun/jblas
- https://github.com/fommil/matrix-toolkits-java
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.
来源:https://stackoverflow.com/questions/30654683/using-numpy-with-jyni