Question

I am trying to run a script using ProcessBuilder

The problem arises when in the script I am exporting to $PYTHONPATH to allow me to pipe an echo command into a python file shown below:

ProcessBuilder:

ProcessBuilder builder = new ProcessBuilder("sh", "sasa_script.sh");
                builder.redirectErrorStream(true);
                builder.directory(new File("/Users/me/script_location"));               
                Process process = builder.start();

sasa_script.sh :

#!/bin/bash
cd /Users/me/dir_for_tool/
export PYTHONPATH=$PYTHONPATH:/Users/me/dir_for_tool/dir_containing_init.py/
echo $PYTHONPATH
echo -e"This is a sample echo." | python ./bin/classifyFromCmdLine.py

The script runs perfectly in mac terminal but when ran with ProcessBuilder causes the error where it ask you to run setup or set your PYTHONPATH (not an error with processBuilder but with the script)

Traceback (most recent call last): File "./bin/classifyFromCmdLine.py", line 22, in "(or add the sasa-tool directory to PYTHONPATH, ie export PYTHONPATH=)?") ImportError: Did you try to run '. setup.env'? (or add the sasa-tool directory to PYTHONPATH, ie export PYTHONPATH=)?

I am also echoing PYTHONPATH which shows that after that command the right path was set.

So basically I am just wondering what Process builder does differently so that exporting before running the command cannot find the PYTHONPATH and how to fix this issue. Thanks.

Was it helpful?

Solution 2

Try actually building the program as an exe jar and run it from the terminal. Then everything should share the same env variables.

OTHER TIPS

When you log in to a shell, your $PYTHONPATH environment variable is set somehow (probably via .bashrc or a similar profile config). When you run ProcessBuilder, this environment variable is not being set. You could either hardcode your PYTHONPATH into the script, or you could try using source to import the config and set the variable:

source /Users/me/.bashrc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top