Streamlit automatically reload imported modules


python version: 3.11.5
streamlit version: 1.31.1

I run local streamlit app and have some folder structure like below (simplified):


In file I do:

import utils

and in file I define some functions and do something like:

print("I am here!")

My questions is, whenever I change file and save, streamlit tells me source file changed, and I click rerun button, but it seems it reloads file as I can see it prints “I am here!” again. I understand when I click rerun button Streamlit will rerun file but isn’t it not supposed to reload as it is already loaded? Or does it clear everything and rerun everything?

What is most weird to me is, I have an old app which is working as I expected (update, click rerun button, but does not reload as I do not see printed message) even though I upgrade python and Streamlit to the same version as above. The old app and the new app above are in the same machine but in different project folders and have different virtual environments.

Could anyone please help me on this? Thank you!

1 Like

Hi @xzhu43,

Thanks for sharing this question!

So Streamlit reruns the entire page upon any interaction on the page or reload. This means that all the modules including gets re-imported that’s why you’re seeing the I am here! printed on the console each time you reload the page especially if the print statement in the utils file is at the top level. To prevent the printing, you can consider wrapping the print statement in a function and only call it when necessary or use the if __name__ == "__main__": to ensure it only runs when the is executed as the main program not when it is imported as a module.

1 Like

I’ve weirdly found the opposite to be true @tonykip.

Running streamlit 1.32.2 in python 3.10.13

I have 2 files, and a
I ran streamlit run

import streamlit as st
from datetime import datetime

print(f'Before Test    {}')
import trial_test as testy
print(f'After Test     {}')

from datetime import datetime
print(f"Test     {}")
count = 0

terminal output:
1 initial run and 2 browser refreshes.

Before Test    2024-03-23 05:05:29.272836
Test           2024-03-23 05:05:29.275250
After Test     2024-03-23 05:05:29.275362
Before Test    2024-03-23 05:05:29.789673
After Test     2024-03-23 05:05:29.789854
Before Test    2024-03-23 05:05:34.898263
After Test     2024-03-23 05:05:34.898341
1 Like

Update: I tested by updating count = 1 and now a browser refresh executes once more.
From this I can conclude that a re-import only happens when a change is recognised in said module. How can I force execute it everytime?

1 Like

You can’t. The proper mechanism to force execution of code in another module is a function call, not importing.



Thanks for sharing your test! This is what I expected to see as I only want to update the UI defined in to make it looks better without reloading which does some calculation. And I managed to do this in my old app, but it failed to do this in the new app. So I have always believed that there is some simple trick or some streamlit config to switch between 2 behavior but I just do not know.

1 Like

So weird why we have the exactly opposite situation. My case in my new app is the change is in file but it reloads again.

1 Like

Thanks for you suggestion. The function in needs to run to make the app work. My issue is not I do not want it to run. It is actually working well when the app launches in production. The issue is when I am doing the development on the UI, I do not want to run again and again as it takes time to wait for it to finish.

1 Like

Then call the function once and store the result somewhere, then use the stored result.

Hi @ruthran-c, did you find a solution to your issue? I’m running into the same thing where an imported library doesn’t reload if I make changes in that file. Only the main file triggers a reload.

I’m 100% sure this is not how it was working previously. My modules would always trigger a reload.

My imported custom module always reloads, 'if ’ a code change is introduced to said module’s .py file. I use Streamlit, version 1.32.2.

Note: I desired a module reload with each app refresh even if the module’s code didn’t change, but I couldn’t achieve it. So, I modified my source code to align with Streamlit’s current operation.
Hope this was helpful!

Aah ok. I misunderstood that.

I’ve just posted in another thread what I found about my own problem though. The module you want to watch for changes can’t be in a parent or sibling directory from what I can see. It only works if it is at or below the main app file directory.

Hopefully this saves someone the 2 hours I just spent on this problem…