question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hot-reloading not working when a module is imported with `from foo import bar`

See original GitHub issue

Summary

@Icerman observes hat hot reloading works as expected when modules are imported like:

import foo.bar

or

import foo.bar as bar

but when importing the same module using:

from foo import bar

then its changes are ignored.

Steps to reproduce

Steps to reproduce are described below.

Debug info

@MarcSkovMadsen gave his system information below.

Additional information

This is the first of two bugs based on this awesome-streamlit issue. The second bug is this.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
lcermancommented, Oct 10, 2019

Hello I have run in the same issue recently. It seems that auto-reload works as expected when modules are imported like:

import foo.bar

or

import foo.bar as bar

but when I import the same module using:

from foo import bar

then its changes are ignored.


Steps to reproduce:

  1. create new venv with only streamlit installed (version 0.47.4).
  2. create following files:

main_ok.py:

import streamlit as st
import foo.bar

st.markdown('Main file')

foo.bar.fn()

main_ok2.py:

import streamlit as st
import foo.bar as bar

st.markdown('Main file')

bar.fn()

main_ko.py:

import streamlit as st
from foo import bar

st.markdown('Main file')

bar.fn()

foo/bar.py:

import streamlit as st

def fn():
    st.markdown('Function in submodule.')
  1. changes in foo/bar.py are reflected when run as streamlit run main_ok.py or streamlit run main_ok.py2 but ignored when run the app as streamlit run main_ko.py

My system is Ubuntu 18.04 running inside WSL at Windows 10, Python 3.6.8.

2reactions
tconklingcommented, Oct 23, 2019

Notes, as I’m investigating:

  • We properly register a filewatcher for from foo import bar modules
  • The first time through LocalSourcesWatcher.update_watched_modules, sys.modules includes these modules as expected
  • But upon rerun, sys.modules seems to… lose them?
  • It turns out we’re doing sys.modules pruning from LocalSourcesWatcher.on_file_changed (removing any watched file that changes from sys.modules), but I’m not sure why. – Per @tvst, this is to force python to reload that module.
  • If we remove this logic from on_file_changed, changing the module causes a re-run as expected. But: it’s as if the module itself is not being reloaded; no changes are reflected.
  • Do we need to reload() watched modules before re-running?

https://stackoverflow.com/questions/7271082/how-to-reload-a-modules-function-in-python

  • Based on the above, I think what needs to happen is: – When we detect a file change: – Recursively… reload that file and every file that depends on it?? – What we really want to do is just throw out everything and reload it all.
  • We’re modifying sys.modules all over the place. Do we need to be more careful with this, across multiple threads? (Or maybe we don’t support hot-reloading if > 1 user is connected?)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Python: reload component Y imported with 'from X import Y'?
Answer. From my tests, the marked answer, which suggests a simple reload(X) , does not work. From what I can tell the correct...
Read more >
Programming FAQ — Python 3.11.1 documentation
Running Python on a top level script is not considered an import and no .pyc will be created. For example, if you have...
Read more >
Modules - Beginner JavaScript - Wes Bos
You can import it from that file. Importing and Exporting Modules. There are two types of module imports: Named Imports; Default Imports.
Read more >
react-hot-loader - npm
Start using react-hot-loader in your project by running `npm i ... Use hot only for module exports , not for module imports ....
Read more >
Features | Vite
Native ES imports do not support bare module imports like the following: ... warn you against the features that do not work with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found