Error after deploying Django in Heroku

I just deployed my sentiment analysis django app to heroku. The deployment was successful and I can access the features like login register etc. But I am unable to analyze the text because apparently, nltk cannot be found. But I have installed and imported it in my local host and it already worked before.

This is my first time deploying in heroku so I am unfamiliar with everything.

This is how it looksin my local host:

This is the error in the herokuapp live webapp when I try to analyze the sentiment.

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
settings.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')

This is my code: https://github.com/zashy05/sentymeter-live

How can I solve this issue?

Judging from that error it seems like nltk is installed properly on heroku, but your resource you’re trying to load isn’t available. When you run this locally, where are the files stored that represent punkt? How are you getting those same files up to the heroku dynos?

I uploaded them in github and connect my github to heroku to deploy it. The files that represents punkt are the sentiment analysis page.

One of the solutions I tried is to connect to heroku ( heroku ps:exec ) and run a django shell python manage.py shell and then install the relevant nltk modules from the python shell:

import nltk
nltk.download('punkt')

It worked but sometimes I get this error

 There was an error connecting to the dyno!

from here:

another way that I found was to create nltk.txt but Im not sure if I did it right because it doesn’t work for me as well.
From here: