Skip to content

LazyModule not playing well with %autoreload #2995

@egaznep

Description

@egaznep

Describe the bug

Executing following in the terminal fails

python -m IPython
%load_ext autoreload
%autoreload 2
import speechbrain
Error in callback <bound method AutoreloadMagics.post_execute_hook of <IPython.extensions.autoreload.AutoreloadMagics object at 0x72a56e59e7b0>> (for post_execute), with arguments args (),kwargs {}:                                                                                                 
---------------------------------------------------------------------------                                                               
ModuleNotFoundError                       Traceback (most recent call last)                                                                        
File ~/miniconda3/envs/test/lib/python3.13/site-packages/speechbrain/integrations/nlp/flair_embeddings.py:12                                       
     11 try:                                                                                                                                       
---> 12     import flair           
     13     from flair.data import Sentence                     

ModuleNotFoundError: No module named 'flair'                                                                                                       
                                                                         
The above exception was the direct cause of the following exception:                                                                               
                                                                         
ImportError                               Traceback (most recent call last)           
File ~/miniconda3/envs/test/lib/python3.13/site-packages/speechbrain/utils/importutils.py:97, in LazyModule.ensure_module(self, stacklevel)        
     96 if self.package is None:                                         
---> 97     self.lazy_module = importlib.import_module(self.target)                                                                                
     98 else:                       
                                                                         
File ~/miniconda3/envs/test/lib/python3.13/importlib/__init__.py:88, in import_module(name, package)                                               
     87         level += 1                                               
---> 88 return _bootstrap._gcd_import(name[level:], package, level)                                                                                                                                                                                                                                    
                                                                         
File <frozen importlib._bootstrap>:1387, in _gcd_import(name, package, level) 
                                                                         
File <frozen importlib._bootstrap>:1360, in _find_and_load(name, import_)
                                                                         
File <frozen importlib._bootstrap>:1331, in _find_and_load_unlocked(name, import_)                                                                 

File <frozen importlib._bootstrap>:935, in _load_unlocked(spec)                                                                                    
                                                                         
File <frozen importlib._bootstrap_external>:1027, in exec_module(self, module)                                                                     
                                                                         
File <frozen importlib._bootstrap>:488, in _call_with_frames_removed(f, *args, **kwds)                                                             
                                                                                                                                                   
File ~/miniconda3/envs/test/lib/python3.13/site-packages/speechbrain/integrations/nlp/__init__.py:3                                                
      1 """Package providing simple wrappers for NLP models."""          
----> 3 from .flair_embeddings import *  # noqa                                                                                                    
      4 from .flair_tagger import *  # noqa  
File ~/miniconda3/envs/test/lib/python3.13/site-packages/speechbrain/integrations/nlp/flair_embeddings.py:16
     15 except ImportError as e:
---> 16     raise ImportError(
     17         f"Failed to import flair: {e}\n"
     18         f"Please install flair e.g. using `pip install flair`.\n"
     19         f"For more details, see https://github.com/flairNLP/flair"
     20     ) from e
     23 class FlairEmbeddings:

ImportError: Failed to import flair: No module named 'flair'
Please install flair e.g. using `pip install flair`.
For more details, see https://github.com/flairNLP/flair

The above exception was the direct cause of the following exception:

ImportError                               Traceback (most recent call last)
File ~/miniconda3/envs/test/lib/python3.13/site-packages/IPython/extensions/autoreload.py:869, in AutoreloadMagics.post_execute_hook(self)
    867 newly_loaded_modules = set(sys.modules) - self.loaded_modules
    868 for modname in newly_loaded_modules:
--> 869     _, pymtime = self._reloader.filename_and_mtime(sys.modules[modname])
    870     if pymtime is not None:
    871         self._reloader.modules_mtimes[modname] = pymtime

File ~/miniconda3/envs/test/lib/python3.13/site-packages/IPython/extensions/autoreload.py:247, in ModuleReloader.filename_and_mtime(self, module)
    246 def filename_and_mtime(self, module):
--> 247     if not hasattr(module, "__file__") or module.__file__ is None:
    248         return None, None
    250     if getattr(module, "__name__", None) in [None, "__mp_main__", "__main__"]:
    251         # we cannot reload(__main__) or reload(__mp_main__)

File ~/miniconda3/envs/test/lib/python3.13/site-packages/speechbrain/utils/importutils.py:112, in LazyModule.__getattr__(self, attr)
    110 def __getattr__(self, attr):
    111     # NOTE: exceptions here get eaten and not displayed
--> 112     return getattr(self.ensure_module(1), attr)

File ~/miniconda3/envs/test/lib/python3.13/site-packages/speechbrain/utils/importutils.py:172, in DeprecatedModuleRedirect.ensure_module(self, stacklevel)
    168 should_warn = self.lazy_module is None
    170 # can fail with exception if the module shouldn't be imported, so only
    171 # actually emit the warning later
--> 172 module = super().ensure_module(stacklevel + 1)
    174 if should_warn:
    175     self._redirection_warn()

File ~/miniconda3/envs/test/lib/python3.13/site-packages/speechbrain/utils/importutils.py:103, in LazyModule.ensure_module(self, stacklevel)
     99             self.lazy_module = importlib.import_module(
    100                 f".{self.target}", self.package
    101             )
    102     except Exception as e:
--> 103         raise ImportError(f"Lazy import of {repr(self)} failed") from e
    105 return self.lazy_module

ImportError: Lazy import of LazyModule(package=None, target=speechbrain.integrations.nlp, loaded=False) failed

Expected behaviour

not throwing an exception

To Reproduce

No response

Environment Details

run conda env create -f test.yaml with a test.yaml as given:

name: test

channels:
  - conda-forge

dependencies:
  - python=3.13
  - pip
  - jupyter
  - pip: 
    - git+https://github.com/speechbrain/speechbrain

Relevant Log Output

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions