简体   繁体   English

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

[英]Executing a python script in Ubuntu defaults to shell and not bash

I am running a python script in Ubuntu which relies on packages being installed. 我在Ubuntu中运行python脚本,该脚本依赖于要安装的软件包。 These are all installed as i can invoke them, but the script cannot. 这些都已安装,因为我可以调用它们,但是脚本不能。

I think this is because the script is using shell(sh) and not the bash version.i get this error message out: 我认为这是因为脚本使用的是shell(sh)而不是bash版本。我收到以下错误消息:

sh: genomeCoverageBed: command not found

but can invoke genomeCoverageBed manually in Bash: 但可以在Bash中手动调用基因组覆盖:

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.

This part of my Stackoverflow question is misleading and wrong!: 我的Stackoverflow问题的这一部分是误导和错误的!:

I have added this shebang to the start of my python script: 我已经将此shebang添加到我的python脚本的开头:

#!/bin/bash #!/ bin / bash

But this has not affected the outcome, 但这并没有影响结果,

Taking this out of my script still causes the same error messages Here is part of the script: 从脚本中删除该脚本仍会导致相同的错误消息,这是脚本的一部分:

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)

Here is what happens when i run this script: 这是我运行此脚本时发生的情况:

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

Do you have any suggestions about what i can do, or even if i am on the right track? 您是否对我的工作有任何建议,或者即使我处在正确的轨道上? thanks for your help! 谢谢你的帮助!

You are using the wrong shebang 您使用的是错误的shebang

#!/bin/bash

When a linux shell is asked to run a text file (eg, ./myprogram.py ), it looks at the shebang to figure out which program should be used to interpret the text. 当要求Linux shell运行文本文件(例如./myprogram.py )时,它将查看shebang以确定应使用哪个程序来解释文本。 In your case you asked the shell to run /bin/bash - another shell and it tried to interpret the python script as a shell script. 在您的情况下,您要求外壳程序运行/bin/bash另一个外壳程序,它试图将python脚本解释为外壳程序脚本。

You can write the shebang differently 你可以用不同的方式写shebang

#!/usr/bin/env python3

This tells the shell to run /usr/bin/env which will search the PATH for a thing called pythnon3 . 这告诉外壳程序运行/usr/bin/env ,它将在PATH中搜索名为pythnon3 Now, you run the python setup for your current environment. 现在,您为当前环境运行python安装程序。 By default its the system python but if you run in a python virtual environment, it runs that python. 默认情况下,它是系统python,但是如果您在python虚拟环境中运行,它将运行 python。

The shebang is only used on linuxy systems and is only used when you make a script executable and run it directly. shebang仅在linuxy系统上使用,仅在使脚本可执行并直接运行它时使用。 If running on windows (outside of cygwin or other unix-like shell) or running python directly ( python /path/to/myscript.py ) then its not used. 如果在Windows(cygwin或其他类似Unix的外壳程序之外)上运行或直接运行python( python /path/to/myscript.py ),则不使用它。

Your shebang is calling a subshell, as @tdelaney mentioned in the comments. 正如评论中提到的@tdelaney一样,您的shebang正在调用subshel​​l。 You need to use something like the following to use python as the program executing the file: 您需要使用以下类似内容,才能将python用作执行该文件的程序:

#! /usr/bin/env python

Ubuntu won't run the python file in python if you put #!/bin/bash , that will open in bash. 如果您输入#!/ bin / bash,Ubuntu将不会在python中运行python文件,该文件将在bash中打开。 Try putting #!/usr/bin/python or #!/usr/bin/env python 尝试放入#!/ usr / bin / python或#!/ usr / bin / env python

I followed this post: 我关注了这篇文章:

https://unix.stackexchange.com/questions/83191/how-to-make-sudo-preserve-path https://unix.stackexchange.com/questions/83191/how-to-make-sudo-preserve-path

and added: 并添加:

sudo env "PATH=$PATH" sudo env“ PATH = $ PATH”

to the start of my command and this solved my issue! 到我的命令开始,这解决了我的问题!

Thanks to all who helped! 感谢所有的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM