summaryrefslogtreecommitdiffstats
path: root/Doc/library/shutil.rst
diff options
context:
space:
mode:
authorCharles Machalow <csm10495@gmail.com>2023-04-04 22:24:13 (GMT)
committerGitHub <noreply@github.com>2023-04-04 22:24:13 (GMT)
commit935aa452359ac3f79febefcdb4387b962cf528af (patch)
tree31700f3b8d0dfb85e7267ffcfd5330237df212f1 /Doc/library/shutil.rst
parentf184abbdc9ac3a5656de5f606faf505aa42ff391 (diff)
downloadcpython-935aa452359ac3f79febefcdb4387b962cf528af.zip
cpython-935aa452359ac3f79febefcdb4387b962cf528af.tar.gz
cpython-935aa452359ac3f79febefcdb4387b962cf528af.tar.bz2
GH-75586: Make shutil.which() on Windows more consistent with the OS (GH-103179)
Diffstat (limited to 'Doc/library/shutil.rst')
-rw-r--r--Doc/library/shutil.rst34
1 files changed, 27 insertions, 7 deletions
diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst
index acba662..373cc7d 100644
--- a/Doc/library/shutil.rst
+++ b/Doc/library/shutil.rst
@@ -433,23 +433,43 @@ Directory and files operations
When no *path* is specified, the results of :func:`os.environ` are used,
returning either the "PATH" value or a fallback of :attr:`os.defpath`.
- On Windows, the current directory is always prepended to the *path* whether
- or not you use the default or provide your own, which is the behavior the
- command shell uses when finding executables. Additionally, when finding the
- *cmd* in the *path*, the ``PATHEXT`` environment variable is checked. For
- example, if you call ``shutil.which("python")``, :func:`which` will search
- ``PATHEXT`` to know that it should look for ``python.exe`` within the *path*
- directories. For example, on Windows::
+ On Windows, the current directory is prepended to the *path* if *mode* does
+ not include ``os.X_OK``. When the *mode* does include ``os.X_OK``, the
+ Windows API ``NeedCurrentDirectoryForExePathW`` will be consulted to
+ determine if the current directory should be prepended to *path*. To avoid
+ consulting the current working directory for executables: set the environment
+ variable ``NoDefaultCurrentDirectoryInExePath``.
+
+ Also on Windows, the ``PATHEXT`` variable is used to resolve commands
+ that may not already include an extension. For example, if you call
+ ``shutil.which("python")``, :func:`which` will search ``PATHEXT``
+ to know that it should look for ``python.exe`` within the *path*
+ directories. For example, on Windows::
>>> shutil.which("python")
'C:\\Python33\\python.EXE'
+ This is also applied when *cmd* is a path that contains a directory
+ component::
+
+ >> shutil.which("C:\\Python33\\python")
+ 'C:\\Python33\\python.EXE'
+
.. versionadded:: 3.3
.. versionchanged:: 3.8
The :class:`bytes` type is now accepted. If *cmd* type is
:class:`bytes`, the result type is also :class:`bytes`.
+ .. versionchanged:: 3.12
+ On Windows, the current directory is no longer prepended to the search
+ path if *mode* includes ``os.X_OK`` and WinAPI
+ ``NeedCurrentDirectoryForExePathW(cmd)`` is false, else the current
+ directory is prepended even if it is already in the search path;
+ ``PATHEXT`` is used now even when *cmd* includes a directory component
+ or ends with an extension that is in ``PATHEXT``; and filenames that
+ have no extension can now be found.
+
.. exception:: Error
This exception collects exceptions that are raised during a multi-file