This is a rough'n'ready experiment to see if it's feasible to detect shadowed modules on import. A shadowed module is one where a user has inadvertently named a module the same as a module on the standard search path (a common example in education is "turtle.py" and my own unfortunately named "picamera.py" :).
If this module is imported, it tweaks the standard import machinery to check for such shadowing
the first time a module is imported. If it finds any, it raises ShadowWarning
(which descends
from ImportWarning
) giving the path of the imported module, and the path of the first module
it is shadowing. For example:
$ touch turtle.py
$ touch numpy.py
$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shadow
>>> import turtle
/home/dave/shadow.py:35: ShadowWarning: /home/dave/turtle.py shadows /usr/lib/python3.5/turtle.py
result.origin, conflict.origin)))
>>> import numpy
/home/dave/shadow.py:35: ShadowWarning: /home/dave/numpy.py shadows /usr/lib/python3/dist-packages/numpy/__init__.py
result.origin, conflict.origin)))
>>>
$ rm turtle.py
$ rm numpy.py
I should add that this will only work under Python 3. In fact, I think it'll only work under Python 3.5 and above (some bits of the import machinery changed in 3.5 and those are the bits I've overridden, though it could probably be refined to work in earlier versions too).
Another important note is that this will slow down import times (obviously) as every first time import will require additional disk accesses to check for shadow conflicts. Therefore I wouldn't advocate this as a general solution but it might interesting to add to systems geared towards education, e.g. the Mu Editor?