我是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?
通过在终端中输入以下三行来解决我的问题: 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"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句