我剛剛將我的情緒分析 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
設定.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')
誰能幫我解決這個問題?
uj5u.com熱心網友回復:
看起來您安裝了 nltk python 包,但沒有安裝任何資料。
如果您想要快速修復,您可以連接到您的 heroku ( heroku ps:exec ) 并運行 django shell python manage.py shell,然后從 python shell 安裝相關的 nltk 模塊:
import nltk
nltk.download('punkt')
由于您希望能夠重新部署它,因此最好創建一個管理命令來執行此操作并以類似于運行遷移的方式運行它。管理命令應在{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(..)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/405422.html
標籤:
