Skip to content

pip list: (Reopened) Incorrect version displayed with pip list when using --target and PYTHONPATH #12916

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions news/12890.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the incorrect version displayed with pip list when using --target and PYTHONPATH.
26 changes: 25 additions & 1 deletion src/pip/_internal/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import operator
import os
import re
import shutil
import site
from optparse import SUPPRESS_HELP, Values
Expand Down Expand Up @@ -574,7 +575,15 @@ def _handle_target_dir(
shutil.rmtree(target_item_dir)
else:
os.remove(target_item_dir)

if item.endswith((".dist-info", ".egg-info")):
matched_info_dir = _contain_info_dir(item, target_dir)
if matched_info_dir:
if not upgrade:
continue
else:
info_dir = os.path.join(target_dir, matched_info_dir)
if os.path.exists(info_dir):
shutil.rmtree(info_dir)
shutil.move(os.path.join(lib_dir, item), target_item_dir)

def _determine_conflicts(
Expand Down Expand Up @@ -781,3 +790,18 @@ def create_os_error_message(
)

return "".join(parts).strip() + "\n"


def _contain_info_dir(item: str, target_dir: str) -> Optional[str]:
"""Determine whether the item is a metadata_location
(.dist-info or egg-info for legacy).
If there is another metadata_location for the package in the 'target_dir',
return found metadata_location that has been found.
"""
raw_name = item.rpartition(".")[0].partition("-")[0]
dist_info_re = re.compile(rf"{raw_name}-[a-z0-9_.!+-]+\.dist-info$", re.IGNORECASE)
egg_info_re = re.compile(rf"{raw_name}[^\s/\\]*\.egg-info$", re.IGNORECASE)
for path in os.listdir(target_dir):
if dist_info_re.match(path) or egg_info_re.match(path):
return path
return None