0

This is not a duplicate of: How can I install various Python libraries in Jython? because that has no Java code showing how to reference PIP packages. Nor is easy setup used anymore.

The dummy method runs fine:

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$ 

but I don't know how to import a pip package so that Jython can use it:

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);

    }

}

Because the pip package is installed to the system:

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:~$ 

Presumably there's a mechanism to load or interact with the nfldb library or package?

Community
  • 1
  • 1
Thufir
  • 8,216
  • 28
  • 125
  • 273

0 Answers0