Is it possible to switch conda environments while Streamlit is running?

Hi there,

Iā€™m a new user to Streamlit and have been playing around from past couple of weeks. Iā€™m trying to create a application for Word embedding. Part of my models use libraries/code base that is dependent on Tensorflow 1.X and part of them use Tensorflow 2.X and also multiple pytorch dependencies. I have two separate anaconda environments for either of them to avoid dependency issues.

I want to have just one application file which will show/run all models and results irrespective of the environments (or hiding all environment details from user). Is there a simple/dynamic way to switch conda environments while the Streamlit app is running.

Was hoping advanced users might have some good insights on this ?

Thanks!
Mohammed Ayub

Hi @mohammed_ayub, welcome to the Streamlit community!

In general, no, you canā€™t switch conda environments from the same Python session (this is a Python limitation, not a Streamlit one). Once you load a module one time, trying to reload it again wonā€™t do anything:

So once Tensorflow 1.x is loaded, thatā€™s what you have. Additionally, with conda, the Python interpreter is part of the environmentā€¦trying to ā€œswitchā€ the interpreter is essentially stopping the code and starting a different executable, which means Streamlit would no longer be running.

In industry, when people want to serve the results of a model, they wrap it up in their own process as an API, then call the API. You could do this in your caseā€¦depending on a drop-down or radio button in streamlit, you could call the different API endpoints for your model results.

Thanks @randyzwitch . That makes sense and right now looks like the best option.
I might convert it to SavedModel format and serve it using Tensorflow Serving API.

Cheers !

Hi @mohammed_ayub,
My article shows how to to tell Streamlit to run a sub processes in different conda environmentsā€¦ Cheers!