【问题标题】:Can't execute casperjs through python on mac无法在mac上通过python执行casperjs
【发布时间】: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

标签: python macos casperjs


【解决方案1】:

通过在终端中键入以下三行来解决我的问题: sudo ln -s /path/to/bin/phantomjs /usr/local/share/phantomjs sudo ln -s /path/to/bin/phantomjs /usr/local/bin/phantomjs sudo ln -s /path/to/bin/phantomjs /usr/bin/phantomjs

并使用python代码:

commands = '''
pwd
cd ..
pwd
cd shared/scripts/javascript
pwd
/usr/local/Cellar/casperjs/1.1.3/libexec/bin/casperjs click_email_confirm_link.js
''' 

process = subprocess.Popen('/bin/bash', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = process.communicate(commands.encode('utf-8'))
print(out.decode('utf-8'))

以下文件也被这样编辑:

vi .bash_profile export PHANTOMJS_EXECUTABLE=/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs export PATH=$PATH:/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs

vi .bashrc + 源代码 .bashrc export PHANTOMJS_EXECUTABLE=/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs export PATH="/usr/local/Cellar/phantomjs/2.1.1/bin:$PATH"

vi .profile export PHANTOMJS_EXECUTABLE=/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs export PATH="$PATH:/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs"

【讨论】:

    猜你喜欢
    • 2017-01-23
    • 2021-10-23
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多