Invoking Python subprocess in Streamlit to run script file


I’m working on a Streamlit script to run a script file (.sh) through the command line in a Linux environment. I examined this thread in the documentation for guidance, but no luck so far. The script runs without an error, but the script file itself doesn’t run. I’ve tried to see if the issue is a path error, but the path info looks good.

Here’s the sample code:

import streamlit as st
import subprocess
import sys

st.title("Test Script")
button1 = st.button("Click me")
if button1:[f"{sys.executable}", "/home/user/grader_2/Scripts/"])

The .sh file looks like this:

cd /home/user/grader_2/essays/TXTs

pylanguagetool text_0.txt  > Comments_0.txt

Any advice or suggestions for invoking a script in command line through Streamlit? Many thanks for any ideas.

Hey @Daniel_Hutchinson -

When you call {sys.executable}, this is calling a Python interpreter. But you’re passing a shell script to it. Have you tried bash or sh instead of {sys.executable}?


Many thanks for the assistance! I changed up from {sys.executable} to a bash as you recommended:

results =['bash', f"{'/home/user/grader_2/Scripts/'}"]) st.write(results)

Good news, the script executes. However, there’s still a problem I can’t account for. 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.

This reply from Stack Overflow believed the problem is a problem of recognizing the shell properly. Any thoughts? Thanks again!