Repository: Project-MONAI/MONAI

Issue:
monai suddenly stoped importing and produced errors
I used this:
pip install git+https://github.com/Project-MONAI/MONAI#egg=MONAI 
to import  and this issue occurred several times

<img width="1413" alt="Screen Shot 2020-09-03 at 4 13 32 PM" src="https://user-images.githubusercontent.com/33342277/92104115-9d054380-ee00-11ea-926e-6a7b3c1393f8.png">
monai suddenly stoped importing and produced errors
I used this:
pip install git+https://github.com/Project-MONAI/MONAI#egg=MONAI 
to import  and this issue occurred several times

<img width="1413" alt="Screen Shot 2020-09-03 at 4 13 32 PM" src="https://user-images.githubusercontent.com/33342277/92104115-9d054380-ee00-11ea-926e-6a7b3c1393f8.png">


Discussion:
Looks like it's trying to load `itk`, which you don't have installed. Try:
```
pip install -r https://raw.githubusercontent.com/Project-MONAI/MONAI/master/requirements-dev.txt
```
Hi @wyli ,

Seems we don't have this issue in `min dependency tests`?
Thanks.
Looks like it's trying to load `itk`, which you don't have installed. Try:
```
pip install -r https://raw.githubusercontent.com/Project-MONAI/MONAI/master/requirements-dev.txt
```
Hi @wyli ,

Seems we don't have this issue in `min dependency tests`?
Thanks.

Resolving patch:
diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py
--- a/monai/data/image_reader.py
+++ b/monai/data/image_reader.py
@@ -107,7 +107,7 @@ def verify_suffix(self, filename: Union[Sequence[str], str]) -> bool:
         """
         return True
 
-    def read(self, data: Union[Sequence[str], str, Image]):
+    def read(self, data: Union[Sequence[str], str, Any]):
         """
         Read image data from specified file or files, or set a `itk.Image` object.
         Note that the returned object is ITK image object or list of ITK image objects.
@@ -251,7 +251,7 @@ def verify_suffix(self, filename: Union[Sequence[str], str]) -> bool:
         suffixes: Sequence[str] = ["nii", "nii.gz"]
         return is_supported_format(filename, suffixes)
 
-    def read(self, data: Union[Sequence[str], str, Nifti1Image]):
+    def read(self, data: Union[Sequence[str], str, Any]):
         """
         Read image data from specified file or files, or set a Nibabel Image object.
         Note that the returned object is Nibabel image object or list of Nibabel image objects.
diff --git a/monai/utils/module.py b/monai/utils/module.py
--- a/monai/utils/module.py
+++ b/monai/utils/module.py
@@ -13,7 +13,7 @@
 from importlib import import_module
 from pkgutil import walk_packages
 from re import match
-from typing import Any, Callable, Tuple
+from typing import Any, Callable, List, Tuple
 
 OPTIONAL_IMPORT_MSG_FMT = "{}"
 
@@ -46,17 +46,19 @@ def load_submodules(basemod, load_all: bool = True, exclude_pattern: str = "(.*[
     `load_all` is True, excluding anything whose name matches `exclude_pattern`.
     """
     submodules = []
-
-    try:
-        for importer, name, is_pkg in walk_packages(basemod.__path__, prefix=basemod.__name__ + "."):
-            if (is_pkg or load_all) and name not in sys.modules and match(exclude_pattern, name) is None:
+    err_mod: List[str] = []
+    for importer, name, is_pkg in walk_packages(
+        basemod.__path__, prefix=basemod.__name__ + ".", onerror=err_mod.append
+    ):
+        if (is_pkg or load_all) and name not in sys.modules and match(exclude_pattern, name) is None:
+            try:
                 mod = import_module(name)
                 importer.find_module(name).load_module(name)
                 submodules.append(mod)
-    except OptionalImportError:
-        pass  # could not import the optional deps., they are ignored
+            except OptionalImportError:
+                pass  # could not import the optional deps., they are ignored
 
-    return submodules
+    return submodules, err_mod
 
 
 def get_full_type_name(typeobj):


Test patch:
diff --git a/tests/min_tests.py b/tests/min_tests.py
--- a/tests/min_tests.py
+++ b/tests/min_tests.py
@@ -92,6 +92,16 @@ def run_testsuit():
 
 
 if __name__ == "__main__":
+
+    # testing import submodules
+    from monai.utils.module import load_submodules
+
+    _, err_mod = load_submodules(sys.modules["monai"], True)
+    if err_mod:
+        # expecting that only engines and handlers are not imported
+        assert sorted(err_mod) == ["monai.engines", "monai.handlers"]
+
+    # testing all modules
     test_runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
     result = test_runner.run(run_testsuit())
     exit(int(not result.wasSuccessful()))