subprocess.Popen():OSError:[Errno 8]python中的Exec格式错误?

subprocess.Popen():OSError:[Errno 8]python中的Exec格式错误?,python,linux,shell,Python,Linux,Shell,昨天,我编写并运行了一个python脚本,它使用subprocess.Popen(command.split())执行一个shell,其中command是字符串,它构成了.sh脚本及其参数。直到昨天,这个脚本一直运行良好。今天,我运行了相同的脚本,现在我不断地遇到这个错误 p=subprocess.Popen(shell_command.split()) File "/usr/lib/python2.7/subprocess.py", line 679, in __init__ errread,

昨天,我编写并运行了一个python
脚本
,它使用
subprocess.Popen(command.split())
执行一个
shell
,其中command是字符串,它构成了
.sh
脚本及其参数。直到昨天,这个脚本一直运行良好。今天,我运行了相同的脚本,现在我不断地遇到这个错误

p=subprocess.Popen(shell_command.split())
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
我知道以前也有人问过与这个问题相关的类似问题,但就我而言,我尝试了所有无法解决我的目的的事情。使用
shell=True
不起作用,因为我的shell脚本调用另一个shell脚本,在此之前必须设置一些环境才能运行该脚本。我被困在这里面了。我只需要重新启动我的系统一次。我正在使用ubuntu 12.04

编辑:

 import subprocess
 import os
 import sys

 arg1=sys.argv[1]
 arg2=sys.argve[2]

 shell_command = 'my_path/my_shell.sh ' + arg1 + ' '+ arg2
 P = subprocess.Popen(shell_command.split())
 P.wait()
  arg1=$1
  arg2=$2

  cd $TOP
  setup the environment and run shell script
  build the kernel ...
  execute shell command .....
my_shell.sh:

 import subprocess
 import os
 import sys

 arg1=sys.argv[1]
 arg2=sys.argve[2]

 shell_command = 'my_path/my_shell.sh ' + arg1 + ' '+ arg2
 P = subprocess.Popen(shell_command.split())
 P.wait()
  arg1=$1
  arg2=$2

  cd $TOP
  setup the environment and run shell script
  build the kernel ...
  execute shell command .....

错误消息表明外部程序不是有效的可执行文件。

正如@tripleee所说,执行脚本时出现问题。确保:

  • 将shell命令更改为“/my\u path/my\u script.sh”或“/bin/bash my\u path/my\u script.sh”。如有必要,说明环境变量
  • 两个脚本都有执行位集(chmod+x)
  • 这些文件存在于您认为它们存在的位置。(使用路径或验证环境)
  • 这些文件有内容
  • 尝试删除并重新键入第一行。我建议删除整行,并多次单击backspace,以防#前面有不可打印的字符

另请参见:

以下声明对我很有用


subprocess.Popen(['sh','my_script.sh'])

我通过将这一行放在被调用的shell脚本的顶部来解决这个问题:

#/bin/sh


这将保证系统在运行脚本时始终使用正确的解释器。

建议安装包binfmt support,以帮助系统更好地识别SCIPT。无论他们是否有shebang行,这都会有所帮助。

如果二进制文件不打算在您的系统上运行,也会发生这种情况

我在OSX上,但我运行的二进制文件并不适用于OSX,正如我在使用
文件路径/to/binary时看到的:

webui/bin/wkhtmltopdf: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.18, BuildID[sha1]=b6566a9e44c43a0eebf18d8c1dc6cb616801a77e, stripped

该错误是因为未按指定格式提供可执行文件,子流程无法执行该文件

例如:

shell_command1 = r"/path/to/executable/shell/file"

shell_command2 = r"./path/to/executable/shell/file"
subprocess.call(shell_command1)
subprocess.Popen(shell_command1)
将无法运行shell_command1,因为子流程需要一个可执行命令(mailx、ls、ping等)或可执行脚本(shell_command2),或者您需要指定这样的命令

subprocess.call(["sh", shell_command1])
subprocess.Popen(["sh", shell_command1])

但是,您也可以使用
os.system()
os.Popen()
导入shlex
并使用
shlex.split(shell_命令)
吗?是的,它给出了相同的错误:(你能发布你的shell_命令和其他(相关)代码吗?还有,脚本的shebang行?@Curtis Matttoon ping..你的脚本的第一行是什么?(#!/usr/bin/…)shell脚本是否确实存在于指定的路径中?使用绝对路径而不是“my_path/my_shell.sh”可能会更好。请确保python和shell脚本中都有shebang,并且都设置了执行位。如果仍然失败,则在第一行之前可能会有一些奇怪的空格或其他内容。onl这个解决方案的警告是“sh”应该在路径中,这通常不是这种情况。这对我来说更可取,因为我使用的是一个我不想更改的shell脚本。最好使用shell和脚本的完整路径,尤其是当脚本将从crontab执行时,这样可以避免您对$path的更改环境变量,也就是说:subprocess.Popen(['/bin/bash','//my_script.sh'])@nyuvec不确定您在哪个平台上;任何远程Unix-y都肯定在
路径中有
sh
。工作正常!谢谢!遇到类似问题([Errno 8]Exec格式错误)这是由于在设置文件上的执行位时缺少适当的shebang造成的。在
chmod-x
之后,问题得到了解决。这个答案让我走上了正确的轨道,所以谢谢:)相反,您也可以通过
subprocess.call([“sh”,./test.sh])
来指定在python中使用哪个解释器。显式优于隐式。