How to disable/suppress Tensorflow warnings in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
5 min

banner

# How to disable/suppress Tensorflow warnings in Python

Use the os.environ module to set the TF_CPP_MIN_LOG_LEVEL environment variable to 3 to disable/suppress all tensorflow warnings.

When the environment variable is set to 3, info, warning and error messages are not logged.

main.py
# 👇️ Disable `tensorflow` warnings import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf print("TensorFlow version:", tf.__version__)

Make sure to add the import statement of the os module and the line that sets the TF_CPP_MIN_LOG_LEVEL environment variable above your tensorflow import in the file.

Running the code sample above produces the following output.

suppress tensorflow warnings

If I remove the import of the os module and the line that sets the environment variable, the following output is produced.

without suppressing tensorflow warnings

Note that the import statement of the os module and the line that sets the environment variable has to be placed above the line that imports tensorflow.

The TF_CPP_MIN_LOG_LEVEL environment variable is used to control TensorFlow's log level.

TF_CPP_MIN_LOG_LEVEL valueDescription
"0"All messages are logged.
"1"Logs all messages except INFO.
"2"Logs all messages except INFO and WARNING. (default)
"3"Logs all messages except INFO, WARNING and ERROR.

By default the log level is set to "2".

# Setting the TF_CPP_MIN_LOG_LEVEL environment variable via the command line

An alternative to setting the TF_CPP_MIN_LOG_LEVEL environment variable in your code is to set it on your machine.

For example, you could use the command line to set the environment variable from your terminal before starting your Python application.

On macOS and Linux, run the following command in bash or zsh.

shell
# On macOS and Linux export TF_CPP_MIN_LOG_LEVEL="3"

On Windows, you could run the following command in CMD.

shell
set TF_CPP_MIN_LOG_LEVEL="3"

If you use PowerShell on Windows, run the following command instead.

shell
$env:TF_CPP_MIN_LOG_LEVEL="3"

Now you can remove the import of the os module and the code that sets the environment variable.

main.py
import tensorflow as tf print("TensorFlow version:", tf.__version__)

However, note that when using this approach, the environment variable is only set for the current shell session.

You could also set the environment variable permanently.

# macOS and Linux: Setting the TF_CPP_MIN_LOG_LEVEL environment variable

If you are on macOS or Linux, you can set the TF_CPP_MIN_LOG_LEVEL environment variable in your profile file.

Every time you start your computer, your profile file (e.g. ~/.bashrc or ~/.zshrc) will run and the environment variable will get set.

For example, if you use bash, you can add the variables to ~/.bashrc or ~/.bash_profile.

shell
# With Gedit sudo gedit ~/.bashrc # Or with Nano sudo nano ~/.bashrc

Add the following line at the bottom of your ~/.bashrc file.

~/.bashrc
export TF_CPP_MIN_LOG_LEVEL="3"

Then, source the updated ~/.bashrc file and restart your server.

shell
# BASH source ~/.bashrc source ~/.bash_profile

If you use zsh, you have to add the environment variable to your ~/.zshrc file instead.

shell
sudo gedit ~/.zshrc sudo nano ~/.zshrc

Paste the following line at the bottom of your ~/.zshrc file.

~/.zshrc
export TF_CPP_MIN_LOG_LEVEL="3"

Source your ~/.zshrc file and restart your server.

shell
source ~/.zshrc

Now you won't see any warning messages when running your TensorFlow code.

# Windows: Setting the TF_CPP_MIN_LOG_LEVEL environment variable

If you want to set the TF_CPP_MIN_LOG_LEVEL environment variable or other environment variables permanently on Windows:

  1. Click on the Search bar and type "environment variables".
  2. Click on "Edit the system environment variables".

edit system environment variables

  1. Click on the "Environment Variables" button.

  2. In the "System variables" section, click on the New... button.

  3. Set TF_CPP_MIN_LOG_LEVEL as the name of the variable and set its value to 3.

  4. Click on OK to apply the changes and restart your terminal and development server.

# Disable Tensorflow warnings using the get_logger() method

You can also use the get_logger() method from the tensorflow module to disable the TensorFlow warnings.

main.py
import tensorflow as tf tf.get_logger().setLevel('ERROR') print("TensorFlow version:", tf.__version__)

The tf.get_logger method has 5 levels of logging from the most serious to the least:

  1. FATAL
  2. ERROR
  3. WARN
  4. INFO
  5. DEBUG

When using this approach you might still see some info messages being printed.

You could also set the TF_CPP_MIN_LOG_LEVEL environment variable to suppress all warnings.

main.py
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf tf.get_logger().setLevel('ERROR') print("TensorFlow version:", tf.__version__)

Another thing you can try is to directly use the logging module to suppress warning messages in TensorFlow.

main.py
import logging logging.getLogger("tensorflow").setLevel(logging.WARNING) import tensorflow as tf print("TensorFlow version:", tf.__version__)

Note that the line that calls the getLogger() method has to be placed above your tensorflow import statement.

You can also try to use the set_verbosity() method in TensorFlow version 2.

main.py
import tensorflow as tf tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR) print("TensorFlow version:", tf.__version__)

# Setting the verbosity of Autograph log messages

You can also use the AUTOGRAPH_VERBOSITY environment variable to control the log level of autograph log messages.

main.py
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf os.environ['AUTOGRAPH_VERBOSITY'] = '0' # Autograph verbosity is now 0 os.environ['AUTOGRAPH_VERBOSITY'] = '3' # Autograph verbosity is now 3 tf.get_logger().setLevel('ERROR') print("TensorFlow version:", tf.__version__)

The AUTOGRAPH_VERBOSITY environment variable can be set to an integer. Larger values mean increased verbosity (e.g. 6-10), whereas 0 means no logging.

When debugging your application, you could try setting the environment variable to 8-10 to increase the verbosity.

# Using the silence_tensorflow module to disable TensorFlow's warnings

If none of the suggestions helped, you can always use the silence_tensorflow module to suppress TensorFlow's warnings.

First, install the module by running the following command from your terminal.

shell
pip install silence_tensorflow # Or with pip3 pip3 install silence_tensorflow # Or with python -m python -m pip install silence_tensorflow python3 -m pip install silence_tensorflow

Then import the silence_tensorflow function from the module and call it above your tensorflow import.

main.py
from silence_tensorflow import silence_tensorflow silence_tensorflow() import tensorflow as tf print("TensorFlow version:", tf.__version__)

Note that the silence_tensorflow() function has to be called before you import tensorflow.

You can also import and initialize the module in a single line.

main.py
import silence_tensorflow.auto import tensorflow as tf print("TensorFlow version:", tf.__version__)

If you use Pylint, you might have to suppress the warning for the unused import statement.

main.py
import silence_tensorflow.auto # pylint: disable=unused-import import tensorflow as tf print("TensorFlow version:", tf.__version__)

As long as the import statement is placed above the line that imports tensorflow, you won't get any warnings messages.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.