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
2
Answers
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
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 awhich python3
to see it.