<p dir="auto">It seems that importing <code class="notranslate">scipy.optimize</code> adds at least <code class="notranslate">"constants"</code> to <code class="notranslate">sys.modules</code> which will cause overshadowing of a local <code class="notranslate">constants.py</code> further up <code class="notranslate">sys.path</code> if imported after <code class="notranslate">scipy.optimize</code> (in this case it was imported by sklearn).</p>
<p dir="auto">I did not try/think it through, but I guess that the <code class="notranslate">setup.py</code> is incorrect and:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    ext = config.add_extension(
        'constants',
        sources=['cython/src/constants.cxx'],
        include_dirs=[
            'cython/src/',
            'src/',
            'src/io/',
            'src/lp_data/',
            'src/simplex/',
        ],"><pre class="notranslate"><code class="notranslate">    ext = config.add_extension(
        'constants',
        sources=['cython/src/constants.cxx'],
        include_dirs=[
            'cython/src/',
            'src/',
            'src/io/',
            'src/lp_data/',
            'src/simplex/',
        ],
</code></pre></div>
<p dir="auto">should not be <code class="notranslate">'constants'</code> but rather <code class="notranslate">'scipy.optimize._highs.constants'</code>?</p>
<h4 dir="auto">Reproducing code example:</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Python 3.8.6 (default, Sep 25 2020, 09:36:53) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import sys

In [2]: &quot;constants&quot; in sys.modules.keys()  # as expected, not already imported
Out[2]: False

In [3]: from scipy import optimize

In [4]: sys.modules[&quot;constants&quot;]  # Now it is the scipy module!
Out[4]: &lt;module 'scipy.optimize._highs.constants' from '.../python3.8/site-packages/scipy/optimize/_highs/constants.cpython-38-x86_64-linux-gnu.so'&gt;

In [5]: import scipy

In [6]: scipy.__version__
Out[6]: '1.6.0'"><pre class="notranslate"><code class="notranslate">Python 3.8.6 (default, Sep 25 2020, 09:36:53) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import sys

In [2]: "constants" in sys.modules.keys()  # as expected, not already imported
Out[2]: False

In [3]: from scipy import optimize

In [4]: sys.modules["constants"]  # Now it is the scipy module!
Out[4]: &lt;module 'scipy.optimize._highs.constants' from '.../python3.8/site-packages/scipy/optimize/_highs/constants.cpython-38-x86_64-linux-gnu.so'&gt;

In [5]: import scipy

In [6]: scipy.__version__
Out[6]: '1.6.0'
</code></pre></div>
<p dir="auto">If you have a module named <code class="notranslate">constants</code> in your project, then if you try to use that module while at the same time using scipy.signal (and it seems any module that uses scipy.optimize), you'll get an exception.  It seems to be related to <a href="https://github.com/scipy/scipy/blame/master/scipy/optimize/_highs/cython/src/constants.pyx">https://github.com/scipy/scipy/blame/master/scipy/optimize/_highs/cython/src/constants.pyx</a></p>
<h4 dir="auto">Reproducing code example:</h4>

<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#!/bin/sh

# Don't run this in a place where you have an existing file named `constants.py`
echo &quot;MY_IMPORTANT_CONSTANT = 1&quot; &gt; constants.py
python3 -c &quot;from scipy import signal; import constants; print(constants.MY_IMPORTANT_CONSTANT)&quot;
rm constants.py"><pre class="notranslate"><span class="pl-c"><span class="pl-c">#!</span>/bin/sh</span>

<span class="pl-c"><span class="pl-c">#</span> Don't run this in a place where you have an existing file named `constants.py`</span>
<span class="pl-c1">echo</span> <span class="pl-s"><span class="pl-pds">"</span>MY_IMPORTANT_CONSTANT = 1<span class="pl-pds">"</span></span> <span class="pl-k">&gt;</span> constants.py
python3 -c <span class="pl-s"><span class="pl-pds">"</span>from scipy import signal; import constants; print(constants.MY_IMPORTANT_CONSTANT)<span class="pl-pds">"</span></span>
rm constants.py</pre></div>
<h4 dir="auto">Error message:</h4>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;&lt;string&gt;&quot;, line 3, in &lt;module&gt;
AttributeError: module 'scipy.optimize._highs.constants' has no attribute 'MY_IMPORTANT_CONSTANT'"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "&lt;string&gt;", line 3, in &lt;module&gt;
AttributeError: module 'scipy.optimize._highs.constants' has no attribute 'MY_IMPORTANT_CONSTANT'
</code></pre></div>
<h4 dir="auto">Scipy/Numpy/Python version information:</h4>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1.6.0 1.19.4 sys.version_info(major=3, minor=9, micro=1, releaselevel='final', serial=0)"><pre class="notranslate"><code class="notranslate">1.6.0 1.19.4 sys.version_info(major=3, minor=9, micro=1, releaselevel='final', serial=0)
</code></pre></div>