Existence check of external command in Python (like `which`)

When dealing with an external program in Python, I wanted to check if the external program existed before execution, so I checked it. You can also check for run-time errors, but before that, you just want to check that it's available. Or you can use it when you want to find out the path where the program is installed.

The external program is supposed to be executed by subprocess.Popen etc. I tried Python versions 3.5.1 and 2.7.11.

Probably the easiest way (Python 3.3 and above)

Use the which function of the shutil module. Returns the path to the command if the command is executable, or None if not found.

import shutil

print(shutil.which('ls'))  # > '/bin/ls'
print(shutil.which('ssss'))  # > None

Remedies for those forced to use Python less than 3.3

shutil.which is not backported and cannot be used in Python 2.7. I think it's better to move to 3.5 or higher immediately, but I think that some people can't do it, so I'll show you how to use it with old Python.

You can do the same with shutil.which with distutils.spawn.find_executable. http://stackoverflow.com/questions/5226958/which-equivalent-function-in-python

import distutils.spawn

print(distutils.span.find_executabl('ls'))  # > '/bin/ls'
print(distutils.span.find_executabl('ssss'))  # > None

If you don't know this, you can't find it ... This method can be used in 3 series, but distutils was treated as a minor and there was no documentation. http://docs.python.jp/3/library/distutils.html

The trap point is

-- shutil is singular, but distutils is plural --If you only use ʻimport distutils, the spawn module will not be imported, so you need to ʻimport distuils.spawn or from distutils import spawn

Etc.

Recommended Posts

Existence check of external command in Python (like `which`)
Execute external command in python
External command execution in Python
Check for external commands in python
Check the behavior of destructor in Python
Check the existence of the file with python
Check for the existence of BigQuery tables in Java
Check the asymptotic nature of the probability distribution in Python
Python --Check type of values
Equivalence of objects in Python
Check OpenSSL version of python 2.6
Implementation of quicksort in Python
Allow brew install of command line tools made in Python
Check the operation of Python for .NET in each environment
Check for memory leaks in Python
Pixel manipulation of images in Python
How to determine the existence of a selenium element in Python
Hit a command in Python (Windows)
Something like JS setTimeout in python
Division of timedelta in Python 2.7 series
How to check the memory size of a variable in Python
MySQL-automatic escape of parameters in python
Existence from the viewpoint of Python
Handling of JSON files in Python
In the python command python points to python3.8
Implementation of life game in Python
Waveform display of audio in Python
Display characters like AA in python
How to check the memory size of a dictionary in Python
Check and move directories in Python
Law of large numbers in python
Implementation of original sorting in Python
Reversible scrambling of integers in Python
Something like tail -f in Python
Self-implementation of something like% timeit, a magic command of jupyter that is convenient for speed measurement, in Python
Change the saturation and brightness of color specifications like # ff000 in python 2.5
Check the in-memory bytes of a floating point number float in Python
Modules of frequently used functions in Python (such as reading external files)
I made a program to check the size of a file in Python
Conversion of string <-> date (date, datetime) in Python
(Bad) practice of using this in Python
General Theory of Relativity in Python: Introduction
Find files like find on linux in Python
Output tree structure of files in Python
Display a list of alphabets in Python 3
Learn the design pattern "Command" in Python
Comparison of Japanese conversion module in Python3
Check and receive Serial port in Python (Port check)
Summary of various for statements in Python
Check if the URL exists in Python
The result of installing python in Anaconda
How to check opencv version in python
Gang of Four (GoF) Patterns in Python
Store RSS data in Zabbix (external check)
Check the path of the Python imported module
The basics of running NoxPlayer in Python
Find and check inverse matrix in Python
Bulk replacement of strings in Python arrays
Project Euler # 16 "Sum of Powers" in Python
Decompose command arguments in one line in Python
Do something like Redis transactions in Python