Error executing script in Python3 (with Symfony / Process) on a Laravel Controller

0

I'm trying to run a Python3 script I've developed, which is located in a folder inside Laravel's app folder. One of the script is called by a Controller and the other by an Artisan Command.

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 environment, the entire integration works perfectly. But, after I uploaded my application to my remote server (Debian), I'm getting the following error:

"""
The command "python3 SearchEngine.py "O que é fogo no **?"" 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
"""

Only, when I run the direct script on my terminal, the results are coming as expected. So the error is not related to installing libs on my server. The script that is called by php artisan: command also works perfectly.

What can be happening? Is there a server configuration?

Thanks for the help!

    
asked by anonymous 18.09.2018 / 14:48

1 answer

1

Your error seems to be related to installing libs on the server.

Take a look at in this SOEn response , apparently the nltk lib did not find the stopwords resource and launched the error.

According to the link above, one solution would be to open python on the console of your server and run the command:

import nltk
nltk.download("stopwords")

Or, as mentioned in this answer :

python -m nltk.downloader stopwords

Just be careful not to run download with no parameters, otherwise you will download a lot of things you may not need.

    
18.09.2018 / 14:55