Search code examples
pythonshutilblast

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


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?


Solution

  • 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.