2016-07-09 22:40



I have this python 3 program that I have problem running. When I run thought ssh using python3 it works like it should(it prints out data), but when I try to run it like python it gives me lots of errors because its PY3 program. So I want to find a way on how could I make that this PY script I have, would print out the answers. To echo everything python prints on website, I am using this WP plugin:

<?php # -*- coding: utf-8 -*-
/* Plugin Name: 4230 */
header('Content-Type: text/html; charset=cp1252');
add_shortcode( '4230', 'execute_python_with_argv' );
function execute_python_with_argv(){
$description = array (     
0 => array("pipe", "r"),  // stdin
1 => array("pipe", "w"),  // stdout
$application_system = "python ";
$application_path .= plugin_dir_path( __FILE__ );
$application_name .= "";
$separator = " ";
$application = $application_system.$application_path.$application_name.$separator;
$pipes = array();
$proc = proc_open ( $application , $description , $pipes );
if (is_resource ( $proc ))
$var=  stream_get_contents ($pipes [1] ); //Reading stdout buffer
echo "<pre>".$var."</pre>";
$output = ob_get_clean();
return $output;

There should not be any syntax errors in this code, I didn't had any problem with it and python3 when I was working on WAMP. But I though that this code should activate python program, so maybe it is possible to make it send request for python to run with python3?

Also as which python3 through ssh prints out /home/meteo/.local/bin/python3, I have tried to add this line to the top of mine python script as a "shebang" like this #!/home/meteo/.local/bin/python3 but it didn't help running my PY script and python3 to print out the data.

So what should I do to make this python script to run as python3 and print out the answer?

EDIT: This is the error I get when I run python script with python

Traceback (most recent call last):
File "", line 4, in <module>
from   bs4 import BeautifulSoup
File "/home/meteo/public_html/wp-content/plugins/bs4/", line 30, in <module>
from .builder import builder_registry, ParserRejectedMarkup
File "/home/meteo/public_html/wp-content/plugins/bs4/builder/", line 4, in <module>
from bs4.element import (
File "/home/meteo/public_html/wp-content/plugins/bs4/", line 8, in <module>
from bs4.dammit import EntitySubstitution
File "/home/meteo/public_html/wp-content/plugins/bs4/", line 13, in <module>
from html.entities import codepoint2name
ImportError: No module named html.entities

EDITv2: Fixed this problem with new WP plugin:

<?php # -*- coding: utf-8 -*-
/* Plugin Name: viassh */   
header('Content-Type: text/html; charset=ANSI_X3.4-1968');
add_shortcode('viassh', 'HelloWorldShortcode');
function HelloWorldShortcode() {
$old_path = getcwd(); 
$output = shell_exec('./');
echo $output;
$output = ob_get_clean();
return $output;

Thanks for reading.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doumu5023 doumu5023 5年前

    A summary of the extended discussion in the comment section and a more general question to 'why is my Python script not running when called from PHP, cgi, etc.?'

    Can you call a Hello World script instead of your real script? e.g.

    #!/usr/bin/env python or python3
    from sys import version    
    • If yes, great, then the problem is in your real Python script and not not the way you call it. Check if you can run the script from the command line (no simple indentation errors, truncated lines from copy&paste), preferably as the user who will execute the script when started via the server (i.e. not as root/admin). Use a logger to see where the script crashes.

    • If no, check the permissions and owner of the script.

    In this particular case, wrap the whole starting Pytyhon and script, in a shell file and call this file from your server. A bit hacky but better than losing all your hair about it.

    create a file file

    echo "just a test to see if the script is executed, remove this line later"
    python3 /absolute/path/to/script/

    make sure the file is executable by using chmod +x and then call this script via CGI or PHP.

    点赞 评论 复制链接分享
  • dprh34164 dprh34164 4年前

    make the python script executable

    chmod +x
    点赞 评论 复制链接分享