无法在 mac 上通过 python 执行 casperjs

Can't execute casperjs through python on mac

我是 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

哪个returns/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` 

哪个returns错误:

Fatal: [Errno 2] No such file or directory; did you install phantomjs?

通过在终端中输入以下三行解决了我的问题: 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'))

还对以下文件进行了如下编辑:

六.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"