Question

In essence I have this program:

from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
print solve(x**2 - 1, x)

And I call this from Java using this code:

public static BufferedReader runFile(Class<?> c, String py, List<String> args) {
    String cmd = c.getResource(py).getPath();
    cmd=cmd.split(py)[0];
    cmd=cmd.substring(0,cmd.length()-1);
    args.add(0, py);
    args.add(0, "python");
    final ProcessBuilder pb = new ProcessBuilder(args);
    new ProcessBuilder();
    pb.directory(new File(cmd));
    pb.redirectError();
    try {
        System.out.println(pb.directory().getPath());
        for(String s:pb.command()){
            System.out.println(s);
        }
        Process p=pb.start();
        return new BufferedReader(new InputStreamReader(p.getErrorStream()));
    }
    catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

When I run the Python program from a terminal everything works as intended, with nothing in the error stream, and it prints [-1,1]. But if I run it from the program, I get this in the error stream:

Traceback (most recent call last):
  File "solve.py", line 1, in <module>
    from sympy.solvers import solve
ImportError: No module named sympy.solvers
Was it helpful?

Solution

Since specifying the full path of Python fixes your problem, you most likely have multiple installations of Python on your system. Rather than PYTHONPATH being different, I suspect it is actually PATH that is different. As a result, your command line uses the Python interpreter you intend, while Java uses another one.

To determine where this alternate install is, which -a python may be useful, but if not, examine PATH from inside your Java code and see if you can find Python in one of those directories.

Regardless, if you really need to specify the full Python path in Java, you should make this a configuration option. It will probably be different on different machines. Storing it in a file seems most prudent.

OTHER TIPS

Your PYTHONPATH (or less likely your working directory) is different when running from your Java context.

You can

 import sys
 print sys.path

which may help you to ensure the path is the same for both.

Telling us more about how your environment is set up will help to get more specific answers.

eg. Maybe the Java is running via a web server or something?


Here are couple of ways to fix the path problem:

Make sure the directory containing sympy is in your PYTHONPATH environment variable

If you're really desperate, append the correct directory to sys.path

import sys
sys.append("/some/dir/with/sympy")
from sympy.solvers import solve
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top