Roberts Greibers Roberts Greibers - 2 months ago 32
Python Question

Can't execute casperjs through python on mac

I'm pretty new to Mac, sorry if this is something very simple.

I can run my javascript file through terminal using command:

casperjs myfile.js

However, I want to execute this command through python script.

this is what i've got:

pathBefore = os.getcwd()
cmd_output = subprocess.check_output(["casperjs click_email_confirm_link.js"], shell = True)
print cmd_output

which returns
/bin/sh: casperjs: command not found

As you can see, changing the working dir doesn't work.
I can't figure out how to make /bin/sh recognise casperjs, any help would be very appreciated


EDIT: this is how my code looks now

.bash_profile environment variable:

export PATH=$PATH:/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs

.profile environment variable:

export PHANTOMJS_EXECUTABLE="/usr/local/Cellar/phantomjs/2.1.1/bin/phantomjs"

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`

which returns error:

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


Solved my issue by typing these three lines in terminal: 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

and using python code:

commands = '''
cd ..
cd shared/scripts/javascript
/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'))

also following files were edited like this:

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 + source .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"