Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop NLTK from outputting to terminal when downloading data?

When I run my program, which is using:

nltk.download('wordnet')
from nltk.corpus import wordnet

I get the following output to my terminal:

[nltk_data] Downloading package wordnet to
[nltk_data]     /Users/.../nltk_data...
[nltk_data]   Package wordnet is already up-to-date!

My program relies on not having this information saved to the terminal and a resulting output file, so how can I prevent the above lines from occurring, or write it to sys.stderr so it doesn't get included instead of it being through print?

like image 753
helpisgood Avatar asked Dec 03 '17 03:12

helpisgood


2 Answers

Use quiet=True:

import nltk
nltk.download('wordnet', quiet=True)
like image 50
alvas Avatar answered Oct 05 '22 23:10

alvas


A much better solution is suggested in this answer.


Old Answer:

According to the source code, nltk downloader uses straightforward print() calls to report progress. This means that there is no logger involved which you can control or pre-configure.

One of the options is to modify the sys.stdout temporarily on the fly - there is that redirect_stdout() context manager in Python 3.4+:

from contextlib import redirect_stdout
import os

import nltk
from nltk.corpus import wordnet


with redirect_stdout(open(os.devnull, "w")):
    nltk.download('wordnet')

Or some other options:

  • Suppress calls to print (python)
  • Silence the stdout of a function in Python without trashing sys.stdout and restoring each function call
like image 21
alecxe Avatar answered Oct 06 '22 01:10

alecxe