question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Please don't configure logging in the top-level __init__.py

See original GitHub issue

import sklearn creates a StreamHandler and attaches it to the sklearn logger:

https://github.com/scikit-learn/scikit-learn/blob/0eebade2642b7f6c5105538eedada5b731998cc2/sklearn/__init__.py#L24

I’m not sure what the motivation for this is, but it’s a deviation from the normal “best practices” for logging, namely that libraries should restrict themselves to issuing log messages, but let the application do all logging configuration (setting up handlers, changing logger levels, and the like). There’s lots written about this elsewhere, but here’s one relevant blog post: http://pieces.openpolitics.com/2012/04/python-logging-best-practices/

In practice, this caused a hard-to-diagnose bug in our IPython- and sklearn-using application (actually, in more than one such application):

  • At application start time, we start an IPython kernel. That kernel swaps out sys.stdout and sys.stderr for its own custom streams, which rely on a lot of fairly complicated machinery (extra threads, ZMQ streams, the asyncio event loop, etc.)
  • sklearn was imported while that IPython kernel was running.
  • The log handler created at import time then picked up IPython’s custom sys.stderr stream instead of the usual one.
  • At application stop time, the IPython kernel and associated machinery were stopped.
  • At process exit time, the stream associated to the handler was flushed (by the logging module’s shutdown function, which is registered as an atexit handler). Because the IPython machinery was no longer active, we got a hard-to-understand traceback.

If the intent of the handler is to suppress the “No logger configured …” messages from the std. lib., perhaps a logging.NullHandler could be used for that purpose instead? I’m happy to create a PR for this if the proposed change sounds acceptable.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jnothmancommented, Apr 27, 2020

I think we should avoid neglecting this for more releases. This bad-citizen code was inserted accidentally.

0reactions
thomasjpfancommented, May 4, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Logging HOWTO — Python 3.11.1 documentation
If the parent has no explicit level set, its parent is examined, and so on - all ancestors are searched until an explicitly...
Read more >
beyond top level package error in relative import
It's because python doesn't record where a package was loaded from. So when you do python -m test_A.test , it basically just discards...
Read more >
Python Logging: A Stroll Through the Source Code
In this step-by-step tutorial, you'll learn about how the Python logging package is designed from an OOP perspective. You'll walk line by line...
Read more >
Understanding Python imports, __init__.py and pythonpath
In short, don't be tempted to club imports os , import sys , and import length all at the top of the script...
Read more >
Dead Simple Python: Project Structure and Imports
Here's the good news: you don't have to be one of them! ... If you do forget __init__.py in your package, it's going...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found