Jython如何挂钩到pip包中?

时间:2017-01-16 16:21:09

标签: java python pip shared-libraries jython

这是重复: How can I install various Python libraries in Jython? 因为它有没有Java代码显示如何引用PIP包。也不再使用简单的设置。

虚拟方法运行良好:

compile:

run:
     [java] Jan 16, 2017 8:13:20 AM net.bounceme.dur.nfl.JythonBean dummy
     [java] INFO: see https://bytes.com/topic/python/answers/22390-executing-jython-function-java
     [java] Jan 16, 2017 8:13:36 AM net.bounceme.dur.nfl.JythonBean nflHook
     [java] INFO: see https://github.com/BurntSushi/nfldb/wiki/An-introduction-to-the-query-interface
     [java] Exception in thread "main" Traceback (most recent call last):
     [java]   File "<string>", line 1, in <module>
     [java] ImportError: No module named nfldb
     [java] <module 'sys' (built-in)>
     [java] 42
     [java] x: 4
     [java] <module 'sys' (built-in)>

BUILD FAILED
/home/thufir/NetBeansProjects/Jython/nbproject/build-impl.xml:1040: The following error occurred while executing this line:
/home/thufir/NetBeansProjects/Jython/nbproject/build-impl.xml:805: Java returned: 1

Total time: 23 seconds
thufir@doge:~/NetBeansProjects/Jython$ 

但我不知道如何导入pip包,以便Jython可以使用它:

package net.bounceme.dur.nfl;

import java.util.logging.Logger;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

public class JythonBean {

    private static final Logger log = Logger.getLogger(JythonBean.class.getName());

    public JythonBean() {
        log.fine("java bean for now..");
    }

    /*
        import nfldb
    db = nfldb.connect()

    q = nfldb.Query(db)
    q.game(season_year=2013, season_type='Preseason', team='NE')
    for g in q.as_games():
        print g
     */
    public void nflHook() {
        log.info("see https://github.com/BurntSushi/nfldb/wiki/An-introduction-to-the-query-interface");
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("import sys");
        interpreter.exec("print sys");
        interpreter.exec("import nfldb");
    }

    public void dummy() {
        log.info("see https://bytes.com/topic/python/answers/22390-executing-jython-function-java");
        PythonInterpreter interpreter = new PythonInterpreter();

        // The exec() method executes strings of code
        interpreter.exec("import sys");
        interpreter.exec("print sys");

        // Set variable values within the PythonInterpreter instance
        interpreter.set("a", new PyInteger(42));
        interpreter.exec("print a");
        interpreter.exec("x = 2+2");

        // Obtain the value of an object from the PythonInterpreter and store it
        // into a PyObject.
        PyObject x = interpreter.get("x");
        System.out.println("x: " + x);

    }

}

因为pip软件包已安装到系统中:

thufir@doge:~$ 
thufir@doge:~$ pip show nflgame
Name: nflgame
Version: 1.2.20
Summary: An API to retrieve and read NFL Game Center JSON data. It can work with real-time data, which can be used for fantasy football.
Home-page: https://github.com/BurntSushi/nflgame
Author: Andrew Gallant
Author-email: andrew@burntsushi.net
License: UNLICENSE
Location: /home/thufir/.local/lib/python2.7/site-packages
Requires: beautifulsoup4, pytz, httplib2
thufir@doge:~$ 

据推测,有一种加载或与nfldb库或包交互的机制吗?

0 个答案:

没有答案