Symfony/Process exception when running Python3 from Laravel controller on remote server (Debian)

621 Views Asked by At

I'm trying to execute a python script (3.6.5) which is located in one folder inside my Laravel app folder. The script is been called from a controller and retrieves the output of the scrpit. I'm using Symfony/process to execute the scrip, like in the code below:

public static function searchAnswers($input)
    {
        $process = new Process(array('dir', base_path() . '/app/SearchEngine'));
        $process->setWorkingDirectory(base_path() . '/app/SearchEngine');
        $process->setCommandLine('python3 SearchEngine.py ' . '"'. $input .'"');
        $process->setTimeout(2 * 3600);

        $process->run();  

        if (!$process->isSuccessful()) {            //Executes after the command finishes
            throw new ProcessFailedException($process);
        }

        $list_ids = array_map('intval', explode(' ', $process->getOutput()));
        info($list_ids);
        $solicitations = Solicitation::join('answers', 'solicitations.id', '=', 'answers.solicitation_id')
                            ->whereIn('solicitations.id', $list_ids)
                            ->limit(20)
                            ->get();
        info($solicitations);
        return $solicitations;
    }

In my localhost, the script is been called with no issues, either from my terminal or from my running application via HTTP requests. But, after I uploaded my app version to my remote server, which is Debian, I'm getting the following exception:

"""
The command "python3 SearchEngine.py "O que é diabetes?"" failed.\n
\n
Exit Code: 1(General error)\n
\n
Working directory: /var/www/plataformaTS/app/SearchEngine\n
\n
Output:\n
================\n
\n
\n
Error Output:\n
================\n
Traceback (most recent call last):\n
  File "/usr/local/lib/python3.4/dist-packages/nltk/corpus/util.py", line 80, in __load\n
    try: root = nltk.data.find('{}/{}'.format(self.subdir, zip_name))\n
  File "/usr/local/lib/python3.4/dist-packages/nltk/data.py", line 675, in find\n
    raise LookupError(resource_not_found)\n
LookupError: \n
**********************************************************************\n
  Resource \e[93mstopwords\e[0m not found.\n
  Please use the NLTK Downloader to obtain the resource:\n
\n
  \e[31m>>> import nltk\n
  >>> nltk.download('stopwords')\n
  \e[0m\n
  Searched in:\n
    - '/var/www/nltk_data'\n
    - '/usr/share/nltk_data'\n
    - '/usr/local/share/nltk_data'\n
    - '/usr/lib/nltk_data'\n
    - '/usr/local/lib/nltk_data'\n
    - '/usr/nltk_data'\n
    - '/usr/share/nltk_data'\n
    - '/usr/lib/nltk_data'\n
**********************************************************************\n
\n
\n
During handling of the above exception, another exception occurred:\n
\n
Traceback (most recent call last):\n
  File "SearchEngine.py", line 20, in <module>\n
    stopwords = stopwords.words('portuguese')\n
  File "/usr/local/lib/python3.4/dist-packages/nltk/corpus/util.py", line 116, in __getattr__\n
    self.__load()\n
  File "/usr/local/lib/python3.4/dist-packages/nltk/corpus/util.py", line 81, in __load\n
    except LookupError: raise e\n
  File "/usr/local/lib/python3.4/dist-packages/nltk/corpus/util.py", line 78, in __load\n
    root = nltk.data.find('{}/{}'.format(self.subdir, self.__name))\n
  File "/usr/local/lib/python3.4/dist-packages/nltk/data.py", line 675, in find\n
    raise LookupError(resource_not_found)\n
LookupError: \n
**********************************************************************\n
  Resource \e[93mstopwords\e[0m not found.\n
  Please use the NLTK Downloader to obtain the resource:\n
\n
  \e[31m>>> import nltk\n
  >>> nltk.download('stopwords')\n
  \e[0m\n
  Searched in:\n
    - '/var/www/nltk_data'\n
    - '/usr/share/nltk_data'\n
    - '/usr/local/share/nltk_data'\n
    - '/usr/lib/nltk_data'\n
    - '/usr/local/lib/nltk_data'\n
    - '/usr/nltk_data'\n
    - '/usr/share/nltk_data'\n
    - '/usr/lib/nltk_data'\n
**********************************************************************\n
\n
"""

The exception points out to errors of import in the python script. But, when I execute the script from the terminal in the remote server either directly or by Artisan commands, everything works fine. Any ideas of what is happening?

Thanks in advance!

1

There are 1 best solutions below

0
On

I found the solution.

The error was that the lib nltk.stopwords was installed as root user and my application was trying to access the file as another user with no permissions. So I logged in as this user and installed the libraries again. Now, everything woks like a charm.