1

My goal is to be able to call a python script to be executed specific python version (conda virtual envinronment) from PHP.

At the moment, I can only execute it using default python versions. (2.7 amd 3.6).

So, from a php script I am call script to be executed in python3 default system version, and from this script I call another script to be executed via a anaconda virtual envinronment. So i try to use subprocess but it does not work, and I get a permission errror:

I call python 3 script (python_transfer.py) from php and then I call script (ptt.py) from python_transfer.py to be executed via anaconda version of python.

php file

$command = 'python3 /var/software/python_transfer.py';
exec($command,$output, $r);

ob_start();
exec($command . " 2>&1", $output);
$result = ob_get_contents();
ob_end_clean();

echo '<pre>';
print_r($output);
echo '</pre>';

python_transfer.py file:

import subprocess 
 output = subprocess.check_output(["/root/anaconda3/envs/10/bin/python3.6", "/var/software/ptt.py"])

ptt.py file:

print("this is a test")

output:

[0] => Traceback (most recent call last):
[1] =>   File "/var/software/python_transfer.py", line 32, in 
[2] =>     output = subprocess.check_output(["/root/anaconda3/envs/10/bin/python3.6", "/var/software/ptt.py"])
[3] =>   File "/usr/lib/python3.6/subprocess.py", line 356, in check_output
[4] =>     **kwargs).stdout
[5] =>   File "/usr/lib/python3.6/subprocess.py", line 423, in run
[6] =>     with Popen(*popenargs, **kwargs) as process:
[7] =>   File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
[8] =>     restore_signals, start_new_session)
[9] =>   File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
[10] =>     raise child_exception_type(errno_num, err_msg, err_filename)
[11] => PermissionError: [Errno 13] Permission denied: '/root/anaconda3/envs/10/bin/python3.6'
Rex5
  • 771
  • 9
  • 23
Brana
  • 1,197
  • 3
  • 17
  • 38

2 Answers2

1

It looks like the user running that php script (and therefore python) doesn't have permissions to execute python. Maybe try changing the permissions of /root/anaconda3/envs/10/bin/python3.6? The easiest way, but might not be the best practice, is to run chmod 775 /root/anaconda3/envs/10/bin/python3.6 or that but with sudo before it if it doesn't work.

  • I am able to execute python and python3 but not anacodan version (/root/anaconda3/envs/10/bin/python3.6). only verutal envinroments are effected. – Brana Aug 01 '19 at 13:01
  • 1
    Hmm well I don't know much about PHP, I thought you would've had to use `shell_exec`. Maybe it could be the permissions of the folder that the file is stored in? Is it feasible to run the command with `sudo`? – Marcus Weinberger Aug 01 '19 at 13:04
  • I tried and shell_exec gives likely the same error. It cannot trace errors so I do not know for sure. – Brana Aug 01 '19 at 13:13
  • Files that i execute are 777, then I do not think folders need to be also 777. The only problem seem to be virtual envinromnents – Brana Aug 01 '19 at 13:15
  • Sudo like this - $command = 'sudo /root/anaconda3/envs/10/bin/python3.6 /var/software/ptt.py'; ? – Brana Aug 01 '19 at 13:18
  • 1
    Yeah, try that. Also take a look at this (https://stackoverflow.com/a/19472082/8291579) and see if that's the issue. – Marcus Weinberger Aug 01 '19 at 13:18
  • 1
    It must be possible, but yeah sorry I'm all out of ideas – Marcus Weinberger Aug 01 '19 at 13:28
  • solved, the problem was that php user did not have right to execute non default python versions. – Brana Aug 01 '19 at 15:44
0

The problem actually was permission of php user. It did not have the right to execute non-default python. It works when I add the php user to have the same permissions as root (which mught not be advisable for security reasons I guess).

Command should be:

$command = 'echo your_php_user_password | sudo -S /root/anaconda3/envs/10/bin/python3.6 /var/software/python_script.py';
$exec($command,$output,$r);

your_php_user_password is the password for the php user.

Also you have to give to the user root priviledges, go to edit this file and add this line under the line that contains settings for root (root ALL=(ALL:ALL) ALL):

sudo visido
php_user ALL=(ALL:ALL) ALL

and then save, ctrl-O and then enter

You edit visudo file, and give the php user more right. If not the php will not have enough permissions to do the job.

Brana
  • 1,197
  • 3
  • 17
  • 38