|
7 | 7 | from optparse import SUPPRESS_HELP, Values
|
8 | 8 | from typing import List, Optional
|
9 | 9 |
|
| 10 | +from pip._vendor.packaging.utils import canonicalize_name |
10 | 11 | from pip._vendor.rich import print_json
|
11 | 12 |
|
12 | 13 | from pip._internal.cache import WheelCache
|
@@ -472,25 +473,29 @@ def run(self, options: Values, args: List[str]) -> int:
|
472 | 473 | )
|
473 | 474 | env = get_environment(lib_locations)
|
474 | 475 |
|
| 476 | + # Display a summary of installed packages, with extra care to |
| 477 | + # display a package name as it was requested by the user. |
475 | 478 | installed.sort(key=operator.attrgetter("name"))
|
476 |
| - items = [] |
477 |
| - for result in installed: |
478 |
| - item = result.name |
479 |
| - try: |
480 |
| - installed_dist = env.get_distribution(item) |
481 |
| - if installed_dist is not None: |
482 |
| - item = f"{item}-{installed_dist.version}" |
483 |
| - except Exception: |
484 |
| - pass |
485 |
| - items.append(item) |
| 479 | + summary = [] |
| 480 | + installed_versions = {} |
| 481 | + for distribution in env.iter_all_distributions(): |
| 482 | + installed_versions[distribution.canonical_name] = distribution.version |
| 483 | + for package in installed: |
| 484 | + display_name = package.name |
| 485 | + version = installed_versions.get(canonicalize_name(display_name), None) |
| 486 | + if version: |
| 487 | + text = f"{display_name}-{version}" |
| 488 | + else: |
| 489 | + text = display_name |
| 490 | + summary.append(text) |
486 | 491 |
|
487 | 492 | if conflicts is not None:
|
488 | 493 | self._warn_about_conflicts(
|
489 | 494 | conflicts,
|
490 | 495 | resolver_variant=self.determine_resolver_variant(options),
|
491 | 496 | )
|
492 | 497 |
|
493 |
| - installed_desc = " ".join(items) |
| 498 | + installed_desc = " ".join(summary) |
494 | 499 | if installed_desc:
|
495 | 500 | write_output(
|
496 | 501 | "Successfully installed %s",
|
|
0 commit comments