Skip to main content

You can use shutil.which to check if an executable is in your PATH

  • Tagged with python
  • Posted

This is useful for checking if something’s installed.

shutil.which() returns the path to the executable, or None if you don’t have it installed:

>>> import shutil
>>> shutil.which('python3')
'/opt/homebrew/bin/python3'
>>> shutil.which('python2.7')
>>> shutil.which('python2.7') is None
True

I used to accomplish something similar with subprocess, but this is a bit simpler and clearer.