title: python 3 - subtle import error
body: <p>I am having tricky ImportError when packaging some modules, to try and support both python 2.7 and 3.6. There is nothing inside the code that's fancy to exlude one of these versions, so I thought I would try. The repo is at <a href="https://github.com/raamana/neuropredict" rel="nofollow noreferrer">https://github.com/raamana/neuropredict</a></p>
  
  <p>It worked fine before on 2.7 alone. To support both 2.7 and 3.6, 
  After much googling and head-scracting, I <strong>thought</strong> I figured out how to do that with the following import code at the top of each of the modules in my package:</p>
  
  <pre><code>from sys import version_info
  
  if version_info.major==2 and version_info.minor==7:
      import rhst, visualize
      from freesurfer import aseg_stats_subcortical, aseg_stats_whole_brain
      import config_neuropredict as cfg
  elif version_info.major &gt; 2:
      from neuropredict import rhst, visualize
      from neuropredict.freesurfer import aseg_stats_subcortical, aseg_stats_whole_brain
      from neuropredict import config_neuropredict as cfg
  else:
      raise NotImplementedError('neuropredict supports only 2.7 or Python 3+. Upgrade to Python 3+ is recommended.')
  </code></pre>
  
  <p>The directory structure is :</p>
  
  <pre><code>$ 22:19:54 miner neuropredict &gt;&gt;  tree
  .
  ├── config_neuropredict.py
  ├── freesurfer.py
  ├── __init__.py
  ├── __main__.py
  ├── model_comparison.py
  ├── neuropredict.py
  ├── rhst.py
  ├── test_rhst.py
  └── visualize.py
  </code></pre>
  
  <p>The <strong>init</strong>.py looks like this:</p>
  
  <pre><code>__all__ = ['neuropredict', 'rhst', 'visualize', 'freesurfer',
             'config_neuropredict', 'model_comparison']
  
  from sys import version_info
  
  if version_info.major==2 and version_info.minor==7:
      import neuropredict, config_neuropredict, rhst, visualize, freesurfer, model_comparison
  elif version_info.major &gt; 2:
      from neuropredict import neuropredict, config_neuropredict, rhst, visualize, freesurfer, model_comparison
  else:
      raise NotImplementedError('neuropredict supports only 2.7 or Python 3+. Upgrade to Python 3+ is recommended.')
  </code></pre>
  
  <p>So when I run the unit tests locally or on CI, it works fine with above import mechanism.</p>
  
  <pre><code>$ 22:19:25 miner neuropredict &gt;&gt;  pytest test_rhst.py 
  =============================================================================================== test session starts ===============================================================================================
  platform linux -- Python 3.6.1, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
  rootdir: /data1/strother_lab/praamana/neuropredict, inifile:
  plugins: hypothesis-3.23.2
  collected 1 item                                                                                                                                                                                                   
  
  test_rhst.py .
  
  ============================================================================================ 1 passed in 8.73 seconds =============================================================================================
  $ 22:19:42 miner neuropredict &gt;&gt;
  </code></pre>
  
  <p>However, when I run neuropredict.py directly, it thrown an error</p>
  
  <pre><code>$ 22:19:57 miner neuropredict &gt;&gt;  python ./neuropredict.py 
  Traceback (most recent call last):
    File "neuropredict.py", line 23, in &lt;module&gt;
      from neuropredict import rhst, visualize
    File "/data1/strother_lab/praamana/neuropredict/neuropredict/neuropredict.py", line 23, in &lt;module&gt;
      from neuropredict import rhst, visualize
  ImportError: cannot import name 'rhst'
  $ 22:29:39 miner neuropredict &gt;&gt;  pwd
  /data1/strother_lab/praamana/neuropredict/neuropredict
  $ 22:29:43 miner neuropredict &gt;&gt;  
  </code></pre>
  
  <p>This is killing me - I need figure out where I am making a mistake, or if I am doing something sacrilegious.</p>
  
  <p>Question 1: why am I not able to </p>
  
  <pre><code>python ./neuropredict.py
  </code></pre>
  
  <p>and get some parser help, when the test scripts can import it successfully? This used to work in many other scenarios. Driving me crazy as I can't understand what the hell is going on.</p>
  
  <p>This is what I need to be able to achieve:</p>
  
  <ol>
  <li>import config_neuropredict.py into all other modules</li>
  <li>import config_neuropredict, rhst, visualize, freesurfer, model_comparison into the neuropredict.py module</li>
  <li>make this reliably work in Python 3+ (2.7 support not important)</li>
  <li>understand python's behaviour during Question 1 above.</li>
  </ol>
  
  <p>If you need more details or into the code, I would appreciate if you can quickly head over to this public repo: <a href="https://github.com/raamana/neuropredict" rel="nofollow noreferrer">https://github.com/raamana/neuropredict</a></p>