Repository: pallets/click

Issue:
install importlib_metadata on Python < 3.8
`click 8.0.0` now relies on `importlib.metadata` under certain conditions.
On Python 3.8, click will raise an error and ask the user to install the `importlib_metadata` backport package.

Ex:
```
Traceback (most recent call last):
  File "/path_to_my_venv/bin/myexecutable", line 8, in <module>
    sys.exit(cli())
  File "/path_to_my_venv/lib/python3.6/site-packages/click/core.py", line 1134, in __call__
    return self.main(*args, **kwargs)
  File "/path_to_my_venv/lib/python3.6/site-packages/click/core.py", line 1058, in main
    with self.make_context(prog_name, args, **extra) as ctx:
  File "/path_to_my_venv/lib/python3.6/site-packages/click/core.py", line 927, in make_context
    self.parse_args(ctx, args)
  File "/path_to_my_venv/lib/python3.6/site-packages/click/core.py", line 1621, in parse_args
    rest = super().parse_args(ctx, args)
  File "/path_to_my_venv/lib/python3.6/site-packages/click/core.py", line 1376, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "/path_to_my_venv/lib/python3.6/site-packages/click/core.py", line 2352, in handle_parse_result
    value = self.process_value(ctx, value)
  File "/path_to_my_venv/lib/python3.6/site-packages/click/core.py", line 2314, in process_value
    value = self.callback(ctx, self, value)
  File "/path_to_my_venv/lib/python3.6/site-packages/click/decorators.py", line 380, in callback
    "Install 'importlib_metadata' to get the version on Python < 3.8."
RuntimeError: Install 'importlib_metadata' to get the version on Python < 3.8.
```

The user can install `importlib_metadata`, but I think Click could use environment markers to install the `importlib_metadata` backport package on Python 3.8, in the same manner that `colorama` is installed on Windows.

I will send a PR soon. Feel free to express any concerns, doubts or considerations to the project context that I've missed.

Environment:

- Python version: 3.6
- Click version: 8.0.0



Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -1,3 +1,9 @@
 from setuptools import setup
 
-setup(name="click", install_requires=["colorama; platform_system == 'Windows'"])
+setup(
+    name="click",
+    install_requires=[
+        "colorama; platform_system == 'Windows'",
+        "importlib-metadata; python_version < '3.8'",
+    ],
+)
diff --git a/src/click/decorators.py b/src/click/decorators.py
--- a/src/click/decorators.py
+++ b/src/click/decorators.py
@@ -370,15 +370,7 @@ def callback(ctx: Context, param: Parameter, value: bool) -> None:
                 from importlib import metadata  # type: ignore
             except ImportError:
                 # Python < 3.8
-                try:
-                    import importlib_metadata as metadata  # type: ignore
-                except ImportError:
-                    metadata = None
-
-            if metadata is None:
-                raise RuntimeError(
-                    "Install 'importlib_metadata' to get the version on Python < 3.8."
-                )
+                import importlib_metadata as metadata  # type: ignore
 
             try:
                 version = metadata.version(package_name)  # type: ignore


Test patch:
diff --git a/requirements/tests.in b/requirements/tests.in
--- a/requirements/tests.in
+++ b/requirements/tests.in
@@ -1,2 +1 @@
 pytest
-importlib_metadata
diff --git a/requirements/tests.txt b/requirements/tests.txt
--- a/requirements/tests.txt
+++ b/requirements/tests.txt
@@ -6,8 +6,6 @@
 #
 attrs==21.2.0
     # via pytest
-importlib-metadata==4.0.1
-    # via -r requirements/tests.in
 iniconfig==1.1.1
     # via pytest
 packaging==20.9
@@ -22,5 +20,3 @@ pytest==6.2.4
     # via -r requirements/tests.in
 toml==0.10.2
     # via pytest
-zipp==3.4.1
-    # via importlib-metadata