Repository: spyder-ide/spyder

Issue:
Missing mandatory packages does not raise error
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

1. Uninstall a mandatory package, e.g. `jellyfish`
2. Launch Spyder, e.g. via bootstrap
3. No error is raised
4. Dependency widget shows no version installed, but still marks as OK



## Versions

* Spyder version: 5.3.0.dev0 74973f6ff
* Python version: 3.9.7
* Qt version: 5.12.9
* PyQt5 version: 5.12.3
* Operating System: Darwin 20.6.0

### Dependencies

```
# Mandatory:
applaunchservices >=0.1.7     :  0.2.1 (OK)
atomicwrites >=1.2.0          :  1.4.0 (OK)
chardet >=2.0.0               :  4.0.0 (OK)
cloudpickle >=0.5.0           :  2.0.0 (OK)
cookiecutter >=1.6.0          :  1.7.3 (OK)
diff_match_patch >=20181111   :  20200713 (OK)
intervaltree >=3.0.2          :  3.0.2 (OK)
IPython >=7.6.0               :  7.30.0 (OK)
jedi >=0.17.2;<0.19.0         :  0.18.1 (OK)
jellyfish >=0.7               :  None (OK)
jsonschema >=3.2.0            :  4.2.1 (OK)
keyring >=17.0.0              :  23.4.0 (OK)
nbconvert >=4.0               :  6.3.0 (OK)
numpydoc >=0.6.0              :  1.1.0 (OK)
parso >=0.7.0;<0.9.0          :  0.8.3 (OK)
pexpect >=4.4.0               :  4.8.0 (OK)
pickleshare >=0.4             :  0.7.5 (OK)
psutil >=5.3                  :  5.8.0 (OK)
pygments >=2.0                :  2.10.0 (OK)
pylint >=2.5.0                :  2.12.1 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.3.1;<1.4.0          :  1.4.0.dev0 (OK)
pylsp_black >=1.0.0           :  None (OK)
qdarkstyle =3.0.2             :  3.0.2 (OK)
qstylizer >=0.1.10            :  0.2.1 (OK)
qtawesome >=1.0.2             :  1.1.1 (OK)
qtconsole >=5.2.1;<5.3.0      :  5.2.1 (OK)
qtpy >=1.5.0                  :  1.11.2 (OK)
rtree >=0.9.7                 :  0.9.7 (OK)
setuptools >=49.6.0           :  59.4.0 (OK)
sphinx >=0.6.6                :  4.3.1 (OK)
spyder_kernels >=2.2.0;<2.3.0 :  2.3.0.dev0 (OK)
textdistance >=4.2.0          :  4.2.2 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog >=0.10.3             :  2.1.6 (OK)
zmq >=17                      :  22.3.0 (OK)

# Optional:
cython >=0.21                 :  0.29.24 (OK)
matplotlib >=2.0.0            :  3.5.0 (OK)
numpy >=1.7                   :  1.21.4 (OK)
pandas >=1.1.1                :  1.3.4 (OK)
scipy >=0.17.0                :  1.7.3 (OK)
sympy >=0.7.3                 :  1.9 (OK)
```



Discussion:

Resolving patch:
diff --git a/spyder/dependencies.py b/spyder/dependencies.py
--- a/spyder/dependencies.py
+++ b/spyder/dependencies.py
@@ -307,9 +307,23 @@ def __init__(self, modname, package_name, features, required_version,
         self.required_version = required_version
         self.kind = kind
 
+        # Although this is not necessarily the case, it's customary that a
+        # package's distribution name be it's name on PyPI with hyphens
+        # replaced by underscores.
+        # Example:
+        # * Package name: python-lsp-black.
+        # * Distribution name: python_lsp_black
+        self.distribution_name = self.package_name.replace('-', '_')
+
         if installed_version is None:
             try:
                 self.installed_version = programs.get_module_version(modname)
+                if not self.installed_version:
+                    # Use get_package_version and the distribution name
+                    # because there are cases for which the version can't
+                    # be obtained from the module (e.g. pylsp_black).
+                    self.installed_version = programs.get_package_version(
+                        self.distribution_name)
             except Exception:
                 # NOTE: Don't add any exception type here!
                 # Modules can fail to import in several ways besides
@@ -321,8 +335,12 @@ def __init__(self, modname, package_name, features, required_version,
     def check(self):
         """Check if dependency is installed"""
         if self.required_version:
-            return programs.is_module_installed(self.modname,
-                                                self.required_version)
+            installed = programs.is_module_installed(
+                self.modname,
+                self.required_version,
+                distribution_name=self.distribution_name
+            )
+            return installed
         else:
             return True
 
@@ -386,19 +404,21 @@ def status(deps=DEPENDENCIES, linesep=os.linesep):
     maxwidth += 1
     text = ""
     prev_order = '-1'
-    for order, title, version in sorted(data,
-                                        key=lambda x: x[0] + x[1].lower()):
+    for order, title, version in sorted(
+            data, key=lambda x: x[0] + x[1].lower()):
         if order != prev_order:
-            text += '{sep}# {name}:{sep}'.format(
-                sep=linesep, name=order_dep[order].capitalize())
+            name = order_dep[order]
+            if name == MANDATORY:
+                text += f'# {name.capitalize()}:{linesep}'
+            else:
+                text += f'{linesep}# {name.capitalize()}:{linesep}'
             prev_order = order
 
-        text += '{title}:  {version}{linesep}'.format(
-            title=title.ljust(maxwidth), version=version, linesep=linesep)
+        text += f'{title.ljust(maxwidth)}:  {version}{linesep}'
 
-    # Remove spurious linesep's when reporting deps to Github
+    # Remove spurious linesep when reporting deps to Github
     if not linesep == '<br>':
-        text = text[1:-1]
+        text = text[:-1]
 
     return text
 
diff --git a/spyder/utils/programs.py b/spyder/utils/programs.py
--- a/spyder/utils/programs.py
+++ b/spyder/utils/programs.py
@@ -852,12 +852,8 @@ def check_version(actver, version, cmp_op):
 
 def get_module_version(module_name):
     """Return module version or None if version can't be retrieved."""
-    ver = None
-    try:
-        mod = __import__(module_name)
-        ver = getattr(mod, '__version__', getattr(mod, 'VERSION', None))
-    except ModuleNotFoundError:
-        pass
+    mod = __import__(module_name)
+    ver = getattr(mod, '__version__', getattr(mod, 'VERSION', None))
     if not ver:
         ver = get_package_version(module_name)
     return ver
@@ -875,7 +871,8 @@ def get_package_version(package_name):
         return None
 
 
-def is_module_installed(module_name, version=None, interpreter=None):
+def is_module_installed(module_name, version=None, interpreter=None,
+                        distribution_name=None):
     """
     Return True if module ``module_name`` is installed
 
@@ -889,6 +886,9 @@ def is_module_installed(module_name, version=None, interpreter=None):
     If ``interpreter`` is not None, checks if a module is installed with a
     given ``version`` in the ``interpreter``'s environment. Otherwise checks
     in Spyder's environment.
+
+    ``distribution_name`` is the distribution name of a package. For instance,
+    for pylsp_black that name is python_lsp_black.
     """
     if interpreter is not None:
         if is_python_interpreter(interpreter):
@@ -934,6 +934,12 @@ def is_module_installed(module_name, version=None, interpreter=None):
         except Exception:
             pass
 
+        # Try to get the module version from its distribution name. For
+        # instance, pylsp_black doesn't have a version but that can be
+        # obtained from its distribution, called python_lsp_black.
+        if not module_version and distribution_name:
+            module_version = get_package_version(distribution_name)
+
     if version is None:
         return True
     else:


Test patch:
diff --git a/spyder/utils/tests/test_programs.py b/spyder/utils/tests/test_programs.py
--- a/spyder/utils/tests/test_programs.py
+++ b/spyder/utils/tests/test_programs.py
@@ -188,6 +188,8 @@ def test_is_module_installed():
     assert is_module_installed('qtconsole', '>=4.5')
     assert not is_module_installed('IPython', '>=1.0;<3.0')
     assert is_module_installed('jedi', '>=0.7.0')
+    assert not is_module_installed('foo')
+    assert not is_module_installed('foo1', '>=1.2.0')
 
 
 def test_is_module_installed_with_custom_interpreter():
@@ -286,12 +288,13 @@ def test_open_files_with_application(tmp_path):
 def test_get_package_version():
     # Primarily a test of pkg_resources/setuptools being installed properly
     assert get_package_version('IPython')
+    assert get_package_version('python_lsp_black')
 
 
 def test_get_module_version():
-    # pyls_black should not have a __version__ attribute, so tests that the
+    # intervaltree should not have a __version__ attribute, so test that the
     # fallback mechanism to get_package_version is working
-    assert get_module_version('python_lsp_black')
+    assert get_module_version('intervaltree')
 
 
 if __name__ == '__main__':