Skip to content

Logging error when checking for new version of pip. #11309

@domdfcoding

Description

@domdfcoding

Description

When pip (22.1.2) checked for a new version it failed with an error. It's coming from the following function:

def get_best_invocation_for_this_pip() -> str:
"""Try to figure out the best way to invoke pip in the current environment."""
binary_directory = "Scripts" if WINDOWS else "bin"
binary_prefix = os.path.join(sys.prefix, binary_directory)
# Try to use pip[X[.Y]] names, if those executables for this environment are
# the first on PATH with that name.
path_parts = os.path.normcase(os.environ.get("PATH", "")).split(os.pathsep)
exe_are_in_PATH = os.path.normcase(binary_prefix) in path_parts
if exe_are_in_PATH:
for exe_name in _EXECUTABLE_NAMES:
found_executable = shutil.which(exe_name)
if found_executable and os.path.samefile(
found_executable,
os.path.join(binary_prefix, exe_name),
):
return exe_name

The problem call is to os.path.samefile on line 58, where it compares the output of shutil.which('pip') to <sys.prefix>/bin/pip (in my case /usr/bin/pip). However, on my system, pip is installed to the user site-packages directory (so the binary is at /home/domdf/.local/bin/pip).

The solution is to check whether the file exists before calling samefile.

I have Python 3.7 and 3.9 installed to /usr alongside the system's Python 3.8, and the error is present with all three versions.

Expected behavior

Pip checks for a new version without an error.

pip version

22.1.2

Python version

3.9.13

OS

Ubuntu 20.04

How to Reproduce

  1. pip install pip==22.1.2
  2. pip install pip <- Any package will do.

Output

$ pip install pip
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pip in ./.local/lib/python3.9/site-packages (22.1.2)
--- Logging error ---
Traceback (most recent call last):
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_internal/utils/logging.py", line 177, in emit
    self.console.print(renderable, overflow="ignore", crop=False, style=style)
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_vendor/rich/console.py", line 1752, in print
    extend(render(renderable, render_options))
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_vendor/rich/console.py", line 1390, in render
    for render_output in iter_render:
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_internal/utils/logging.py", line 134, in __rich_console__
    for line in lines:
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_vendor/rich/segment.py", line 245, in split_lines
    for segment in segments:
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_vendor/rich/console.py", line 1368, in render
    renderable = rich_cast(renderable)
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_vendor/rich/protocol.py", line 36, in rich_cast
    renderable = cast_method()
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_internal/self_outdated_check.py", line 130, in __rich__
    pip_cmd = get_best_invocation_for_this_pip()
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_internal/utils/entrypoints.py", line 58, in get_best_invocation_for_this_pip
    if found_executable and os.path.samefile(
  File "/usr/lib/python3.9/genericpath.py", line 101, in samefile
    s2 = os.stat(f2)
FileNotFoundError: [Errno 2] No such file or directory: '/usr/bin/pip'
Call stack:
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/__main__.py", line 31, in <module>
    sys.exit(_main())
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_internal/cli/main.py", line 70, in main
    return command.main(cmd_args)
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_internal/cli/base_command.py", line 101, in main
    return self._main(args)
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_internal/cli/base_command.py", line 223, in _main
    self.handle_pip_version_check(options)
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_internal/cli/req_command.py", line 148, in handle_pip_version_check
    pip_self_version_check(session, options)
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_internal/self_outdated_check.py", line 237, in pip_self_version_check
    logger.info("[present-rich] %s", upgrade_prompt)
  File "/usr/lib/python3.9/logging/__init__.py", line 1446, in info
    self._log(INFO, msg, args, **kwargs)
  File "/usr/lib/python3.9/logging/__init__.py", line 1589, in _log
    self.handle(record)
  File "/usr/lib/python3.9/logging/__init__.py", line 1599, in handle
    self.callHandlers(record)
  File "/usr/lib/python3.9/logging/__init__.py", line 1661, in callHandlers
    hdlr.handle(record)
  File "/usr/lib/python3.9/logging/__init__.py", line 952, in handle
    self.emit(record)
  File "/home/domdf/.local/lib/python3.9/site-packages/pip/_internal/utils/logging.py", line 179, in emit
    self.handleError(record)
Message: '[present-rich] %s'
Arguments: (UpgradePrompt(old='22.1.2', new='22.2'),)

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: awaiting PRFeature discussed, PR is neededtype: bugA confirmed bug or unintended behavior

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions