1

Suppose in main.py I have

from tools import myconstant

and in tools.py I have:

myconstant = 15

Now I add something to tools.py:

myconstant = 15
myotherconstant = 25

Save the file and in main.py I run (still from the same python session):

from tools import myotherconstant

which throws an error. If I restart my python session it works. Why is that? I know that usually, one would run the main file from a fresh session and never have this 'problem' but I am still curious as to why python cannot detect changes in files. If I change the contents of a CSV file and read it in again it would reflect the updated data. What is the difference here?

There is no problem to solve here, I am just asking for clarification of python concepts :) Some minimal examples that demonstrate the inner workings would be appreciated.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
safex
  • 2,398
  • 17
  • 40
  • Does this answer your question? [How do I unload (reload) a Python module?](https://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module) – CrazyChucky Dec 27 '21 at 12:29
  • Python caches imports so subsequent expensive imports don't need to be entirely reloaded again. – Axe319 Dec 27 '21 at 12:32
  • 2
    Does this answer your question? [does python cache imported files?](https://stackoverflow.com/questions/57346840/does-python-cache-imported-files) – Axe319 Dec 27 '21 at 12:32
  • Another related question: [Can Python recognize changes to a file that it is running interactively?](https://stackoverflow.com/a/53550752/2745495) – Gino Mempin Dec 27 '21 at 12:44

2 Answers2

1

Modules are only checked at load time which is a good thing, You can use reload()

import importlib
importlib.reload(tools.py)
MortenB
  • 2,749
  • 1
  • 31
  • 35
1

A restart is not specifically required, you just need to reload the module

Python will cache the modules that are imported

from the docs

The first place checked during import search is sys.modules. This mapping serves as a cache of all modules that have been previously imported, including the intermediate paths.

You can delete the module from the cache by deleting the dictionary key from sys.modules and do the import again. Although, this is not the recommended approach (as other modules may hold references to it).

You should use importlib.reload From the docs

Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter.

Example,

import xyz # reload this
import importlib
importlib.reload(xyz)

Or

from xyz import pqr
import importlib
import sys
importlib.reload(sys.modules.get('xyz')

In your example, you need to do

import importlib
import sys
importlib.reload(sys.modules.get('tools'))

Doing a restart will clear the cache too, but it is not required.

dheeraj
  • 43
  • 4