whichcraft
whichcraft is a Python package that provides cross-platform and cross-Python `shutil.which` functionality. It serves as a shim for the `shutil.which` function, designed to work consistently across Python 2.7 and various Python 3 versions, including environments like Windows where system commands might behave differently. Its primary goal is to offer a reliable way to locate executables on the system PATH, similar to the Unix `which` command.
Common errors
-
which('my_command') returns None on Windows when I expect a path, but works on Linux.cause The command you are searching for (`my_command`) might be an internal shell command (e.g., `date`, `dir`) on Windows, not a standalone executable file that exists on the system's PATH. `whichcraft.which` primarily finds external executable files.fixVerify if `my_command` is an actual executable file (`.exe`, `.bat`, `.cmd`) on Windows and if its directory is included in the system's PATH environment variable. If it's a shell built-in, `whichcraft` cannot locate it. For such commands, consider using `subprocess` with `shell=True` (with caution) or Python's native functionalities. -
ImportError: cannot import name 'which' from 'whichcraft' (or similar import errors)
cause This typically means the `whichcraft` package is either not installed, or there's a typo in the import statement, or there's a naming conflict with another module or file named `whichcraft.py` in your project's directory or Python path.fixFirst, ensure the library is installed: `pip install whichcraft`. Second, double-check the import statement: `from whichcraft import which`. Third, if you have a file named `whichcraft.py` in your current working directory or a directory on your `PYTHONPATH`, Python might be trying to import from your local file instead of the installed package. Rename your local file or ensure it's not shadowing the package.
Warnings
- gotcha The behavior of `whichcraft.which('date')` (or similar internal system commands) differs between Windows and Unix-like systems. On Unix-like systems (Linux, macOS), `date` is typically a standalone executable returning its path. On Windows, `date` is often an internal shell command (cmd.exe built-in) and thus `whichcraft.which` will return `None`, as it only searches for external executables on the PATH.
- deprecated For Python 3.3 and newer, the functionality provided by `whichcraft` is natively available in the standard library as `shutil.which`. While `whichcraft` still works, it may not be necessary for projects targeting only modern Python 3 versions, as its primary value was cross-version (Python 2.7 and Python 3.x) compatibility and Windows support for older Python 3 versions.
Install
-
pip install whichcraft
Imports
- which
from whichcraft import which
Quickstart
from whichcraft import which
# Find the path to a common executable
python_path = which('python')
print(f"Python executable found at: {python_path}")
# Try to find a command that might not exist
non_existent_command = which('nonexistent_command_123')
print(f"Non-existent command found at: {non_existent_command}")
# Example of a command that might behave differently across OS (e.g., 'date')
date_command = which('date')
print(f"'date' command found at: {date_command}")