10

I have to execute a Python script which pulls a large amount of data to the database. It is working fine while I am running a project using the command php artisan serve, but it is throwing an error after calling the public folder URL given below

localhost/project/public

Error:

The command "python /opt/lampp/htdocs/laravel/projectname/beta/projectname/public/python/pubmed_abstract/pubmed_engine.py '{"term":"cancer"}'" failed.

Exit Code: 1(General error) Working directory: /opt/lampp/htdocs/laravel/infocytosis/beta/infocytosis/public Output:

================ Error Output: ================

Traceback (most recent call last): File "/opt/lampp/htdocs/laravel/projectname/beta/projectname/public/python/pubmed_abstract/pubmed_engine.py", line 5, in from Bio import Entrez ImportError: No module named Bio

Code I used:

   $python_path=public_path().'/python/doom_abstract/doom_engine.py';
   
   $variables='{"term":"'.addslashes($request->term).'"}';

   $process = new Process("python $python_path '$variables'");
   $process->run(); 

  return redirect()->back()->withMessage('Filter saved successfully');
Community
  • 1
  • 1
Ujjual
  • 958
  • 2
  • 10
  • 36
  • 2
    line 5, in _from Bio import Entrez ImportError: No module named Bio_ points at the problem. It looks like the python script contains errors or dependencies are missing. Maybe you need to switch to the correct python environment first? – dbrumann Mar 06 '19 at 10:41
  • That error won't appear and works fine if i run project with internal server like php artisan serve.It appears when the I call like http://localhost/laravel/infocytosis/beta/infocytosis/public/ @dbrumann – Ujjual Mar 06 '19 at 11:21
  • 1
    I think `php artisan serve` is using php-cli to run commands but when you use a web server (apache or nginx), there is no php-cli. try something more pure. like `exec` or `shel_exec` instead of `Process` – reza Mar 13 '19 at 07:51
  • have you tried something like $process = new Process(["python",$python_path, $variables]); looks like the process method takes an array https://symfony.com/doc/current/components/process.html also https://stackoverflow.com/questions/41020068/running-python-script-in-laravel – hurnhu Nov 15 '19 at 16:38
  • I'm not sure but I faced a similar problem and fixed it by changing the environment before executing the script – Parsa_Gholipour Jan 02 '21 at 17:15

2 Answers2

1

You can try following one

$python_path=public_path('python/doom_abstract/doom_engine.py');
Sadikur Rahaman
  • 128
  • 1
  • 4
-1

Instead of:

$process = new Process("python $python_path '$variables'");
$process->run(); 

how about

shell_exec("python $python_path '$variables'")
senty
  • 12,385
  • 28
  • 130
  • 260