This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: distutils: find_executable() fails if the PATH environment variable is not set
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Marcus.Smith, dstufft, miss-islington, ncoghlan, paul.moore, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2018-08-28 12:47 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 8968 closed vstinner, 2018-08-28 12:51
PR 9049 merged vstinner, 2018-09-03 21:26
PR 9056 merged miss-islington, 2018-09-04 09:01
PR 9057 merged miss-islington, 2018-09-04 09:01
PR 9058 merged vstinner, 2018-09-04 09:18
PR 9071 closed vstinner, 2018-09-05 14:44
Messages (10)
msg324245 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-08-28 12:47
Example:

$ env -i ./python -c 'import distutils.spawn; print(distutils.spawn.find_executable("true"))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/vstinner/prog/python/master/Lib/distutils/spawn.py", line 176, in find_executable
    path = os.environ['PATH']
  File "/home/vstinner/prog/python/master/Lib/os.py", line 672, in __getitem__
    raise KeyError(key) from None
KeyError: 'PATH'


Attached PR fixes the issue for the master branch.
msg324246 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-08-28 12:52
I'm not sure that it's worth it to modify Python 3.7 and older. distutils changes are now well welcomed (breaking setuptools or pip is unpopular :-)).
msg324380 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-08-30 12:32
find_executable() always searches executables from the current directory. This effectively equivalent having os.curdir at the start of path. shutil.which() does this only on Windows. This change LGTM, but it potentially can break user code, thus it may be not safe to make it in maintained versions.
msg324381 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-08-30 13:15
> find_executable() always searches executables from the current directory.

Oh! I didn't notice that!
msg324539 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-09-03 21:28
I missed the fact that find_executable() always lookup in the current directory, whereas shutil.which() doesn't. I abandoned my PR 8968 in favor of PR 9049 which only fix the case where PATH env var is not defined. This PR can easily be backported to all branches, whereas backporting PR 8968 to stable branches would be risky: changes to distutils are likely to break a random package on PyPI.
msg324566 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-09-04 09:01
New changeset 39487196c87e28128ea907a0d9b8a88ba53f68d5 by Victor Stinner in branch 'master':
bpo-34530: Fix distutils find_executable() (GH-9049)
https://github.com/python/cpython/commit/39487196c87e28128ea907a0d9b8a88ba53f68d5
msg324575 - (view) Author: miss-islington (miss-islington) Date: 2018-09-04 09:19
New changeset 7aa3eadca2a50ce651ce603d6100b05bb7106f1c by Miss Islington (bot) in branch '3.7':
bpo-34530: Fix distutils find_executable() (GH-9049)
https://github.com/python/cpython/commit/7aa3eadca2a50ce651ce603d6100b05bb7106f1c
msg324578 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-09-04 09:40
New changeset 7056ca880bf4ff428dfb2c4eee67919dc43ecd34 by Victor Stinner in branch '2.7':
bpo-34530: Fix distutils find_executable() (GH-9049) (GH-9058)
https://github.com/python/cpython/commit/7056ca880bf4ff428dfb2c4eee67919dc43ecd34
msg324636 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-09-05 14:45
New changeset e2c1657dff86decf1e232b66e766d2e51381109c by Victor Stinner (Miss Islington (bot)) in branch '3.6':
bpo-34530: Fix distutils find_executable() (GH-9049) (GH-9057)
https://github.com/python/cpython/commit/e2c1657dff86decf1e232b66e766d2e51381109c
msg324640 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-09-05 14:48
I chose to merge the simplest change:

-        path = os.environ['PATH']
+        path = os.environ.get('PATH', os.defpath)

And I added unit tests for find_executable(). The bug is now fixed.

I'm not longer interested to reuse shutil.which() in distutils.find_executable(), since find_executable() first checks if the executable is in the current directory.
History
Date User Action Args
2022-04-11 14:59:05adminsetgithub: 78711
2018-09-05 14:48:46vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg324640

stage: patch review -> resolved
2018-09-05 14:45:11vstinnersetmessages: + msg324636
2018-09-05 14:44:48vstinnersetpull_requests: + pull_request8531
2018-09-04 09:40:35vstinnersetmessages: + msg324578
2018-09-04 09:19:20miss-islingtonsetnosy: + miss-islington
messages: + msg324575
2018-09-04 09:18:07vstinnersetpull_requests: + pull_request8518
2018-09-04 09:01:30miss-islingtonsetpull_requests: + pull_request8517
2018-09-04 09:01:21miss-islingtonsetpull_requests: + pull_request8516
2018-09-04 09:01:13vstinnersetmessages: + msg324566
2018-09-03 21:28:47vstinnersetmessages: + msg324539
versions: + Python 2.7, Python 3.6, Python 3.7
2018-09-03 21:26:37vstinnersetpull_requests: + pull_request8511
2018-08-30 13:15:19vstinnersetmessages: + msg324381
2018-08-30 12:32:37serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg324380
2018-08-28 12:52:57vstinnersetnosy: + paul.moore, ncoghlan, dstufft, Marcus.Smith

messages: + msg324246
versions: - Python 2.7, Python 3.6, Python 3.7
2018-08-28 12:51:53vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request8449
2018-08-28 12:47:31vstinnercreate