PIP Installing from Github

Hi @sophiegeoghan and @C-Roensholt :wave:

I had no luck via the requirements.txt route either :disappointed: What worked was installing the package in the main app script itself via a subprocess.

Say your script is streamlit_app.py. Include the following at the very top of your app:

import streamlit as st
import subprocess
import sys
import time

try:
  # replace "yourpackage" with the package you want to import
  import yourpackage

# This block executes only on the first run when your package isn't installed
except ModuleNotFoundError as e:
  subprocess.Popen([f'{sys.executable} -m pip install git+https://${{token}}@github.com/yourusername/yourrepo.git'], shell=True)
  # wait for subprocess to install package before running your actual code below
  time.sleep(90)
  
# Rest of your code goes here
import yourpackage
st.write(yourpackage.somefunc())

In the secrets management console paste in your token like so:

token="xxxx"

where xxxx is your GitHub personal access token.

5 Likes