python / python/cpython

Linux: shutil.which fails to find executable in current working directory/cwd when PATH unset (subprocess works)

Aberta
#141,041 2 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

stdlib type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece pelo ponto de entrada shutil.which e reproduza o caso do Linux com PATH=, comparando-o com subprocess.run(['python', '-V']). Determine o comportamento pretendido para um PATH vazio, depois verifique se shutil.which o segue e cubra o caso com um teste de regressão.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
operating-systems
Tipo de issue
Bug
Dificuldade
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
42/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.