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?

Yethrosh
  • 131
  • 1
  • 1
  • 9

2 Answers2

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.

Blcknx
  • 1,921
  • 1
  • 12
  • 38
  • Yes, this method works but it does not take $args variable. Any suggestion, please? – Yethrosh Jan 02 '19 at 09:59
  • 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: https://stackoverflow.com/questions/2740906/how-to-access-command-line-arguments-inside-a-function-using-bash – Blcknx Jan 02 '19 at 20:09
  • 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? – Blcknx Jan 07 '19 at 09:31
  • Yes it perfectly works on shell, but does not in php. It seems that $args variable does not work. – Yethrosh Jan 09 '19 at 08:58
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;

?>
PrakashG
  • 1,642
  • 5
  • 20
  • 30
  • Thanks for your reply. I am trying this but it throws . – Yethrosh Jan 02 '19 at 09:35
  • @Yethrosh, Please remove the function and use function's code directly. And I'm sure enough that this is not because of this function exec_command() as I daily use this. – PrakashG Jan 02 '19 at 10:37
  • Could you please update the above code as you said? :) – Yethrosh Jan 03 '19 at 03:09
  • @Yethrosh, Please try the updated solution, and if it helps you then accept the answer. – PrakashG Jan 03 '19 at 11:20
  • No, it does not goes in virtual environment. – Yethrosh Jan 07 '19 at 04:27
  • @Yethrosh, Please post the exact error message you're getting when you run above code, so that I can change accordingly. – PrakashG Jan 07 '19 at 06:40
  • It throws error of importing python library which i installed only in virtual environment. And now I want to call python script (of virtual environment) using php as described in question. :) – Yethrosh Jan 09 '19 at 08:57