Invoking a Python subprocess in a deployed Streamlit app

Let's suppose you want to invoke a subprocess to run a Python script script.py in your deployed Streamlit app streamlit_app.py. For example, the machine learning library Ludwig is run using a command-line interface, or maybe you want to run a bash script or similar type of process from Python.

You have tried the following, but run into dependency issues for script.py, even though you have specified your Python dependencies in a requirements file:

# streamlit_app.py import streamlit as st import subprocess subprocess.run(["python", "script.py"])

When you run the above code block, you will get the version of Python that is on the system path—not necessarily the Python executable installed in the virtual environment that the Streamlit code is running under.

The solution is to detect the Python executable directly with sys.executable:

# streamlit_app.py import streamlit as st import subprocess import sys subprocess.run([f"{sys.executable}", "script.py"])

This ensures that script.py is running under the same Python executable as your Streamlit code—where your Python dependencies are installed.

forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.

Was this page helpful?

editEdit this page on GitHub