diff --git a/pip/commands/install.py b/pip/commands/install.py index d957f407d69..227c5264a0d 100644 --- a/pip/commands/install.py +++ b/pip/commands/install.py @@ -18,7 +18,7 @@ InstallationError, CommandError, PreviousBuildDirError, ) from pip import cmdoptions -from pip.utils import ensure_dir +from pip.utils import ensure_dir, get_installed_version from pip.utils.build import BuildDirectory from pip.utils.deprecation import RemovedInPip10Warning from pip.utils.filesystem import check_path_owner @@ -341,6 +341,14 @@ def run(self, options, args): root=options.root_path, prefix=options.prefix_path, ) + + possible_lib_locations = get_lib_location_guesses( + user=options.use_user_site, + home=temp_target_dir, + root=options.root_path, + prefix=options.prefix_path, + isolated=options.isolated_mode, + ) reqs = sorted( requirement_set.successfully_installed, key=operator.attrgetter('name')) @@ -348,9 +356,11 @@ def run(self, options, args): for req in reqs: item = req.name try: - if hasattr(req, 'installed_version'): - if req.installed_version: - item += '-' + req.installed_version + installed_version = get_installed_version( + req.name, possible_lib_locations + ) + if installed_version: + item += '-' + installed_version except Exception: pass items.append(item) @@ -420,3 +430,8 @@ def run(self, options, args): ) shutil.rmtree(temp_target_dir) return requirement_set + + +def get_lib_location_guesses(*args, **kwargs): + scheme = distutils_scheme('', *args, **kwargs) + return [scheme['purelib'], scheme['platlib']] diff --git a/pip/utils/__init__.py b/pip/utils/__init__.py index b5d01cde61f..feecf3e5132 100644 --- a/pip/utils/__init__.py +++ b/pip/utils/__init__.py @@ -838,14 +838,17 @@ def __get__(self, obj, cls): return value -def get_installed_version(dist_name): +def get_installed_version(dist_name, lookup_dirs=None): """Get the installed version of dist_name avoiding pkg_resources cache""" # Create a requirement that we'll look for inside of setuptools. req = pkg_resources.Requirement.parse(dist_name) # We want to avoid having this cached, so we need to construct a new # working set each time. - working_set = pkg_resources.WorkingSet() + if lookup_dirs is None: + working_set = pkg_resources.WorkingSet() + else: + working_set = pkg_resources.WorkingSet(lookup_dirs) # Get the installed distribution from our working set dist = working_set.find(req)