简体   繁体   中英

Error from importing TextBlob

I am having a problem when running a Python script within my Laravel project. I do not get this problem when running the python script using virtualenv and without. I am using Win 10 64-bit.

When running the python script within Laravel using Symfony/Process, I am getting this error:

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

From what I understand is that nltk is a textblob dependency, however nltk/downloader.py is returning false at default_download_dir (but it should be true as I test the if condition in a python shell):

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

This is how textblob is declared on my main.py

from textblob import TextBlob

My other imports are not having any problem besides this specific package. I'm at a roadblock at this point so all help will be appreciated.

I got my answer from this post: ValueError: Could not find a default download directory of nltk

What I did is to initialize a NLTK_DATA environment variable within the py script. This solved the problem.

import os

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM