Access environment variables from the local host (not cloud)

Summary

We run Streamlit on an internal host under Docker (actually podman).
We need to access environment variables not in a file from inside the app.

Question

How can we make the environment from the Docker container available to Streamlit? We would prefer not having to store the data in a config file and import them but simply to rely on the environment we run the app in.

When developing, we typically put data in .env and we can import it using the python-dotenv call load_dotenv(). But for production, we need to be able to get the same information from the environment directly.

Today, Streamlit seems only to show its own environment without inclusions from the initializing enviroment.

To reproduce:

print(os.environ) inside the streamlit app will not show any variables form the host.

Debug info

  • Streamlit version: 1.27.1
  • Python version: 3.10.12
  • OS version: Ubuntu 20.04

Hi @MatsB

Perhaps you can use os.environ to retrieve environment variables.

Also check out this blog for additional information:

Hope this helps!

Thank you!
I went back to the problem thanks to your message and realised that even though I do source the .env environment file and even though I can do e.g. echo $API_KEY, these variables were not exported.
So by simply making sure I call β€œexport” for my variables, they do appear in the streamlit context.
Great! Thanks for making me take a second look.
Best, /Mats

1 Like

Glad to hear that your second look helps to resolve the issue.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.