导入TextBlob

时间:2018-05-09 18:45:47

标签: python python-3.x laravel nltk textblob

我在Laravel项目中运行Python脚本时遇到问题。使用virtualenv运行python脚本时没有遇到此问题。我正在使用Win 10 64位。

使用Symfony / Process在Laravel中运行python脚本时,我收到此错误:

"""
The command "C:\xampp\htdocs\projects\laravel-project\scripts\sentiment\env\Scripts\activate && py C:\xampp\htdocs\projects\laravel-project\scripts\sentiment\main.py" failed.\n ◀
\n
Exit Code: 1(General error)\n
\n
Working directory: C:\xampp\htdocs\projects\laravel-project\public\n
\n
Output:\n
================\n
\n
\n
Error Output:\n
================\n
Traceback (most recent call last):\r\n
  File "C:\xampp\htdocs\projects\laravel-project\scripts\sentiment\main.py", line 10, in <module>\r\n
    from textblob import TextBlob\r\n
  File "C:\xampp\htdocs\projects\laravel-project\scripts\sentiment\env\lib\site-packages\textblob\__init__.py", line 2, in <module>\r\n ◀
    from .blob import TextBlob, Word, Sentence, Blobber, WordList\r\n
  File "C:\xampp\htdocs\projects\laravel-project\scripts\sentiment\env\lib\site-packages\textblob\blob.py", line 28, in <module>\r\n
    import nltk\r\n
  File "C:\xampp\htdocs\projects\laravel-project\scripts\sentiment\env\lib\site-packages\nltk\__init__.py", line 160, in <module>\r\n
    from nltk.downloader import download, download_shell\r\n
  File "C:\xampp\htdocs\projects\laravel-project\scripts\sentiment\env\lib\site-packages\nltk\downloader.py", line 2237, in <module>\r\n ◀
    _downloader = Downloader()\r\n
  File "C:\xampp\htdocs\projects\laravel-project\scripts\sentiment\env\lib\site-packages\nltk\downloader.py", line 443, in __init__\r\n ◀
    self._download_dir = self.default_download_dir()\r\n
  File "C:\xampp\htdocs\projects\laravel-project\scripts\sentiment\env\lib\site-packages\nltk\downloader.py", line 954, in default_download_dir\r\n ◀
    raise ValueError("Could not find a default download directory")\r\n
ValueError: Could not find a default download directory\r\n
"""

据我所知,nltk是一个textblob依赖项,但是nltk / downloader.py在default_download_dir处返回false(但是当我测试python shell中的if条件时它应该是true):

    # On Windows, use %APPDATA%
    if sys.platform == 'win32' and 'APPDATA' in os.environ:
        homedir = os.environ['APPDATA']

    # Otherwise, install in the user's home directory.
    else:
        homedir = os.path.expanduser('~/')
        if homedir == '~/':
            raise ValueError("Could not find a default download directory")

这就是我在main.py

上声明textblob的方法
from textblob import TextBlob

除了这个特定的包,我的其他进口没有任何问题。我现在处于障碍中,所以所有的帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

我从这篇文章得到了答案: ValueError: Could not find a default download directory of nltk

我所做的是在py脚本中初始化NLTK_DATA环境变量。这解决了这个问题。

import os

os.environ['NLTK_DATA'] = os.path.abspath('../scripts/nltk_data')