2

我正在尝试执行一个 python 脚本(3.6.5),它位于我的 Laravel 应用程序文件夹中的一个文件夹中。从控制器调用脚本并检索脚本的输出。我正在使用 Symfony/process 来执行脚本,如下面的代码所示:

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;
    }

在我的本地主机中,无论​​是从我的终端还是从我正在运行的应用程序通过 HTTP 请求,都可以毫无问题地调用该脚本。但是,在我将我的应用程序版本上传到我的远程服务器(即 Debian)后,我得到了以下异常:

"""
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
"""

异常指出 python 脚本中的导入错误。但是,当我直接或通过 Artisan 命令从远程服务器的终端执行脚本时,一切正常。对正在发生的事情有任何想法吗?

提前致谢!

4

1 回答 1

1

我找到了解决方案。

错误是该库nltk.stopwords是作为 root 用户安装的,而我的应用程序试图以另一个没有权限的用户身份访问该文件。因此,我以该用户身份登录并再次安装了库。现在,一切都像魅力一样。

于 2018-09-18T17:59:26.307 回答