简体   繁体   English

VirtualEnv Python作为MacOS中的Java Python解释器

[英]VirtualEnv Python as Java PythonInterpreter in MacOS

I'm currently finding a way to get some output from a python script through Java. 我目前正在寻找一种通过Java从python脚本获取一些输出的方法。 After finding jython, I used PythonInterpreter to send parameters and receive output I expect. 找到jython之后,我使用PythonInterpreter发送参数并接收期望的输出。

My Code 我的密码

    PythonInterpreter interpreter = new PythonInterpreter();
    String fileUrlPath = "/Users/me/Desktop/";
    String scriptName = "pythonScript";
    interpreter.exec("import sys\n" + "import os \n" + "sys.path.append('" + fileUrlPath + "')\n"+ "from "+scriptName+" import * \n");
    String funcName = "predictionFunction";
    PyObject someFunc = interpreter.get(funcName);

    try 
    {
        PyObject out = someFunc.__call__(new PyString(pathToImageFolder), new PyString(fileName));
        System.out.println(out);
    } 
    catch (PyException e) 
    {
        e.printStackTrace();
    }

My Problem 我的问题

But whenever I launch my program, It shows, numpy module not found error. 但是,每当我启动程序时,它就会显示numpy module not found错误。 So I checked the python version and it showed 2.7.0 which is version of jython python interpreter. 所以我检查了python版本,它显示了2.7.0 ,这是jython python解释器的版本。

But all my python libraries has been installed in a Virtual environment in path path/to/vir/env/bin . 但是我所有的python库都已安装在路径为path/to/vir/env/bin的虚拟环境中。 So I need to change the interpreter of java PythonInterpreter function to the python I've installed in my virtual environment. 因此,我需要将Java PythonInterpreter函数的解释器更改为我在虚拟环境中安装的python。

is it possible? 可能吗? If so, really appreciate if someone can help. 如果是这样,如果有人可以提供帮助,请多谢。

What you're asking for is impossible. 您要的是不可能的。

Jython can't use C-API extensions, which includes NumPy. Jython无法使用C-API扩展,其中包括NumPy。 Changing the sys.path to include the location where you installed NumPy won't help. sys.path更改为包括NumPy的安装位置将无济于事。

Jython can use Java packages, and there are Java packages for array-based numerics, but none of them is anywhere near a drop-in replacement for NumPy; Jython 可以使用Java程序包,并且有一些Java程序包可以用于基于数组的数字,但是它们都不能替代NumPy。 you'd have to rewrite all of your code to use a pretty different API, and it may not have features you're relying on. 您必须重写所有代码才能使用完全不同的API,并且它可能没有您所依赖的功能。


However, if all you're trying to do is call some Python code with some simple string arguments and get back a string as output, you can do that just by running CPython as a subprocess. 但是,如果您要做的就是用一些简单的字符串参数调用一些Python代码并返回一个字符串作为输出,则可以通过将CPython作为子进程运行来实现。 And it seems like that's exactly what you're doing: you've got some wrapper code that just imports your module, calls a function with two strings, and prints out the return value as a string. 看来这正是您正在做的事情:您已经有了一些包装器代码,这些包装器代码仅导入您的模块,使用两个字符串调用一个函数,然后将返回值打印为字符串。

Step 1: Wrap that Python code up in a script that takes its arguments via sys.argv , and outputs its results via print . 步骤1:将Python代码包装在脚本中,该脚本通过sys.argv接受其参数,并通过print输出其结果。

Step 2: Run your script with the normal CPython interpreter from the virtualenv that has NumPy installed, and capture its output with a pipe. 步骤2:使用安装了NumPy的virtualenv中的普通CPython解释器运行脚本,并使用管道捕获其输出。

Step 3: There is no step 3. (Or was it something something life finds a chaos theory because Earth girls are easy? I don't know, it's all Jeff Goldblum.) 第3步:没有第3步。(或者是因为地球女孩很容易,某种生活发现了混乱的理论吗?我不知道,这就是杰夫·戈德布鲁姆的全部。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM