1

I am running many python scripts from PHP. My php script template as below:

<?php
setlocale(LC_ALL, "en_US.utf8");
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");

$command = escapeshellcmd("/usr/bin/python2.7 /path/to/script");
$args = escapeshellarg($_GET["title"]). " " . 
escapeshellarg($_GET["user"]);
$output = shell_exec($command . " " . $args);
echo $output;

But now I need to run some python scripts which are in virtual environment.

I tried to replace /usr/bin/python2.7 with ./www/python/venv/bin/python3, but it does not work.

So how to run it in PHP?

3
  • 2
    Please explain what you mean by "but it does not work". Commented Dec 31, 2018 at 10:35
  • It means that Python script does not run. Commented Dec 31, 2018 at 10:39
  • Do you get an error? If yes, which one? Commented Jan 1, 2019 at 11:32

2 Answers 2

2

To really run venv you would need to do three steps in the shell:

  1. Change into project root.
  2. source venv/bin/activate
  3. run python path/to/script

Prerequisite you already have prepared a virtual env for the project.

You could combine this three steps into a bash script and call this script from PHP.

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

4 Comments

Yes, this method works but it does not take $args variable. Any suggestion, please?
I am sure you can access the $args variable in the bash script you call. However bash scripts are not my realm. Should be easy to look that up. Probably this is the answer: stackoverflow.com/questions/2740906/…
Figure out which part does not work! Can't you receive the argument inside the script? Does it work on the shell? Then, does it work from PHP?
Yes it perfectly works on shell, but does not in php. It seems that $args variable does not work.
1

Ideally You should use APIs, that is the best practice. But if you don't have API available you can use pipe.
That can be used like this function: exec_command($command) where,
$command = $command . " " . $args
Below is the code:

<?php
setlocale(LC_ALL, "en_US.utf8");
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");

$command = escapeshellcmd("/usr/bin/python2.7 /path/to/script");
$args = escapeshellarg($_GET["title"]). " " . 
escapeshellarg($_GET["user"]);

$command = $command . " " . $args;

$output = "";
$hd = popen($command, "r");
while(!feof($hd))
{
  $output .= fread($hd, 4096);
}
pclose($hd);

echo $output;

?>

7 Comments

Thanks for your reply. I am trying this but it throws <class 'importError'>.
@Yethrosh, Please remove the function and use function's code directly. And I'm sure enough that this <class 'importError'> is not because of this function exec_command() as I daily use this.
Could you please update the above code as you said? :)
@Yethrosh, Please try the updated solution, and if it helps you then accept the answer.
No, it does not goes in virtual environment.
|

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.