Home » Php » I try to run Python using the exec function in php, but the specific library required for Python code is not imported

I try to run Python using the exec function in php, but the specific library required for Python code is not imported

Posted by: admin February 25, 2020 Leave a comment

Questions:

This php code.

 exec("C:/Users/조영래/AppData/Local/Programs/Python/Python37/python.exe C:/Bitnami/wampstack-7.2.19-2/apache2/htdocs/main.py", $out, $ret);
 foreach ($out as $line){
   print "$line\n";
 }

This python code code.(main.py)

import os
print("4")

import torch
print("5")

from torchvision.transforms import ToTensor, Normalize
print("6")

from torchvision.utils import save_image
print("7")

from PIL import Image
print("8")

from networks import Generator
print("9")

from options import TestOption
print("10")

result in webpage

4 5

It works with python in PC, but it does not work in webpage using php exec.
I write Print(“number”) because I want to check where happen error in code).
And I found problem.
I changed torchvision Importing code’s position and checked it.
I checked it only with torchvision.

How to&Answers: