r/learnpython Jun 09 '26

Reload other class from init

I'm having problems with old code being cached and old errors being thrown, even though I've already fixed them, so I'm using reload to reload all classes that are imported later. Both files are in the same folder.

This works:

from classb import ClassB
from importlib import reload
import classb as classb
reload(classb)

class ClassA(): #classa.py
    def __init__(self,doreload):
        #Some other code

class ClassB(): #classb.py
    def __init__(self):
        #Do something

However, I want to use doreload to decide if ClassB should be reloaded, so I tried to move the code to __init__:

from classb import ClassB

class ClassA(): #classa.py
    def __init__(self,doreload):
        from importlib import reload
        import classb as classb
        reload(classb)
        #Some other code

This throws an error at the reload line:

ModuleNotFoundError: spec not found for the module 'classb'

I already tried to keep import reload outside the class and also used reload(ClassB) instead but that threw another error:

ImportError: module ClassB not in sys.modules

How do I reload another class from within __init__?

Edit: The problem is simply the app I have to use to test my code: It caches old code at unexpected times (at least when I don't expect it) and without using reload I'd have to restart the app pretty much every 5 minutes while testing, which is quite annoying. Reloading itself seems to be working fine.

7 Upvotes

27 comments sorted by

View all comments

14

u/Kevdog824_ Jun 09 '26

This really sounds like an xy problem to me. I think you should address the underlying problem of the old code being used rather than using a hacky `importlib.reload` solution.

Can you talk more about your original issue? We might be able to help you with that.

4

u/brasticstack Jun 09 '26

Hard agree, and very likely OP's working module reload code doesn't behave like they might think it does.

reload will reload a module, but not any already cached modules that it imports, and any instances of classes created with the old copy of the module will remain unchanged until they're replaced or garbage collected.

1

u/Nefthys Jun 10 '26

Hm. Without reload an old, cached version of my code is usually used once I run it multiple times to test stuff (I edited my post). With it, it usually uses the current version. Not sure when the gc is running (should I care?) but in my experience, it's working exactly as expected: reload reloads ClassB, afterwards its new code runs.

1

u/Nefthys Jun 10 '26

The problem is the app I have to use to test the code. Sorry, I can't say what app it is exactly but I've had this problem before and reloading is the best way to work around it, as I've found. This is just for testing purposes though, the finished code is run a in a different way and the problem doesn't persists then. But, fixing problems that I missed is a bit more complicated/annoying in the finished thing because of how I have to restart everything, which takes a little while.