1

I'm trying to execute a command in my terminal. The problem is, when I execute the command in terminal, it succeed, but when I run the command from java, the command is executed but, I got an error message showing me that some python module is missing.

try{

        String[] list = { "python3", "script.py" };
        ProcessBuilder pb = new ProcessBuilder(list);
        pb.directory(
                new File("/home/script"));
        System.out.println("" + pb.directory());
        Process process = pb.start();
        InputStream str = process.getErrorStream();
        InputStreamReader isr = new InputStreamReader(str);
        BufferedReader br = new BufferedReader(isr);
        String line;
        System.out.printf("Output of running %s is:", Arrays.toString(args));
        while ((line = br.readLine()) != null) {
            System.out.println(line);}
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String ret = in.readLine();
        System.out.println("value is : "+ret);
        process.waitFor();
        process.destroy();

    }catch (Exception ex) {
        ex.printStackTrace();
    }

The error message:

/home/script
Output of running [] is:Traceback (most recent call last):
  File "scraper.py", line 8, in <module>
    from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'
value is : null

PS: When I execute the command directly from terminal, everything works good, I don't get the missing module error.

0

1 Answer 1

3

Similar to Java, python allows to import other stuff. That message tells you that your python script wants to use the module selenium, but can't find it.

Most likely you have some special ENV var setup when running commands manually in a shell/console. So check your .bashrc or .initrc or whatever defines your ENV variables. On a unix system, typing the command env might show you all settings, too. Simply check if the env var PYTHONPATH is setup.

As that call works from the command line, then for sure, the module is installed on your system. Your only problem is that python can't find it when you invoke that script through the Java ProcessBuilder!

One solution might be that you "manually" adjust the PYTHONPATH from within your script. Thus: figure the correct setup for PYTHONPATH, then update your script to "do the right thing".

For further details, see the python documentation!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help .. I changed the python path and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.