Repository: python-pillow/Pillow

Issue:
setup.py build fails without PATH
### What did you do?
```
$ env - python3.9 setup.py build
```
### What did you expect to happen?

The build succeeds.

### What actually happened?
```pytb
$ env - python3.9 setup.py build
... (skipped many lines) ...
writing manifest file 'src/Pillow.egg-info/SOURCES.txt'
running build_ext
Traceback (most recent call last):
  File "$(BUILD_DIR)/setup.py", line 993, in <module>
    setup(
  File "/usr/lib/python3.9/vendor-packages/setuptools/__init__.py", line 108, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib/python3.9/vendor-packages/setuptools/_distutils/core.py", line 185, in setup
    return run_commands(dist)
  File "/usr/lib/python3.9/vendor-packages/setuptools/_distutils/core.py", line 201, in run_commands
    dist.run_commands()
  File "/usr/lib/python3.9/vendor-packages/setuptools/_distutils/dist.py", line 969, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3.9/vendor-packages/setuptools/dist.py", line 1213, in run_command
    super().run_command(command)
  File "/usr/lib/python3.9/vendor-packages/setuptools/_distutils/dist.py", line 988, in run_command
    cmd_obj.run()
  File "/usr/lib/python3.9/vendor-packages/setuptools/_distutils/command/build.py", line 132, in run
    self.run_command(cmd_name)
  File "/usr/lib/python3.9/vendor-packages/setuptools/_distutils/cmd.py", line 318, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.9/vendor-packages/setuptools/dist.py", line 1213, in run_command
    super().run_command(command)
  File "/usr/lib/python3.9/vendor-packages/setuptools/_distutils/dist.py", line 988, in run_command
    cmd_obj.run()
  File "/usr/lib/python3.9/vendor-packages/setuptools/command/build_ext.py", line 84, in run
    _build_ext.run(self)
  File "/usr/lib/python3.9/vendor-packages/Cython/Distutils/old_build_ext.py", line 186, in run
    _build_ext.build_ext.run(self)
  File "/usr/lib/python3.9/vendor-packages/setuptools/_distutils/command/build_ext.py", line 346, in run
    self.build_extensions()
  File "$(BUILD_DIR)/setup.py", line 438, in build_extensions
    if _cmd_exists(os.environ.get("PKG_CONFIG", "pkg-config")):
  File "$(BUILD_DIR)/setup.py", line 248, in _cmd_exists
    for path in os.environ["PATH"].split(os.pathsep)
  File "/usr/lib/python3.9/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'PATH'
$
```
### What are your OS, Python and Pillow versions?

* OS: OpenIndiana
* Python: 3.9.16
* Pillow: 9.4.0


Discussion:
I'm not sure that we can reasonably expect a build to work if there's no `PATH`? We have to be able to find compilers, linkers, and pkg-config. 

Are there any binary packages that build with this configuration?

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -242,7 +242,9 @@ def _find_include_dir(self, dirname, include):
             return subdir
 
 
-def _cmd_exists(cmd):
+def _cmd_exists(cmd: str) -> bool:
+    if "PATH" not in os.environ:
+        return False
     return any(
         os.access(os.path.join(path, cmd), os.X_OK)
         for path in os.environ["PATH"].split(os.pathsep)


Test patch: