Linux: shutil.which fails to find executable in current working directory/cwd when PATH unset (subprocess works)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
On Linux, executables from the cwd are used when PATH is unset:
$ PATH= python -V
Python 3.13.7
subprocess.run also works in this case:
$ PATH= python -q
>>> from subprocess import run
... run(['python', '-V'])
...
Python 3.13.7
CompletedProcess(args=['python', '-V'], returncode=0)
>>>
But shutil.which returns None:
>>> from shutil import which
>>> which('python')
>>>
This is because when PATH is empty(""), the executable ("python") is passed to the kernel's execve directly, without resolving. execve then resolves the file relative to the cwd, I believe it's the case in both Linux and FreeBSD.
POSIX 2024 says this about the PATH environment variable:
If PATH is unset or is set to null, or if a path prefix in PATH contains a character ('%'), the path search is implementation-defined.
EDIT: PATH is empty, not unset
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the shutil.which entry point and reproduce the Linux case with PATH=, comparing it with subprocess.run(['python', '-V']). Determine the intended behavior for an empty PATH, then verify that shutil.which follows it and cover the case with a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100