0

I have tried implementing the steps mentioned in this post: Previous Post

However, there is not output. it appears that Python script is not executing at all. Just to mention here that I have worked around with Hosting provider to enable popen, pclose, proc_open, and proc_close.

Here is the code in the wordpress plugin:

<?php # -*- coding: utf-8 -*-
/* Plugin Name: gm-python */

add_shortcode( 'python', 'embed_python' );

function embed_python( $attributes )
{
    $data = shortcode_atts(
        [
            'file' => 'hello.py'
        ],
        $attributes
    );


    if($_SERVER['REQUEST_METHOD'] === 'GET'){

        $handle = popen( __DIR__ . '/' . $data['file'] . '2>&1', 'r' );
        $read = '';

        if($read){

            while (!feof($handle))
            {
                $read.= fread($handle, 2096);
            }
        }
    }
    pclose($handle);

    return $read;
}

here is what is in the python script:

#!/usr/bin/env python
print('Hello World')

I have installed python outside of the websites as well. enter image description here

Can anybody help to find the correct way to configure the path for python execution through wordpress plugin?

  • Your if($read) evaluates to false, because you have set it to an empty string. So the while statement isn't executed. – fuxia Dec 21 '19 at 19:16
  • @fuxia I have modified the code as it is in the original post. however, it is still not executing. I am wondering if I am mentioning the shebang path correctly in the python file. In my case python path is not usr/bin/env python rather it is home/(my main account for hosting)/virtualenv/analysis/3.7/bin python. – Syed Dec 22 '19 at 18:14
  • I have been struggling to make the simple hello code work out but no luck untill I added the encoding instruction # -- coding: UTF-8 -- This instruction in hello.py file worked. – Syed Dec 28 '19 at 3:56
  • Weird. That shouldn't affect the script execution. :) – fuxia Dec 28 '19 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.