This Python tutorial explains shutil.which() Function: Usage and Examples. Follow the example, inspect the output, and practise an input change so you understand the behavior rather than simply copying the syntax.
What This Feature Does
Find an executable by name using shutil.which().
shutil.which(command) searches the process executable lookup path (usually PATH) and returns a resolved command path or None. On Windows it also accounts for applicable executable extension rules.
When to Use It
This technique belongs to file copying, temporary files and matching. It is useful when the input, operation and expected outcome are known. Start with a small controlled example before integrating it into a larger workflow.
Step-by-Step Python Example
Read the code once before running it. Notice which values are inputs, which function or method performs the work, and whether the result is returned or stored by changing an existing object.
import shutil
python_executable = shutil.which("python") or shutil.which("python3")
print(python_executable is None or isinstance(python_executable, str))
Understanding the Code
- First, the imports make shutil available. They provide the filesystem, data or standard-library functions used later.
- Find the call shutil.which("python"). Its parameters determine the specific operation demonstrated in the title.
- Its purpose is to find an executable by name using shutil.which(). Compare this description against the result printed by the program.
- The print() expressions display a checkable outcome. Print values and exception names are not substitutes for the program result itself.
Expected Output
True
The output is the observable result of the sample operation. Compare its value and type with the explanation, not just whether the program runs.
Important Details and Edge Cases
sys.executable is the direct way to identify the current Python interpreter.
shutil.which() may return None when an executable cannot be found; check before attempting to run it.
Second Example: A Useful Variation
import shutil
import sys
executable_path = shutil.which('python') or shutil.which('python3')
print(executable_path is None or isinstance(executable_path, str))
print(isinstance(sys.executable, str))
shutil.which() looks through PATH for a matching executable, while sys.executable identifies the interpreter running the current script.
Variation Output
True
True
Practice: Try It Yourself
Search for a tool such as git and handle a None result.
Begin by copying the example into a separate practice file. Change one input at a time, run it again, and describe why the result changed. If the exercise writes or deletes files, keep it inside a temporary test folder.
Official Python Documentation
For the complete reference, see the Python standard library documentation.
Common Beginner Mistakes
- Assuming which() downloads or installs missing software automatically.
- Executing a program from an untrusted PATH without considering command shadowing.
- shutil.which() may return None when an executable cannot be found; check before attempting to run it.
Key Takeaways
- Find an executable by name using shutil.which().
- shutil.which(command) searches the process executable lookup path (usually PATH) and returns a resolved command path or None. On Windows it also accounts for applicable executable extension rules.
- Avoid this common pitfall: Assuming which() downloads or installs missing software automatically.