2

I'm attempting to run a Linux script through Python's subprocess module. Below is the subprocess command:

result = subprocess.run(['/dir/scripts/0_texts.sh'], shell=True)
print(result)

Here is the 0_texts.sh script file:

cd /dir/TXTs

pylanguagetool text_0.txt > comments_0.txt

The subprocess command executes the script file, writing a new comments_0.txt in the correct directory. However, there's an error in the execution. The comments_0.txt contains an error of "input file is required", and the subprocess result returns returncode=2. When I run the pylanguagetool text_0.txt > comments_0.txt directly in the terminal the command executes properly, with the comments_0.txt written with the proper input file of text_0.txt.

Any suggestions on what I'm missing?

2
  • 1
    it sounds like if you ran /dir/scripts/0_texts.sh in the terminal it would also fail? are you sure the paths (eg cd /dir/TXTs) are correct? Commented Mar 10, 2022 at 18:03
  • I ran the 0_texts.sh in terminal, and it runs correctly, writing the new file in the correct directory with the right input file. Weird. Commented Mar 10, 2022 at 18:06

1 Answer 1

1

There is some ambiguity here in that it's not obvious which shell is run each time 0_texts.sh is invoked, and whether it has the values you expect of environment variables like PATH, which could result in a different copy of pylanguagetool running from when you call it at the command line.

First I'd suggest removing the shell=True option in subprocess.run, which is only involving another, potentially different shell here. Next I would change subprocess.run(['/dir/scripts/0_texts.sh']) to subprocess.run(['bash', '/dir/scripts/0_texts.sh']) (or whichever shell you wanted to run, probably bash or dash) to remove that source of ambiguity. Finally, you can try using type pylanguagetool in the script, invoking pylanguagetool with its full path, or calling bash /dir/scripts/0_texts.sh from your terminal to debug the situation further.

A bigger-picture issue is, pyLanguageTool is a Python library, so you're almost certainly going to be better off calling its functions from your original Python script directly instead of using a shell script as an intermediary.

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

Comments

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.