简体   繁体   中英

Python script can't find module when executed through PHP, but can find it when run via CLI

Having an issue when I try to run a python script via my PHP frontend I've tried both of the following:

$command = "(python3 /my/script.py --params) 2>&1";
$output = shell_exec($command);

and

ob_start();
passthru($command);
$output = ob_get_clean();

Both return:

Traceback (most recent call last): File "/my/script.py", line 14, in from woocommerce import API ModuleNotFoundError: No module named 'woocommerce'

When run through the CLI, the script works just fine, but it throws the above when run through PHP. This is the line in question giving me issues. At a loss as to why it can find the module in one instance and not the other.

from woocommerce import API
from wordpress_json import WordpressJsonWrapper

looks like your woocommerce module is not correctly installed in the system. It can be found when running the script in CLI, but when the system user that controls your PHP instance starts the script, the woocommerce module is not there. That indicates this module is installed for your normal CLI user only, and not globally in the system.

The direct solution is to install the woocommerce module globally, so that other users (including the system user that runs PHP) can find it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM