1

I am trying to execute a python script from php in my Cpanel. I have setup a python app version 3.7.12, Below is my php code to execute the script

<?php
   $command=escapeshellcmd('python /home/mesuivln/seopython/h1parser.py');
   $output = shell_exec($command);
   echo $output;
 ?>

it is working with below Python Code

#!/usr/bin/env python

import requests
import re
import sys

print('ok')

But don't work whenever I try to import below modules. Returns internal server error

#!/usr/bin/env python

import requests
from readability import Document
import yake
from bs4 import BeautifulSoup
from urllib.parse import urlparse
from collections import Counter
import re
import sys
import mysql.connector
import html

print('ok')

But samething works when I execute from the cpanel terminal with below commands

source /home/mesuivln/virtualenv/seopython/3.7/bin/activate && cd /home/mesuivln/seopython

then

 python h1parser.py

Below is my ErrorLog

  Traceback (most recent call last):
  File "/home/mesuivln/seopython/passenger_wsgi.py", line 8, in <module>
  wsgi = imp.load_source('wsgi', 'h1parser.py')
  File "/opt/alt/python37/lib64/python3.7/imp.py", line 171, in load_source
  module = _load(spec)
  File "<frozen importlib._bootstrap>", line 696, in _load
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "h1parser.py", line 5, in <module>
  import yake
  ModuleNotFoundError: No module named 'yake'
  Traceback (most recent call last):
  File "/home/mesuivln/seopython/passenger_wsgi.py", line 8, in <module>
  wsgi = imp.load_source('wsgi', 'h1parser.py')
  File "/opt/alt/python37/lib64/python3.7/imp.py", line 171, in load_source
  module = _load(spec)
  File "<frozen importlib._bootstrap>", line 696, in _load
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "h1parser.py", line 5, in <module>
  import yake
  ModuleNotFoundError: No module named 'yake'

I have installed the module yake in my app location and it is working from the terminal command but not working while calling from php or through app url, below is the module directory location

https://i.stack.imgur.com/0ygur.png

how can I solve this

roy
  • 131
  • 1
  • 10
  • 3
    With only `internal server error` many people can't help you unless you also post the error that is logged in your log file. – Baracuda078 Dec 23 '22 at 10:30
  • I have edited my question and included the log – roy Dec 23 '22 at 10:39
  • That the script can't find the module can be caused by permission errors [maybe this question can help you](https://stackoverflow.com/questions/17236675/how-to-make-my-python-module-available-system-wide-on-linux) – Baracuda078 Dec 23 '22 at 10:45

0 Answers0