Skip to content Skip to sidebar Skip to footer

How To Correctly Run Python Script From PHP

I have a python script that I would like to run from PHP. This is my PHP script: $data = array('as', 'df', 'gh'); // Execute the python script with the JSON data $result = shell_e

Solution 1:

On my machine, the code works perfectly fine and displays:

array(1) {
  'status' =>
  string(4) "Yes!"
}

On the other hand, you may make a few changes to diagnose the issue on your machine.

  1. Check the default version of Python. You can do this by running python from the terminal. If you see something like:

    Python 2.7.6 (default, Mar 22 2014, 22:59:56)
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

    you're fine. If you see that you are running Python 3, this could be an issue, since your Python script is written for Python 2. So:

    Python 3.4.0 (default, Apr 11 2014, 13:05:11)
    [...]
    

    should be a clue.

  2. Again from the terminal, run python myScript.py "[\"as\",\"df\",\"gh\"]". What do you see?

    {"status": "Yes!"}
    

    is cool. A different response indicates that the issue is probably with your Python script.

  3. Check permissions. How do you run your PHP script? Do you have access to /path/to/? What about /path/to/myScript.php?

    Replace your PHP code by:

    <?php
    echo file_get_contents("/path/to/myScript.php");
    ?>
    

    Do you get the actual contents?

  4. Now let's add a few debugging helpers in your PHP code. Since I imagine that you are not using a debugger, the simplest way is to print debug statements. This is OK for 10-LOC scripts, but if you need to deal with larger applications, invest your time in learning how to use PHP debuggers and how do use logging.

    Here's the result:

    /path/to/demo.php

    <?php
    $data = array('as', 'df', 'gh');
    
    $pythonScript = "/path/to/myScript.py";
    $cmd = array("python", $pythonScript, escapeshellarg(json_encode($data)));
    $cmdText = implode(' ', $cmd);
    
    echo "Running command: " . $cmdText . "\n";
    $result = shell_exec($cmdText);
    
    echo "Got the following result:\n";
    echo $result;
    
    $resultData = json_decode($result, true);
    
    echo "The result was transformed into:\n";
    var_dump($resultData);
    ?>
    

    /path/to/myScript.py

    import sys, json
    
    try:
        data = json.loads(sys.argv[1])
        print json.dumps({'status': 'Yes!'})
    except Exception as e:
        print str(e)
    

    Now run the script:

    cd /path/to
    php -f demo.php
    

    This is what I get:

    Running command: python /path/to/myScript.py '["as","df","gh"]'
    Got the following result:
    {"status": "Yes!"}
    The result was transformed into:
    array(1) {
      'status' =>
      string(4) "Yes!"
    }
    

    yours should be different and contain a hint about what is happening.


Solution 2:

I got it to work by adding quotes around the argument! Like so: <?php $data = array('as', 'df', 'gh'); $temp = json_encode($data); echo shell_exec('python myScript.py ' . "'" . $temp . "'"); ?>


Post a Comment for "How To Correctly Run Python Script From PHP"