在Spring引导应用程序中包含Python脚本,但未找到Jython模块

2024-05-26 11:08:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试习惯python+java交互,因此我编写了一个小python脚本,希望从我的Spring Boot Application中执行该脚本。该脚本位于(相对于.java-file)路径/scripts_py/getStockPrice.py中,该路径包含getStockPrice-method(请参见下面的代码)。因此,我集成了jython,并尝试执行以下CronJob

@Component
public class CronService {

    private PythonScriptSingleton pss = PythonScriptSingleton.getInstance();
    private final Logger logger = LoggerFactory.getLogger(CronService.class);

    //call every 5 sec
    @Scheduled(fixedRate = 5000)
    public void initStockPriceAPICall() {
        this.getStockPrice("NFLX");
    }

    public void getStockPrice(String ticker) {
        String result = (String) (Object) this.pss.getFunc_getPriceForTicker().__call__(new PyString(ticker));
        try {
            logger.info("Price is " + result);
        } catch (NullPointerException e) {
            logger.info("Catched NPE");
        }

    }
}

PythongScriptSingleton(想法是,我可能需要多次执行该脚本-因此我尝试使用这个类的singleton实例来保存该脚本,因此我不需要每次都重新编译它:

public class PythonScriptSingleton {

    private static PythonScriptSingleton ps;

    public static PythonScriptSingleton getInstance() {
        if (ps == null) {
            ps = new PythonScriptSingleton();
            ps.initScript();
        }
        return ps;
    }

    private PyObject func_getPriceForTicker;

    private PythonScriptSingleton() {

    }

    private void initScript() {
        PythonInterpreter interpreter = new PythonInterpreter();
        String fileUrlPath = "/scripts_py";
        String scriptName = "getStockPrice.py";
        interpreter.exec("import sys\n" + "import os \n" + "sys.path.append('" + fileUrlPath + "')\n" + "from "
                + scriptName + " import * \n");
        String funcName = "getStockPrice";
        PyObject someFunc = interpreter.get(funcName);
        this.func_getPriceForTicker = someFunc;
        interpreter.close();
    }

    public PyObject getFunc_getPriceForTicker() {
        return func_getPriceForTicker;
    }
}

Python脚本:

from yahoo_fin import stock_info as si

def getStockPrice(ticker):
    price = si.get_live_price(ticker)
    return price

我正在将嵌入的tomcatSpring Boot(可执行文件JAR-File)和jython-standalone 2.7.2一起使用:

   <dependency>
        <groupId>org.python</groupId>
        <artifactId>jython-standalone</artifactId>
        <version>2.7.2</version>
    </dependency>

我一直遇到的错误是,脚本未定义-我尝试在文件结尾(.py)和未结尾(.py)的情况下定义它,但这两个错误都没有改变任何内容:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [de.fr.stockticker.pythoninteraction.CronService]: Constructor threw exception; nested exception is Traceback (most recent call last):
  File "<string>", line 4, in <module>
ImportError: No module named getStockPrice

        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:217) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1312) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        ... 81 common frames omitted
Caused by: org.python.core.PyException: ImportError: No module named getStockPrice
        at org.python.core.Py.ImportError(Py.java:329) ~[jython-standalone-2.7.2.jar:2.7.2]
        at org.python.core.imp.import_first(imp.java:1230) ~[jython-standalone-2.7.2.jar:2.7.2]
        at org.python.core.imp.import_module_level(imp.java:1361) ~[jython-standalone-2.7.2.jar:2.7.2]
        at org.python.core.imp.importName(imp.java:1528) ~[jython-standalone-2.7.2.jar:2.7.2]
        at org.python.core.ImportFunction.__call__(__builtin__.java:1285) ~[jython-standalone-2.7.2.jar:2.7.2]
        at org.python.core.PyObject.__call__(PyObject.java:433) ~[jython-standalone-2.7.2.jar:2.7.2]
        at org.python.core.__builtin__.__import__(__builtin__.java:1232) ~[jython-standalone-2.7.2.jar:2.7.2]
        at org.python.core.imp.importAll(imp.java:1647) ~[jython-standalone-2.7.2.jar:2.7.2]
        at org.python.pycode._pyx0.f$0(<string>:4) ~[na:na]
        at org.python.pycode._pyx0.call_function(<string>) ~[na:na]
        at org.python.core.PyTableCode.call(PyTableCode.java:173) ~[jython-standalone-2.7.2.jar:2.7.2]
        at org.python.core.PyCode.call(PyCode.java:18) ~[jython-standalone-2.7.2.jar:2.7.2]
        at org.python.core.Py.runCode(Py.java:1687) ~[jython-standalone-2.7.2.jar:2.7.2]
        at org.python.core.Py.exec(Py.java:1731) ~[jython-standalone-2.7.2.jar:2.7.2]
        at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:268) ~[jython-standalone-2.7.2.jar:2.7.2]
        at de.fr.stockticker.pythoninteraction.PythonScriptSingleton.initScript(PythonScriptSingleton.java:28) ~[classes/:na]
        at de.fr.stockticker.pythoninteraction.PythonScriptSingleton.getInstance(PythonScriptSingleton.java:13) ~[classes/:na]
        at de.fr.stockticker.pythoninteraction.CronService.<init>(CronService.java:12) ~[classes/:na]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_251]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_251]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_251]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_251]
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:204) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        ... 83 common frames omitted

Tags: orgcore脚本releasejythonjavacallat
1条回答
网友
1楼 · 发布于 2024-05-26 11:08:03

我可以部分使用Jython运行您的程序-我无法从雅虎获得股票流程,因为雅虎内部依赖于numpy,而且Jython不支持numpy,因为它是cpython库

private void initScript() { 
    PythonInterpreter interpreter = new PythonInterpreter(); 
    String fileUrlPath = "/users/sagar/demo/src/main/resources/python"; 
    interpreter.exec("import sys"); 
    interpreter.exec("sys.path.append('" + fileUrlPath + "')"); 
    interpreter.exec("from getStockPrice import *"); this.func_getPriceForTicker = 
    interpreter.get("getStockPrice", PyFunction.class); 
    interpreter.close(); 
}

您将能够克服错误,但您将看到错误

Missing required dependencies ['numpy'] 

因此,我尝试使用另一个java python库jep,并做了如下更改

@SpringBootApplication
@EnableScheduling
public class DemoApplication  {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    private final Logger logger = LoggerFactory.getLogger(DemoApplication.class);

    //call every 5 sec
    @Scheduled(fixedRate = 5000)
    public void initStockPriceAPICall() throws JepException {
        this.getStockPrice("NFLX");
    }

    private void getStockPrice(String ticker) throws JepException {
        try (Interpreter interp = new SharedInterpreter()) {
            String fileUrlPath = "/users/sagar/demo/src/main/resources/python";
            interp.exec("import sys");
            interp.exec("sys.path.append('" + fileUrlPath + "')");
            interp.exec("from getStockPrice import *");
            interp.set("ticker", ticker);
            interp.exec("price = getStockPrice(ticker)");
            Object result = interp.getValue("price");
            logger.info("Price is " + result);
        }
    }
}

pom.xml

<dependency>
    <groupId>black.ninia</groupId>
    <artifactId>jep</artifactId>
    <version>3.9.0</version>
</dependency>

确保安装jep模块-它具有本机库

pip install jeb

添加java库路径以加载本机库

-Djava.library.path=/usr/local/lib/python2.7/site-packages/jep

引用-https://github.com/ninia/jep/wiki/Getting-Started

enter image description here

相关问题 更多 >