0

I am trying to automate something using a python script from php. I have the following codes in php whereby it is trying to execute the python script

$command = escapeshellcmd('python interaction.py');
$output = shell_exec($command);

I have the interaction.py python script in the same location. Also the chrome driver in the same location too.

interaction.py contains the following codes

from selenium import webdriver # Importing the web driver class from selenium
from selenium.webdriver.common.keys import Keys # This is the import for those keys that is not letter, number etc. Eg: Enter, Tab, Shift etc

driver_location = "chromedriver.exe" # Chrome driver location
driver = webdriver.Chrome(executable_path=driver_location) # Initiating the driver
driver.get("http://secure-retreat-92358.herokuapp.com/") # Opening the web page

But when I trigger the php page, the python is not executing. I believe this could be due to the libraries are not imported correctly. I used pycharm to run and test that python script whereby all those libraries are able to import correctly and execute the codes.

Does anyone know what is the issue here? If that is due to those import issue, how can I include those imports successfully into my interaction.py file while calling from php?

Anu
  • 1,123
  • 2
  • 13
  • 42
  • Hi Anu, Some ideas to try if you haven't already might include these: Have you tried running the `interaction.py` program on its own (without the PHP part for now) just using Python to see if it runs as expected? Also, if you try running the PHP part, would it be possible to show what the `$output` variable contains after it runs (using something like `print_r($output);` after the `$output = ...` line)? – summea Feb 27 '21 at 22:47
  • @summea. Yes. I tried the interaction.py in my PyCharm and it is running without any issue. I checked also in my shell if `selenium` is installed. But when I try from php, it is not executing. About the $output, it is printing `NULL` when I do a `var_dump` in php side – Anu Mar 01 '21 at 05:41
  • Another thought might be to try [specifying the full path](https://stackoverflow.com/questions/66404529/crontab-via-exec-not-working-with-php-7-4-deb-10/66406082#comment117396094_66404529) to python in your PHP code. For example, you might need to run the `which python` command in your terminal first to see the full path to python in your environment. Then, you could use that full path like this, based on your code: `$command = escapeshellcmd('/full/path/to/python interaction.py');`. – summea Mar 03 '21 at 02:44
  • I tried this earlier. But didn't work. I tried giving full path to interaction.py too. But it worked neither – Anu Mar 03 '21 at 03:07
  • Hi Anu, Thanks for trying that at least! Another idea might be: what happens if you try using a different Python script, maybe something simple like: `print('hello')` called `test.php` and try running that in your PHP script instead just as a test. I would still recommend putting the full path to `python` and the full path to your `test.php` file. This approach might help you to know if there really is an import issue going on. If the output shows "hello" after being run from the Python script, it might help narrow down what is working vs. what isn't working. – summea Mar 03 '21 at 04:02
  • Hi @summea Thank you for giving suggestions. I just want to emphasize that I am trying to run python script from php. As you said I already tried simple code such as `print("Hello")` in my interaction.py. But nothing is happening. When do `var_dump`, I am getting `NULL` as output – Anu Mar 03 '21 at 05:32

0 Answers0