Execute python in PHP and get response as i get in terminal
Question: i am new to PHP Programming. I need to execute a python code from php. My
Python code contains some external modules. The Php code is

    
    
    $op=shell_exec('python mgs.py');
    echo $op;
    
    ?>
    

This is my intended code and i need to get the exact output as i get in
terminal(if errors ,that too)

python code

    
    
    import mechanize
    def mgs():
        a=0
        flag=0
        browser = mechanize.Browser(factory=mechanize.RobustFactory())
        browser.set_handle_robots(False)
        browser.open("http://14.139.185.88/cbcsshrCamp/index.php?module=public&page=result") 
        browser.select_form(nr=0)
        control=browser.find_control('exam_id')
        print control
        control.value=['203']
        browser.form["prn"]="130021069679"
        browser.submit()
        html = browser.response().readlines()               
        for i in range(0,len(html)):
           if 'Failed' in html[i]:                 
              flag=1
        if flag==1:
           print "Fail"
        else:
           print "Pass"
    
    
    
    mgs()
    

. I am not getting any output on browser window, but when i tried to execute
the PHP code in terminal, its fine.. ie, ( php index.php)

My OS> Ubuntu Apache 2


Answer: Most likely, `python` is not in the path of the user that executes your php
script.

Try this instead:

    
    
    <?php
        $op = shell_exec('/usr/bin/python3 mgs.py');
        echo $op;
    ?>
    

Of course, change the interpreter to the correct path if this isn't the
correct one.

