为什么Python的shutil.which()不工作?

blu*_*ast 2 python shutil blast

我正在尝试查看是否可以从 NCBI 的 BLAST 中shutil.which()查找命令。blastn在我的终端上运行which blastn会产生/usr/local/bin/blastn. 但是,如果我这样做shutil.which("blastn"),它只会返回None。搜索 Python 工作正常,shutil.which("python")返回/usr/bin/python。为什么会发生这种情况?

pla*_*pus 6

这意味着 shell 中的环境与 Python 运行时中的环境具有不同的 PATH。可能的原因有很多,但通常会发生这种情况,因为您的某些内容.bashrc附加到了 PATH,这在 shell 中可以看到,但 Python 中看不到。

检查shell环境:

$ echo $PATH
Run Code Online (Sandbox Code Playgroud)

检查Python环境:

import os
print(os.environ["PATH"])
Run Code Online (Sandbox Code Playgroud)

您可能会发现 shell 的环境在 PATH 中具有 的位置blastn,而 Python 的环境中没有。