【问题标题】:Having Error after deploying Django in Heroku. LookupError在 Heroku 中部署 Django 后出现错误。查找错误
【发布时间】:2022-01-06 01:16:08
【问题描述】:

我刚刚将我的情绪分析 django 应用程序部署到 heroku。部署成功,我可以访问登录注册等功能。但我无法分析文本,因为显然找不到 nltk。但是我已经在我的本地主机中安装并导入了它,它之前已经工作过。

这是我第一次在heroku中部署,所以我对一切都不熟悉。

这是它在我的本地主机中的外观:

这是我尝试分析情绪时 herokuapp live webapp 中的错误。

    LookupError at /sentiment/type/
**********************************************************************
  Resource e[93mpunkte[0m not found.
  Please use the NLTK Downloader to obtain the resource:

  e[31m>>> import nltk
  >>> nltk.download('punkt')
  e[0m
  For more information see: https://www.nltk.org/data.html

  Attempted to load e[93mtokenizers/punkt/PY3/english.picklee[0m

  Searched in:
    - '/app/nltk_data'
    - '/app/.heroku/python/nltk_data'
    - '/app/.heroku/python/share/nltk_data'
    - '/app/.heroku/python/lib/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
    - ''
**********************************************************************
Request Method: POST
Request URL:    https://sentymeter.herokuapp.com/sentiment/type/
Django Version: 4.0
Exception Type: LookupError
Exception Value:    
**********************************************************************
  Resource e[93mpunkte[0m not found.
  Please use the NLTK Downloader to obtain the resource:

  e[31m>>> import nltk
  >>> nltk.download('punkt')
  e[0m
  For more information see: https://www.nltk.org/data.html

  Attempted to load e[93mtokenizers/punkt/PY3/english.picklee[0m

  Searched in:
    - '/app/nltk_data'
    - '/app/.heroku/python/nltk_data'
    - '/app/.heroku/python/share/nltk_data'
    - '/app/.heroku/python/lib/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
    - ''
**********************************************************************
Exception Location: /app/.heroku/python/lib/python3.8/site-packages/nltk/data.py, line 583, in find
Python Executable:  /app/.heroku/python/bin/python
Python Version: 3.8.9
Python Path:    
['/app/.heroku/python/bin',
 '/app',
 '/app/.heroku/python/lib/python38.zip',
 '/app/.heroku/python/lib/python3.8',
 '/app/.heroku/python/lib/python3.8/lib-dynload',
 '/app/.heroku/python/lib/python3.8/site-packages']
Server time:    Wed, 05 Jan 2022 20:16:57 +0000

setting.py

    """
Django settings for sentiment_emotion_analysis project.

Generated by 'django-admin startproject' using Django 2.1.7.

For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""


import os
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sentiment.settings")

# import django
# django.setup()

# from django.core.management import call_command

from django.contrib.messages import constants as messages
MESSAGE_TAGS = {
        messages.DEBUG: 'alert-debug',
        messages.INFO: 'alert-info',
        messages.SUCCESS: 'success',
        messages.WARNING: 'alert-warning',
        messages.ERROR: 'danger',
}


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'g&x!0fjh8c8)e_-z@gs1^lbngvqwk2(o3s(5zg!o&woxdsu_un'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['sentymeter.herokuapp.com' , '127.0.0.1']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'sentiment',
    'user',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    
    'whitenoise.middleware.WhiteNoiseMiddleware',
   
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'sentiment_emotion_analysis.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'sentiment_emotion_analysis.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

MODELS = os.path.join(BASE_DIR, 'sentiment/models')

谁能帮我解决这个问题?

【问题讨论】:

    标签: django heroku nltk


    【解决方案1】:

    看起来你安装了 nltk python 包,但没有任何数据。

    如果您想快速修复,可以连接到您的 heroku (heroku ps:exec ) 并运行 django shell python manage.py shell,然后从 python shell 安装相关的 nltk 模块:

    import nltk
    nltk.download('punkt')
    

    由于您希望能够重新部署它,最好创建一个management command 来执行此操作并运行它,就像您运行迁移的方式一样。管理命令应该在{app}/management/commands/

    这样的命令:

    from django.core.management.base import BaseCommand, CommandError
    
    class Command(BaseCommand):
        help = 'Install NLTK stuff'
       
        def handle(self, *args, **options):
            import nltk
            nltk.download(..) # replace the dots with the relevant corpora
    
    

    【讨论】:

    • 我尝试了第二种方法,但是已经将提交推送到了 github,但是当我尝试在 heroku 中重新部署它时,我收到了这个错误-----> Downloading NLTK corpora… ! 'nltk.txt' not found, not downloading any corpora ! Learn more: https://devcenter.heroku.com/articles/python-nltk -----> $ python manage.py collectstatic --noinput
    • 然后我尝试添加 nltk.txt 并添加了 punkt n 得到了这个错误-----> Downloading NLTK corpora… -----> Downloading NLTK packages: punkt /app/.heroku/python/lib/python3.8/runpy.py:127: RuntimeWarning: 'nltk.downloader' found in sys.modules after import of package 'nltk', but prior to execution of 'nltk.downloader'; this may result in unpredictable behaviour warn(RuntimeWarning(msg)) [nltk_data] Downloading package punkt to [nltk_data] /tmp/build_6f3f8e9f/.heroku/python/nltk_data... [nltk_data] Unzipping tokenizers/punkt.zip. -----> $ python manage.py collectstatic --noinput
    • 但是你给出的第一个方法确实有效,所以我想我会坚持下去。非常感谢你帮助我:)
    • 是的,你应该用你想要安装的 nltk 语料库替换 2 个点,我猜你的情况是 punkt
    • 我有一个问题,如果我关闭我的笔记本电脑或者关闭我下载软件包的外壳,安装的打包会消失吗?因为我让我的笔记本电脑休眠了一段时间,同样的错误再次发生。当我打开笔记本电脑时,我在 shell Connection to the dyno timed out! 上看到了这个。但是在我按照你提到的步骤重新安装 bac 之后,一切都恢复正常了。但是如果我关闭我的笔记本电脑,它会再次发生吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 2017-01-23
    • 2016-08-26
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多