GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Support path-like objects in shutil.which() · Issue #161788 · python/cpython

Support path-like objects in shutil.which()  #161788

New issue
Copy link
New issue
Copy link
Open
Open
Support path-like objects in shutil.which() #161788
Copy link
Labels
pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@Builder106
Builder106
opened on Nov 10, 2026
Issue body actions

shutil.which() accepts a bytes command and returns a bytes path. It also accepts path-like command objects, but a path-like object whose __fspath__() method returns bytes raises TypeError instead of behaving like the equivalent direct bytes value.

import shutil


class BytesPath:
    def __fspath__(self):
        return b"python3"


print(shutil.which(b"python3"))
print(shutil.which(BytesPath()))

On CPython main, the first call returns a bytes path and the second fails:

b'/usr/bin/python3'
TypeError: Can't mix strings and bytes in path components

which() checks whether cmd is bytes before resolving the path-like object. It then decodes PATH to str, while os.path.split() has resolved the command to bytes. Calling os.fspath(cmd) before choosing the string or bytes path keeps the command and search path on the same type.

CPython versions tested on

  • CPython main branch (3.16.0a2+)

Operating systems tested on

  • Linux ARM64

Linked PRs

  • gh-161788: Handle path-like commands in shutil.which() #161789

Activity

  1. aisk
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Nov 11, 2026
  2. picnixz commented on Nov 11, 2026

    @picnixz
    picnixz
    on Nov 11, 2026
    Member
    More actions

    I don't think it's a bug. It accepts path-like objects by change I think, not by contract. Supporting bytes was a new feature because it also returned bytes. But looking at older issues, it's not clear whether shutil.which() should support path-like objects (see #90883, #67430 as well)

  3. picnixz
    added
    type-featureA feature request or enhancementA feature request or enhancement
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    and removed
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Nov 11, 2026
  4. picnixz
    changed the title [-]shutil.which() fails for bytes-backed path-like commands[/-] [+]Support path-like objects in shutil.which() [/+] on Nov 11, 2026
  5. Builder106 commented on Nov 11, 2026

    @Builder106
    Builder106
    on Nov 11, 2026
    ContributorAuthor
    More actions

    @picnixz

    Oh right, hadn't considered that path-like support might be accidental. I was thinking the return type could follow whatever os.fspath() returns, but I do see your point.

    Do you think it's worth supporting that, or should we leave path-like commands unsupported?

  6. picnixz commented on Nov 11, 2026

    @picnixz
    picnixz
    on Nov 11, 2026
    Member
    More actions

    Considering how hard it was for adding the bytes support, I'd rather not add pathlike support. I'd prefer that every functions that seems to work with pathlike in shutil to be considered accidental and decide later whether to add pathlike support entirely or not. Semantically, it's a bit hard to determine whether cmd should be considered a path or not in the first place (e.g., shutil.which('ls') returns a path but this doesn't mean that the command itself should be a path).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.