Repository: numba/numba

Issue:
Numba namespace package in ipython
- [x] I am using the latest released version of Numba (most recent is visible in
 the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [ ] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

---

I'm seeing this very odd behaviour:
```
(gammapy-dev) hfm-1804a:tmp deil$ ipython
Python 3.7.0 | packaged by conda-forge | (default, Nov 12 2018, 12:34:36) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.3.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import numba                                                                                                                                                                                        

In [2]: numba.jit                                                                                                                                                                                           
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-509e32de4401> in <module>
----> 1 numba.jit

AttributeError: module 'numba' has no attribute 'jit'

In [3]: numba                                                                                                                                                                                               
Out[3]: <module 'numba' (namespace)>

In [4]: numba.__path__                                                                                                                                                                                      
Out[4]: _NamespacePath(['/Users/deil/.ipython/numba'])

In [5]:                                                                                                                                                                                                     
Do you really want to exit ([y]/n)? 
(gammapy-dev) hfm-1804a:tmp deil$ ls -lh /Users/deil/.ipython/numba
total 24
-rw-r--r--  1 deil  staff   5.4K Apr 20 11:30 ipython-input-4-f982ea8710c3.f-fc3bc4f3ad.py37m.1.nbc
-rw-r--r--  1 deil  staff   961B Apr 20 11:30 ipython-input-4-f982ea8710c3.f-fc3bc4f3ad.py37m.nbi
```

This occurs in ipython and Jupyter when I `import numba` (but not with regular Python), and only in conda environments where I don't have `numba` installed.

It looks like what is happening is that two weeks ago for some reason Numba cache files were put in `~/.ipython/numba`, and now ipython and Jupyter think that `~/.ipython/numba` is a Python namespace package?

In case others run into the same issue - the solution is to remove that folder and then one gets the normal and clear `ModuleNotFoundError`:
```
(gammapy-dev) hfm-1804a:tmp deil$ rm -r ~/.ipython/numba
(gammapy-dev) hfm-1804a:tmp deil$ ipython
Python 3.7.0 | packaged by conda-forge | (default, Nov 12 2018, 12:34:36) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.3.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import numba                                                                                                                                                                                        
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-68d4f1bcefa7> in <module>
----> 1 import numba

ModuleNotFoundError: No module named 'numba'
```

Can anyone reproduce the issue, i.e. should numba cache files ever go in `~/.ipython/numba` ?

If yes, probably that should be avoided?

If no, please just close this issue directly - not sure how this happened. I was just playing around with numba to learn how it works - I don't really have a need to use the caching functionality at all.


Discussion:
Thanks for the report. The `ipython` cache dir is that obtained via:
https://github.com/numba/numba/blob/1de3cd5a687011d90b1a26070c6fe47abdf8a2a5/numba/caching.py#L288-L293

I'm not sure what these default to or how their behaviour has changed in time. Right now on IPython 7.2.0 I have an IPython cache dir of `/home/user/.cache/ipython`. I guess it's possible to create the situation you encountered above (as you encountered it!) probably by mixing a combination of different versions of ipython with Numba caching, and then with Numba not being present. I think this results in a hard to reproduce but evidently undesirable behaviour. I think the fix is however, simple, https://github.com/numba/numba/blob/1de3cd5a687011d90b1a26070c6fe47abdf8a2a5/numba/caching.py#L293 need to replace `'numba'` with e.g. `'numba_cache'` or similar so as to make it so that it would not be imported as a module namespace in place of the actual package.

Resolving patch:
diff --git a/numba/caching.py b/numba/caching.py
--- a/numba/caching.py
+++ b/numba/caching.py
@@ -290,7 +290,7 @@ def get_cache_path(self):
         except ImportError:
             # older IPython version
             from IPython.utils.path import get_ipython_cache_dir
-        return os.path.join(get_ipython_cache_dir(), 'numba')
+        return os.path.join(get_ipython_cache_dir(), 'numba_cache')
 
     def get_source_stamp(self):
         return hashlib.sha256(self._bytes_source).hexdigest()


Test patch: