On 10/29/2015 5:18 PM, Laura Creighton wrote:
In a message of Thu, 29 Oct 2015 15:50:30 -0500, Ryan Gonzalez writes:
Why not just check the path of the imported modules and compare it with the 
Python library directory?

My friend Åsa who is 12 years old suggested exactly this at the club.

This was my first idea, until I realized that it would be even better to avoid shadowing in the first place.

If this works then I will be certain to mention this to her.

As far as I can tell, comparison in not foolproof, even if done carefully. This is a proper stdlib import.

>>> import string
>>> string.__file__
'C:\\Programs\\Python35\\lib\\string.py'

If we look at suffixes, the only part guaranteed, after changing Windows' '\\' to '/', is '/lib/string.py'. Now suppose someone runs python in another 'lib' directory containing string.py.

>>> import string
>>> string.__file__
'C:\\Users\\Terry\\lib\\string.py'

Same suffix.  Let's try prefixes.

>>> import os.path
>>> import sys
>>> os.path.dirname(string.__file__) in sys.path
False

This is True for the stdlib import. Hooray. But this requires more imports, which also might be shadowed. Having '' at the front of sys.path is a real nuisance when one wants to guaranteed authentic stdlib imports.

--
Terry Jan Reedy


_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to