tensorflow: "import tensorflow" configures Python logging if tensorboard is not installed
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOS 10.15.7, Python 3.8 from MacPorts, Python venv
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: N/A
- TensorFlow installed from (source or binary): binary wheel
tensorflow-2.4.1-cp38-cp38-macosx_10_11_x86_64.whl
from PyPI (usingpython -m pip install tensorflow
) - TensorFlow version (use command below): v2.4.0-49-g85c8b2a817f 2.4.1
- Python version: 3.8.8
- Bazel version (if compiling from source): N/A
- GCC/Compiler version (if compiling from source): N/A
- CUDA/cuDNN version: N/A
- GPU model and memory: Radeon Pro 560X 4 GB, Intel UHD Graphics 630 1536 MB
Describe the current behavior
After uninstalling tensorboard
from the Python virtual environment with pip uninstall tensorboard
, executing import tensorflow
at the prompt causes logging to be configured: in particular, a StreamHandler
handler is added to the root logger.
As a result, in a large Python GUI (PyQt5) application, an import tensorflow
at application startup time resulted in many log messages unrelated to tensorflow being printed to the console; without importing tensorflow, those log messages weren’t visible. (Note that the application had set the level of the root logger to logging.DEBUG
, and added its own root-level loggers, under the assumption that it would be the only part of the Python environment working with the configuration for the root-level logger.)
Describe the expected behavior
Following usual logging best practices (libraries should only emit log messages but not configure logging; log configuration should be left to the application using the library), import tensorflow
does not affect the root logger.
Standalone code to reproduce the issue
Here’s an interpreter session demonstrating the issue
Python 3.8.8 (default, Feb 22 2021, 09:21:28)
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> root_logger = logging.root
>>> root_logger.handlers
[]
>>> import tensorflow
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
WARNING:root:Limited tf.summary API due to missing TensorBoard installation.
>>> root_logger.handlers # expected an empty list
[<StreamHandler <stderr> (NOTSET)>]
Additional information
This looks like a shallow bug and an easy fix. Here’s the troublesome code: https://github.com/tensorflow/tensorflow/blob/c023a829167aa4a233a36479cd338ef9784b6990/tensorflow/compat_template.__init__.py#L43
Diagnosis: because the Python logging system hadn’t already been configured at the point of the import tensorflow
execution, and because tensorboard
wasn’t available,_logging.warning
was then called as above: that then implicitly configured logging by calling logging.basicConfig
.
A simple solution would be to set up a logger in the normal way (logger = logging.getLogger(__name__)
) and use logger.warning
instead of logging.warning
; that way, implicit configuration of the logging machinery is avoided.
We were able to work around this problem in our application by making sure that we configured logging (adding a null handler to the root logger) before importing tensorflow
, so that logging.basicConfig
was no longer invoked.
Another workaround was to ensure that tensorboard
was installed, but since we don’t generally need the functionality provided by tensorboard
, our packaging system leaves tensorboard
out as a explicit dependency of tensorflow
. If the recommendation is that tensorboard
should be treated as a required dependency of tensorflow
, that would be good to know.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 1
- Comments: 18 (3 by maintainers)
☝️ this
I’d also like to point out that it is rather bad style to use the root logger for these things, which makes it harder to track down where these logs are coming from and impractical to filter them out with a logging config.
@SuryanarayanaY I’m going to unfollow this issue. We’ve long since worked around the issue in our own code; my motivation in reporting it was to help others who might encounter the same problem. (I don’t know for sure, but I’d guess that we’re not the only people unbundling
tensorboard
.)I’m a little bit baffled by the process and the discussion here, and especially by all the attempts to reproduce in a notebook instead of at a console - the notebook is quite a different (and much more complicated) environment. We seem to be going in circles trying to reproduce. The issue exists and is trivially easy to reproduce at a console (or in a script); it’s also an easy fix. The decision to be made should simply be whether it’s worth fixing it or not. I can certainly see arguments either way there, and it’s up to the tensorflow developers to decide.
@SuryanarayanaY
I looked at the gist, but I don’t see how it’s related to the issue. The issue is that
import tensorflow
configures logging: it adds a handler to Python’s root logger. In general, libraries should not do logging configuration, since it makes it more difficult for applications using those libraries to configure logging in a way that’s appropriate for the application.As I said, it’s a relatively minor issue, so would be completely reasonable for the developers to close as “won’t fix”. But it is still an issue with the latest tensorflow, and it’s essentially a two-line fix.
Hi @mdickinson , At first step itself i have used command
pip uninstall tensorboard
and then installed TF 2.10V and found that Tensorboard 2.10 also getting installed.The i have uninstalled tensorboard again and then got confirmation thatwhere we are getting uninstallation message as well as Warning.
I have tested the issue with latest version 2.11 V and found that tensorboard-2.11 installed along with TF.Upon
pip uninstall tensorboard
i got confirmation message as below.Successfully uninstalled tensorboard-2.11.0
Again on trying
pip uninstall tensorboard
i got the warning as below.WARNING: Skipping tensorboard as it is not installed.
Please refer to attached gist.The problem resolved in latest version 2.11V.
Please go through and confirm whether we can close this issue now.
Note though that
tensorboard
is listed as a dependency in TF’ssetup.py
. Whenever youpip install tensorflow
you should also get TB.@jvishnuvardhan: Ah, looking harder at your gist, you’ve already imported
tensorflow
to get the version, right at the top. That means that your later import will be using the existing cached import. You need to import tensorflow for the first time after uninstalling tensorboard.