I need to setup environment with the path to a binary. In the shell, I can use which
to find the path. Is there an equivalent in python?
This is my code.
cmd = ["which","abc"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
res = p.stdout.readlines()
if len(res) == 0: return False
return True
PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.
type Command The type command can not only show the path of a Linux command, but it can also tell if the target is built-in, a function, an alias, or an external executable.
Go to "My computer -> properties -> advanced -> environment variables -> Path" and edit path by adding .exe 's directory into path.
There is distutils.spawn.find_executable()
.
I know this is an older question, but if you happen to be using Python 3.3+ you can use shutil.which(cmd)
. You can find the documentation here. It has the advantage of being in the standard library.
An example would be like so:
>>> import shutil
>>> shutil.which("bash")
'/usr/bin/bash'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With