Why is Python's shutil.which() not working?

Question:

I’m trying to see if shutil.which() works for finding the blastn command from NCBI’s BLAST. Running which blastn on my terminal results in /usr/local/bin/blastn. However, if I do shutil.which("blastn"), it simply returns None. Searching for Python works fine, as shutil.which("python") returns /usr/bin/python. Why is this happening?

Asked By: blueblast

||

Answers:

This means that the environment in your shell has a different PATH than the environment within the Python runtime. There are many possibly causes, but it commonly happens because something in your .bashrc appends to PATH, which will be seen in shell but not by Python.

To check the environment in shell:

$ echo $PATH

To check the environment in Python:

import os
print(os.environ["PATH"])

You’ll likely find that the shell’s environment has the location of blastn in PATH and Python’s doesn’t.

Answered By: platypus

I had this problem and it was caused by having them equal, but the path containing the command I was searching was referred in the PATH variable with the home notation : ~/my/path rather than /home/username/my/path. Fixing this, the ‘which’ command worked correctly.

Answered By: crillion
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.