<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# Make sure that DeprecationWarning within this package always gets printed
warnings.filterwarnings('always', category=DeprecationWarning,
module=r'^{0}\.'.format(re.escape(__name__)))"><pre class="notranslate"><code class="notranslate"># Make sure that DeprecationWarning within this package always gets printed
warnings.filterwarnings('always', category=DeprecationWarning,
module=r'^{0}\.'.format(re.escape(__name__)))
</code></pre></div>
<p dir="auto">via <a href="https://github.com/scikit-learn/scikit-learn/blob/b21f9b4f98e6997bd0bc6ec5e89df74989b5ece2/sklearn/__init__.py#L28"><code class="notranslate">__init__.py</code></a></p>
<p dir="auto">This is really quite bad behavior. If you feel like you really must do this, <a href="https://docs.python.org/3/library/warnings.html#warnings.filterwarnings" rel="nofollow">please use <code class="notranslate">append=True</code></a></p>
<h4 dir="auto">Description</h4>
<p dir="auto">Importing sklearn changes the current warning filters, making it very difficult to debug warnings using standard Python syntax, e.g <code class="notranslate">-W</code> command flag.</p>
<h4 dir="auto">Steps/Code to Reproduce</h4>
<p dir="auto">The module <code class="notranslate">sklearn.cross_validation</code> is deprecated, and importing it results in a warning.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python3.6 -c 'import mypackage'
/usr/local/lib/python3.6/dist-packages/sklearn/cross_validation.py:41: DeprecationWarning:
This module was deprecated in version 0.18 in favor of the model_selection module into
which all the refactored classes and functions are moved. Also note that the interface of
the new CV iterators are different from that of this module. This module will be removed
in 0.20.
  &quot;This module will be removed in 0.20.&quot;, DeprecationWarning)"><pre class="notranslate"><code class="notranslate">$ python3.6 -c 'import mypackage'
/usr/local/lib/python3.6/dist-packages/sklearn/cross_validation.py:41: DeprecationWarning:
This module was deprecated in version 0.18 in favor of the model_selection module into
which all the refactored classes and functions are moved. Also note that the interface of
the new CV iterators are different from that of this module. This module will be removed
in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
</code></pre></div>
<p dir="auto">Okay, I'm surprised to see a DeprecationWarning — that's not supposed to happen in Python unless I ask to see it — but let me try to get a stack trace so I can see where it's coming from.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python3.6 -W error::DeprecationWarning:: -c 'import mypackage'
/usr/local/lib/python3.6/dist-packages/sklearn/cross_validation.py:41: DeprecationWarning:
This module was deprecated in version 0.18 in favor of the model_selection module into
which all the refactored classes and functions are moved. Also note that the interface of
the new CV iterators are different from that of this module. This module will be removed
in 0.20.
  &quot;This module will be removed in 0.20.&quot;, DeprecationWarning)"><pre class="notranslate"><code class="notranslate">$ python3.6 -W error::DeprecationWarning:: -c 'import mypackage'
/usr/local/lib/python3.6/dist-packages/sklearn/cross_validation.py:41: DeprecationWarning:
This module was deprecated in version 0.18 in favor of the model_selection module into
which all the refactored classes and functions are moved. Also note that the interface of
the new CV iterators are different from that of this module. This module will be removed
in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
</code></pre></div>
<p dir="auto">Oh that's weird, the <code class="notranslate">-W error::DeprecationWarning::</code> should raise the warning an exception, which would show me a full stack trace and tell me where <code class="notranslate">sklearn.cross_validation</code> is being imported from. Why does the configuration flag have no effect?</p>
<p dir="auto">I tried playing with the flags for a while, then eventually realized that it is not possible to change sklearn's warning filter, <em>because it is hardcoded in the module!</em></p>
<h4 dir="auto">Expected Results</h4>
<ol dir="auto">
<li>At the very least, honor the user's warning filters and don't change the warning filter to something that the user didn't ask for.</li>
<li>Ideally, don't configure warning filters at all. This behavior should be controlled by an application or an end user, not a library.</li>
</ol>
<p dir="auto">If the <code class="notranslate">warnings.filterwarnings(…)</code> is removed completely, then the result would be expected Python behavior and I would be able to debug this very quickly:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python3.6 -W error::DeprecationWarning:: -c 'import mypackage'
Traceback (most recent call last):
  File &quot;&lt;string&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/usr/local/lib/python3.6/dist-packages/mypackage/__init__.py&quot;, line 5, in &lt;module&gt;
    from .classifiers import (
  File &quot;/usr/local/lib/python3.6/dist-packages/formasaurus/classifiers.py&quot;, line 8, in &lt;module&gt;
    from mypackage import formtype_model, mypackage
  File &quot;/usr/local/lib/python3.6/dist-packages/mypackage/formtype_model.py&quot;, line 9, in &lt;module&gt;
    from mypackage.annotation import get_annotation_folds
  File &quot;/usr/local/lib/python3.6/dist-packages/mypackage/annotation.py&quot;, line 5, in &lt;module&gt;
    from sklearn.cross_validation import LabelKFold
  File &quot;/usr/local/lib/python3.6/dist-packages/sklearn/cross_validation.py&quot;, line 41, in &lt;module&gt;
    &quot;This module will be removed in 0.20.&quot;, DeprecationWarning)
DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20."><pre class="notranslate"><code class="notranslate">$ python3.6 -W error::DeprecationWarning:: -c 'import mypackage'
Traceback (most recent call last):
  File "&lt;string&gt;", line 1, in &lt;module&gt;
  File "/usr/local/lib/python3.6/dist-packages/mypackage/__init__.py", line 5, in &lt;module&gt;
    from .classifiers import (
  File "/usr/local/lib/python3.6/dist-packages/formasaurus/classifiers.py", line 8, in &lt;module&gt;
    from mypackage import formtype_model, mypackage
  File "/usr/local/lib/python3.6/dist-packages/mypackage/formtype_model.py", line 9, in &lt;module&gt;
    from mypackage.annotation import get_annotation_folds
  File "/usr/local/lib/python3.6/dist-packages/mypackage/annotation.py", line 5, in &lt;module&gt;
    from sklearn.cross_validation import LabelKFold
  File "/usr/local/lib/python3.6/dist-packages/sklearn/cross_validation.py", line 41, in &lt;module&gt;
    "This module will be removed in 0.20.", DeprecationWarning)
DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
</code></pre></div>
<p dir="auto"><code class="notranslate">sklearn</code> is not special enough to break with standard Python conventions.</p>