0

So i have seen other questions like this, but what i have seen isn't working and i'm not sure if its a laravel 5 thing or not.

i have a python file - jobs.py:

import yaml
import requests
import os
import inspect
import sys
import datetime
import random
import urllib2
import cPickle 



print "hey"
print sys.argv[1]
print str(sys.argv)

In the normal linux command line if I type in the following:

python /home/mscarpa/PhpstormProjects/quasar-node/quasar-node/quasar-node/src/lib/jobs.py arg1 arg2

I will get this response as expected:

hey arg1 ['/home/mscarpa/PhpstormProjects/quasar-node/quasar-node/quasar-node/src/lib/jobs.py', 'arg1', 'arg2']

But when i try to do the same thing in my php file:

public function getJobs()
{
    $jobs = \App\Job::all();
    $var1 = "arg1";
    $command = exec('python /home/mscarpa/PhpstormProjects/quasar-node/quasar-node/quasar-node/src/lib/jobs.py  $var1');
    return view('jobs/jobs_table', compact('jobs'));
}

And start using xdebug to see what is going on it says that its connected when its steps over $command but $command only returns "hey". I am trying to pass $var1='arg1' to the python script.

Thanks

2
  • 2
    $var1 will not be interpreted unless you use double quotes. (I mean "python /home/mscarpa/PhpstormProjects/quasar-node/quasar-node/quasar-node/src/lib/jobs.py $var1") Commented Apr 10, 2015 at 18:11
  • A. Mazing. I was panicking. Thanks so much!!! Commented Apr 10, 2015 at 18:23

1 Answer 1

1

Python is not receiving the contents of $var1, because when using a string with single quotes, as in

$command = exec('python /home/.../src/lib/jobs.py  $var1');

any contained variables will not be parsed. You must use double quotes like this:

$command = exec("python /home/.../src/lib/jobs.py  $var1");

You can read about variable parsing here.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure where $var1 is coming from but if it's user input, be sure to escape it using escapeshellarg()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.