-1

I'm trying to reload from a python script a custom function because i'm editing the function constantly and i dont want to reload the kernel each time i make a change on the file. I have read that i can use importlib .

However, when i try to use reload i get ImportError: module Classifier not in sys.modules

My code:

from importlib import reload
from FilterFile import Classifier as foo

reload(foo)

I tried just using reload(Classifier) and reload(FilterFile import Classifier) , but i'm getting the same error.

1
  • 1
    I assume that Classifier is the actual function, rather than a submodule containing the function (I can't tell from the details you posted). importlib.reload() only deals with entire modules, not objects contained within them - but you haven't imported the module itself, so there is nothing you can validly pass to reload(). You need to write this as import FilterFile and reload(FilterFile) - the function would be referred to as FilterFile.Classifier. If you still want to usefrom FilterFile import Classifier as foo, you'd have to repeat that line after EVERY reload. Commented Jul 30, 2024 at 19:08

1 Answer 1

0

In this case, Classifier isn't the module or a sub-module, it's something you've imported from the FilterFile module (from-imports actually still import everything from the module, but only allow you to access what you've requested).

You need to do import FilterFile instead (you can still do something like import FilterFile as foo, as long as the thing you're reloading is a reference to the actual module).


I tested the following as my Classifier function (the same logic applies for classes and such):

def Classifier():
    print('before')

While testing, I would change 'before' to 'after' during a delay. When I tried the following, no error occurred but Classifier didn't actually update unless called through the module:

import time
import FilterFile as bar
from importlib import reload
from FilterFile import Classifier as foo

foo()             # "before"
time.sleep(3.5)   # (a delay so I could change `Classifier`)
reload(bar)
foo()             # "before" <- wrong!!!
bar.Classifier()  # "after"

However, you can redo the from-import to update it:

import time
import FilterFile as bar
from importlib import reload
from FilterFile import Classifier as foo

foo()             # "before"
time.sleep(3.5)   # (a delay so I could change `Classifier`)
reload(bar)
from FilterFile import Classifier as foo
foo()             # "after" <- hooray!!!

I recommend wrapping importlib.reload() in your own function that also redoes your from-imports. There might be a hacky way to do this without needing a true import statement and without redoing the from-imports, but I hope this is acceptable for your purposes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.