I just had this same problem. What ended up working for me is creating an 'nltk_data' directory in the application's folder itself, downloading the corpus to that directory and adding a line to my code that lets the nltk know to look in that directory. You can do this all locally and then push the changes to Heroku.
So, supposing my python application is in a directory called "myapp/"
Step 1: Create the directory
cd myapp/
mkdir nltk_data
Step 2: Download Corpus to New Directory
python -m nltk.downloader
This'll pop up the nltk
downloader. Set your Download Directory to whatever_the_absolute_path_to_myapp_is/nltk_data/
. If you're using the GUI downloader, the download directory is set through a text field on the bottom of the UI. If you're using the command line one, you set it in the config menu.
Once the downloader knows to point to your newly created nltk_data
directory, download your corpus.
Or in one step from Python code:
nltk.download("wordnet", "whatever_the_absolute_path_to_myapp_is/nltk_data/")
Step 3: Let nltk Know Where to Look
ntlk
looks for data,resources,etc. in the locations specified in the nltk.data.path
variable. All you need to do is add nltk.data.path.append('./nltk_data/')
to the python file actually using nltk, and it will look for corpora, tokenizers, and such in there in addition to the default paths.
Step 4: Send it to Heroku
git add nltk_data/
git commit -m 'super useful commit message'
git push heroku master
That should work! It did for me anyway. One thing worth noting is that the path from the python file executing nltk stuff to the nltk_data directory may be different depending on how you've structured your application, so just account for that when you do nltk.data.path.append('path_to_nltk_data')