Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the path for an executable?

Tags:

python

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
like image 921
prosseek Avatar asked Mar 08 '11 00:03

prosseek


People also ask

What is an executable path?

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.

How can you know the path of an executable file command in Linux?

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.

How do I create an executable path in Windows?

Go to "My computer -> properties -> advanced -> environment variables -> Path" and edit path by adding .exe 's directory into path.


2 Answers

There is distutils.spawn.find_executable().

like image 65
zonksoft Avatar answered Oct 12 '22 01:10

zonksoft


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'
like image 91
iLoveTux Avatar answered Oct 12 '22 00:10

iLoveTux