简体   繁体   English

导入TextBlob时出错

[英]Error from importing TextBlob

I am having a problem when running a Python script within my Laravel project. 在Laravel项目中运行Python脚本时遇到问题。 I do not get this problem when running the python script using virtualenv and without. 使用virtualenv运行python脚本时,没有出现此问题。 I am using Win 10 64-bit. 我正在使用Win 10 64位。

When running the python script within Laravel using Symfony/Process, I am getting this error: 使用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
"""

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): 据我了解,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")

This is how textblob is declared on my main.py 这就是在我的main.py上声明textblob的方式

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 我从这篇文章中得到了答案: ValueError:找不到nltk的默认下载目录

What I did is to initialize a NLTK_DATA environment variable within the py script. 我所做的是在py脚本中初始化NLTK_DATA环境变量。 This solved the problem. 这样就解决了问题。

import os

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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