python - 在 Ubuntu 中执行 python 脚本默认为 shell 而不是 bash

标签 python linux bash shell ubuntu

我在 Ubuntu 中运行一个 python 脚本,它依赖于正在安装的包。这些都已安装,因为我可以调用它们,但脚本不能。

我认为这是因为脚本使用的是 shell(sh) 而不是 bash 版本。我得到了这条错误消息:

sh: genomeCoverageBed: command not found

但可以在 Bash 中手动调用 genomeCoverageBed:

ubuntu@fat:~/jonathan/scripty$ genomeCoverageBed 


Tool:    bedtools genomecov (aka genomeCoverageBed)
Version: v2.26.0

Summary: Compute the coverage of a feature file among a genome.

我的 Stackoverflow 问题的这一部分具有误导性和错误性!

I have added this shebang to the start of my python script:

#!/bin/bash

But this has not affected the outcome,

从我的脚本中删除它仍然会导致相同的错误消息 这是脚本的一部分:

for filename in os.listdir(folder):
if filename.endswith(".sorted.bam"):
    namefile = filename.replace(".sorted.bam", "_ncoverage.txt")
    folder_name = filename.replace(".sorted.bam", "")
    os.system("mkdir " + folder_name)
    os.system("coverageBed -s -d -a bam " + folder + filename + " -b " + gff_file + " > " + folder_name + "/" + namefile)

以下是我运行此脚本时发生的情况:

ubuntu@fat:~/jonathan/script$ sudo python2.7 script1.py ./Folder/ ./Folder/ref.fasta genbank.gff
sh: coverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: coverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: coverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: coverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: coverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: coverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: coverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: coverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: coverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found
sh: genomeCoverageBed: command not found

您对我能做什么有什么建议,或者即使我走在正确的轨道上? 感谢您的帮助!

最佳答案

你用错了 shebang

#!/bin/bash

当 linux shell 被要求运行一个文本文件时(例如,./myprogram.py),它会查看 shebang 以确定应该使用哪个程序来解释文本。在您的情况下,您要求 shell 运行 /bin/bash - 另一个 shell,它试图将 python 脚本解释为 shell 脚本。

你可以用不同的方式写 shebang

#!/usr/bin/env python3

这会告诉 shell 运行 /usr/bin/env,它将在 PATH 中搜索名为 python3 的东西。现在,您为当前环境运行 python 安装程序。默认情况下它是系统 python,但如果您在 python 虚拟环境中运行,它会运行 that python。

shebang 仅在 linuxy 系统上使用,并且仅在您使脚本可执行并直接运行时使用。如果在 Windows 上运行(在 cygwin 或其他类似 unix 的 shell 之外)或直接运行 python (python/path/to/myscript.py),那么它不会被使用。

关于python - 在 Ubuntu 中执行 python 脚本默认为 shell 而不是 bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40468490/

相关文章:

bash - 在 rsh 中转义管道字符

相同 gzip 文件的 Python md5 哈希值不一致

python - 为数组 A 中的唯一值获取数组 B 中的 NumPy 数组索引,对于两个数组中存在的值,与数组 A 对齐

python - Qt TableView 索引在带/不带标题的不同行为

python - 如何使用 python 在 imaplib 中按时间对电子邮件进行排序

无需加入域即可通过 Active Directory 进行 Linux 身份验证

linux -/etc/rc.local在启动时未运行命令

linux - linux bash 中的 $RANDOM 在启动时总是相同的

bash 获取第一个字段 awk grep

linux - 跳过前 N 行并打印文件的其余部分,假设文件中间存在​​文件损坏