Repository: Project-MONAI/MONAI

Issue:
monai-weekly/monai pip install conflicting `load_submodules`
**Describe the bug**
running `pip install monai` after running `pip install monai-weekly` causes module import random errors:

```bash
$ pip install monai-weekly
Collecting monai-weekly
  Using cached monai_weekly-0.10.dev2234-py3-none-any.whl (1.0 MB)
Requirement already satisfied: numpy>=1.17 in /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages (from monai-weekly) (1.23.2)
Requirement already satisfied: torch>=1.7 in /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages (from monai-weekly) (1.12.1)
Requirement already satisfied: typing-extensions in /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages (from torch>=1.7->monai-weekly) (4.3.0)
Installing collected packages: monai-weekly
Successfully installed monai-weekly-0.10.dev2234

$ pip install monai
Collecting monai
  Using cached monai-0.9.1-202207251608-py3-none-any.whl (990 kB)
Requirement already satisfied: numpy>=1.17 in /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages (from monai) (1.23.2)
Requirement already satisfied: torch>=1.7 in /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages (from monai) (1.12.1)
Requirement already satisfied: typing-extensions in /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages (from torch>=1.7->monai) (4.3.0)
Installing collected packages: monai
Successfully installed monai-0.9.1

$ python
Python 3.8.12 (default, Oct 12 2021, 06:23:56) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import monai
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/monai/__init__.py", line 48, in <module>
    load_submodules(sys.modules[__name__], True, exclude_pattern=excludes)
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/monai/utils/module.py", line 191, in load_submodules
    mod = import_module(name)
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/monai/apps/reconstruction/networks/blocks/varnetblock.py", line 16, in <module>
    from monai.apps.reconstruction.networks.nets.utils import sensitivity_map_expand, sensitivity_map_reduce
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/monai/apps/reconstruction/networks/nets/utils.py", line 21, in <module>
    from monai.apps.reconstruction.complex_utils import complex_conj_t, complex_mul_t
ImportError: cannot import name 'complex_conj_t' from 'monai.apps.reconstruction.complex_utils' (/usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/monai/apps/reconstruction/complex_utils.py)
>>> 
```

**Expected behavior**
no import error


**Additional context**
the root cause is both pip install `monai-weekly` and `monai` may write to the same folder 
 (e.g., `python3.8/site-packages/monai/`), the `load_submodules` function tries to preload all subfolders: https://github.com/Project-MONAI/MONAI/blob/aa93129d795b64647c3bfb7f7ebdeaf715ce30c0/monai/__init__.py#L45


a workaround would be always run `pip uninstall monai-weekly` and `pip uninstall monai` *multiple times* before pip installing a new version (either monai-weekly or monai).


monai-weekly/monai pip install conflicting `load_submodules`
**Describe the bug**
running `pip install monai` after running `pip install monai-weekly` causes module import random errors:

```bash
$ pip install monai-weekly
Collecting monai-weekly
  Using cached monai_weekly-0.10.dev2234-py3-none-any.whl (1.0 MB)
Requirement already satisfied: numpy>=1.17 in /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages (from monai-weekly) (1.23.2)
Requirement already satisfied: torch>=1.7 in /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages (from monai-weekly) (1.12.1)
Requirement already satisfied: typing-extensions in /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages (from torch>=1.7->monai-weekly) (4.3.0)
Installing collected packages: monai-weekly
Successfully installed monai-weekly-0.10.dev2234

$ pip install monai
Collecting monai
  Using cached monai-0.9.1-202207251608-py3-none-any.whl (990 kB)
Requirement already satisfied: numpy>=1.17 in /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages (from monai) (1.23.2)
Requirement already satisfied: torch>=1.7 in /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages (from monai) (1.12.1)
Requirement already satisfied: typing-extensions in /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages (from torch>=1.7->monai) (4.3.0)
Installing collected packages: monai
Successfully installed monai-0.9.1

$ python
Python 3.8.12 (default, Oct 12 2021, 06:23:56) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import monai
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/monai/__init__.py", line 48, in <module>
    load_submodules(sys.modules[__name__], True, exclude_pattern=excludes)
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/monai/utils/module.py", line 191, in load_submodules
    mod = import_module(name)
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/monai/apps/reconstruction/networks/blocks/varnetblock.py", line 16, in <module>
    from monai.apps.reconstruction.networks.nets.utils import sensitivity_map_expand, sensitivity_map_reduce
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/monai/apps/reconstruction/networks/nets/utils.py", line 21, in <module>
    from monai.apps.reconstruction.complex_utils import complex_conj_t, complex_mul_t
ImportError: cannot import name 'complex_conj_t' from 'monai.apps.reconstruction.complex_utils' (/usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/monai/apps/reconstruction/complex_utils.py)
>>> 
```

**Expected behavior**
no import error


**Additional context**
the root cause is both pip install `monai-weekly` and `monai` may write to the same folder 
 (e.g., `python3.8/site-packages/monai/`), the `load_submodules` function tries to preload all subfolders: https://github.com/Project-MONAI/MONAI/blob/aa93129d795b64647c3bfb7f7ebdeaf715ce30c0/monai/__init__.py#L45


a workaround would be always run `pip uninstall monai-weekly` and `pip uninstall monai` *multiple times* before pip installing a new version (either monai-weekly or monai).




Discussion:

Resolving patch:
diff --git a/monai/utils/module.py b/monai/utils/module.py
--- a/monai/utils/module.py
+++ b/monai/utils/module.py
@@ -193,6 +193,12 @@ def load_submodules(basemod, load_all: bool = True, exclude_pattern: str = "(.*[
                 submodules.append(mod)
             except OptionalImportError:
                 pass  # could not import the optional deps., they are ignored
+            except ImportError as e:
+                raise ImportError(
+                    "Multiple versions of MONAI may have been installed,\n"
+                    "please uninstall existing packages (both monai and monai-weekly) and install a version again.\n"
+                    "See also: https://docs.monai.io/en/stable/installation.html\n"
+                ) from e
 
     return submodules, err_mod
 


Test patch: