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

Asked

Viewed 30 times

0

I’m trying to run a script in Python3 that I developed, which is located in a folder within the "Folder app" of Laravel. One 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 whole integration works perfectly. But, after I uploaded my application to my remote server (Debian), I am 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 script directly 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 might be going on? It’s some server configuration?

Thanks for your help!

1 answer

1

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

Take a look in this reply by Soen, apparently the lib nltk did not find the resource stopwords and threw the error.

According to the link above, a solution would be to open python on your server console 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 no parameters, otherwise you will download a lot of things that you may not need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.