【发布时间】:2016-08-05 08:45:04
【问题描述】:
我对 Mac 还很陌生,如果这很简单,请见谅。
我可以使用命令通过终端运行我的 javascript 文件:
casperjs myfile.js
但是,我想通过 python 脚本执行这个命令。
这就是我所拥有的:
pathBefore = os.getcwd()
os.chdir("path/to/javascript/")
cmd_output = subprocess.check_output(["casperjs click_email_confirm_link.js"], shell = True)
os.chdir(pathBefore)
print cmd_output
返回/bin/sh: casperjs: command not found
如您所见,更改工作目录不起作用。 我不知道如何让 /bin/sh 识别 casperjs,任何帮助将不胜感激
谢谢
编辑:这就是我的代码现在的样子
.bash_profile 环境变量:
export PATH=$PATH:/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs
.profile 环境变量:
export PHANTOMJS_EXECUTABLE="/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs"
`try:
CASPER ='/usr/local/bin/casperjs'
SCRIPT = 'path/to/javascript/click_email_confirm_link.js'
params = CASPER + ' ' + SCRIPT
stdout_as_string = subprocess.check_output(params, shell=True)
print stdout_as_string
except CalledProcessError as e:
print e.output`
返回错误:
Fatal: [Errno 2] No such file or directory; did you install phantomjs?
【问题讨论】:
-
您是否尝试过指定 casperjs 可执行文件的完整路径?
-
将“casperjs”更改为“usr/local/Cellar/casperjs/1.1.3/bin/casperjs”,python 返回:“/bin/sh: ./usr/local/Cellar/casperjs /1.1.3/libexec/bin/casperjs:没有这样的文件或目录”
-
您似乎忘记了前导斜线。应该是
/usr/local/Cellar/casperjs/1.1.3/bin/casperjs -
另外,您确定路径正确吗?
which casperjs返回什么? -
which casperjs返回/usr/local/bin/casperjs现在我的代码如下所示:cmd_output = subprocess.check_output(["/usr/local/bin/casperjs click_email_confirm_link.js"], shell = True)但现在我收到错误消息:CalledProcessError: Command '['/usr/local/bin/casperjs click_email_confirm_link.js']' returned non-zero exit status 1