【问题标题】:Unable to run shell script from the Pydev environment in Eclipse无法从 Eclipse 中的 Pydev 环境运行 shell 脚本
【发布时间】:2014-12-08 07:56:06
【问题描述】:

我正在使用 Centos 7.0 并在 Pydev 环境中安装了 Eclipse Kepler。我想使用子进程通过 Python 运行一个简单的 c shell 脚本,如下所示:

import subprocess
subprocess.call(["./test1.csh"])

这个 c shell 脚本在终端中执行,如果我运行像“ls”或“”pwd 这样的命令,那么我会得到正确的输出,例如

subprocess.call(["ls"]) # give me the list of all files
subprocess.call(["pwd"]) # gives me the location of current directory.

但是当我运行 subprocess.call(["./test1.csh"]) 时,我得到以下错误:

Traceback (most recent call last):
File "/home/nishant/workspace/codec_implement/src/NTTool/raw2waveconvert.py", line 8, in <module>
    subprocess.call(["./test1.csh"])
File "/usr/lib64/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied

我哪里错了?请推荐

【问题讨论】:

  • 您是否为脚本设置了可执行位? chmod +x test1.csh?第一行是否有正确的shebang
  • 正如我在问题中提到的,它在终端中运行,即 c shell 终端,并且文件是可执行的。
  • @nprak: subprocess.call() 默认情况下运行任何shell。看到test1.csh 顶部的#!/bin/sh 了吗?

标签: python linux eclipse shell subprocess


【解决方案1】:

确保文件test1.csh 是可执行的。正如 Lukas Graf 评论的那样,还要检查第一行中的 shebang (#!...)。

为了确认,在通过 Python 运行它之前,在 shell 中运行它。

$ ls -l test1.csh
...
$ ./test1.csh

当前工作目录将与您在终端中运行时不同。指定 shell 脚本的完整路径。或者更改 PyDev 中的工作目录配置。

更新

添加 shell 可执行文件:

import subprocess
subprocess.call(["csh", "./test1.csh"])

【讨论】:

  • 正如我在问题中提到的,它在终端中运行,即 c shell 终端,并且文件是可执行的。
  • @user2658684,如果指定完整路径会怎样?
  • @falsetrue,即使我指定了完整路径,它也会给出同样的错误,只是提到 shell 脚本正在 c shell 终端中运行。
  • @user2658684,subprocess.call(["csh", "./test1.csh"]) 怎么样?
  • @falsetrue 非常感谢,它成功了.. 非常感谢。我怎么能赞成你的回答,因为它对我有帮助。
猜你喜欢
  • 2015-12-26
  • 2019-07-25
  • 2016-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多