Python subprocess on Linux: no such file or directory

Question:

I am trying to get the install location of conda. This works fine on Windows:

conda_path = subprocess.check_output('where anaconda').decode("utf-8").strip()

In a linux shell whereis conda works. os.system("whereis conda") returns zero.

However,

conda_path = subprocess.check_output('where anaconda').decode("utf-8").strip()

Fails with: FileNotFoundError: [Errno 2] No such file or directory: 'whereis conda': 'whereis conda'

Any suggestions?

Asked By: illan

||

Answers:

If you pass a single string, you need a shell to parse it into the command name and its arguments: subprocess.check_output('where anaconda', shell=True)....

Otherwise, you need to provide a list where the command name and each argument is a separate element. (One of the primary jobs of a shell is to do precisely that parsing.)

conda_path = subprocess.check_output(['where', 'anaconda']).decode("utf-8").strip()
Answered By: chepner
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.