Home > Software engineering >  How do I get shell_exec() to change the environment before running a python script
How do I get shell_exec() to change the environment before running a python script

Time:01-19

I created a virtual environment inside the www/mysite/venv folder and have a python script inside the folder that I'm trying to execute from the web browser. The PHP function I'm using is shell_exec().

<?php
shell_exec("source /home/www/mysite/venv/activate");
shell_exec("python3 /home/www/mysite/venv/python-script.py");
?>

The second line in the script runs but doesn't work properly because the required pip libraries are in the virtual environment and the environment does not get activate

I've also tried:

  • /bin/bash/source

  • /bin/sh/source

  • source bin/activate

CodePudding user response:

You need to find the path to the python executable for your virtual environment (like ~/.venv/path/to/python or something similar). You can find out when your python venv is active, just do a "which python3" to see it.

CodePudding user response:

shell_exec("/home/www/mysite/venv/bin/python3 /home/www/mysite/venv/python-script.py"); worked without having to activate the virtual environment. I had to give the full path to the python version installed in the venv and the full path to the location of the script

  • Related