Python Forum

Full Version: How to avoid reloading modules in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am getting the warning:
Error:
"reloaded modules: chromosome_length"
Chromosome_length is actually a function that I am calling in another *.py file via the following:

from chromosome_length import chromosome_length
So, three things:

1) Why does a reloaded module happen?

2) What are the problems/issues it can cause?

3) How to avoid this happening?

P.S. I know how to deactivate the warning message, but I also want to understand why this is happening and how I should avoid it.
Unless you're doing something wacky with the importlib module, you can't reload modules.
What's the whole warning/error message? Is there a traceback?
(May-04-2018, 08:26 PM)nilamo Wrote: [ -> ]Unless you're doing something wacky with the importlib module, you can't reload modules.
What's the whole warning/error message? Is there a traceback?

It's actually the warning that I mentioned only:

Error:
"reloaded modules: chromosome_length"
I can deactivate it using the following:

import warnings
warnings.filterwarnings("ignore", message="Reloaded modules: <chromosome_length>")
I just wonder why I am getting this warning and how it can cause issues in my program and how to avoid this (instead of filtering the warning indeed)
I can't imagine it'd cause any issues in your code, unless you do it a lot (a LOT), and then it might slow your program down a bit.
I also can't imagine how that's happening without you purposefully reloading the module, though, as they're cached. So just importing it multiple times wouldn't cause a reload.
(May-04-2018, 09:03 PM)nilamo Wrote: [ -> ]I can't imagine it'd cause any issues in your code, unless you do it a lot (a LOT), and then it might slow your program down a bit.
I also can't imagine how that's happening without you purposefully reloading the module, though, as they're cached. So just importing it multiple times wouldn't cause a reload.

Thanks! So I just found this option in spyder which says:

"User Module Reloader (UMR): UMR forces Python to reload modules which were imported when executing a file in a Python or IPython console with the runfile function."

And I deactivated it and now it works perfectly! Now my question is, why would somebody need this option?! Like why would I need to reload a module if it is already imported when I was executing the file?
https://github.com/spyder-ide/spyder/blo...loader-umr Wrote:This behavior is sometimes unexpected when working with the Python interpreter in interactive mode, because one must either always restart the interpreter or remove manually the .pyc files to be sure that changes made in imported modules were taken into account.

So one reason, is so that you can use whatever file you're editing, within the interactive session. Without the reload, your changes wouldn't be available in the interactive session, as the original file was still cached.

Practically, I don't think it matters, as you're not going to be using it interactively once you're done, anyway.