Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search all distributions when trying to find a match in get_distribution() #8702

Merged
merged 5 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions news/8695.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix regression that distributions in system site-packages are not correctly
found when a virtual environment is configured with ``system-site-packages``
on.
1 change: 1 addition & 0 deletions src/pip/_internal/commands/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def print_results(hits, name_column_width=None, terminal_width=None):
write_output(line)
if name in installed_packages:
dist = get_distribution(name)
assert dist is not None
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole module is using the distribution-related functions in a very inprecise way. I’ll submit a standalone refactoring PR after this is accepted

with indent_log():
if dist.version == latest:
write_output('INSTALLED: %s (latest)', dist.version)
Expand Down
27 changes: 22 additions & 5 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,22 +483,39 @@ def user_test(d):
]


def search_distribution(req_name):
def _search_distribution(req_name):
# type: (str) -> Optional[Distribution]
"""Find a distribution matching the ``req_name`` in the environment.

This searches from *all* distributions available in the environment, to
match the behavior of ``pkg_resources.get_distribution()``.
"""
# Canonicalize the name before searching in the list of
# installed distributions and also while creating the package
# dictionary to get the Distribution object
req_name = canonicalize_name(req_name)
packages = get_installed_distributions(skip=())
packages = get_installed_distributions(
local_only=False,
skip=(),
include_editables=True,
editables_only=False,
user_only=False,
paths=None,
)
pkg_dict = {canonicalize_name(p.key): p for p in packages}
return pkg_dict.get(req_name)


def get_distribution(req_name):
"""Given a requirement name, return the installed Distribution object"""
# type: (str) -> Optional[Distribution]
"""Given a requirement name, return the installed Distribution object.

This searches from *all* distributions available in the environment, to
match the behavior of ``pkg_resources.get_distribution()``.
"""

# Search the distribution by looking through the working set
dist = search_distribution(req_name)
dist = _search_distribution(req_name)

# If distribution could not be found, call working_set.require
# to update the working set, and try to find the distribution
Expand All @@ -514,7 +531,7 @@ def get_distribution(req_name):
pkg_resources.working_set.require(req_name)
except pkg_resources.DistributionNotFound:
return None
return search_distribution(req_name)
return _search_distribution(req_name)


def egg_link_path(dist):
Expand Down