[Python-ideas] Prevent importing yourself?

Oscar Benjamin oscar.j.benjamin at gmail.com
Fri Jan 29 19:29:48 EST 2016


On 30 January 2016 at 00:16, Andrew Barnert via Python-ideas
<python-ideas at python.org> wrote:
>> Maybe a suggestion would be to add the path of the module to the error
>> message?
>
> That would probably help, but think about what it entails:
>
> Most AttributeErrors aren't on module objects, they're on instances of user-defined classes with a typo, or on None because the user forgot a "return" somewhere, or on str because the user didn't realize the difference between the string representation of an object and the objects, etc.

Oh yeah, good point. Somehow I read the AttributeError as an ImportError e.g.

$ python random.py
Traceback (most recent call last):
  File "random.py", line 1, in <module>
    import urllib2
  File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
    import httplib
  File "/usr/lib/python2.7/httplib.py", line 80, in <module>
    import mimetools
  File "/usr/lib/python2.7/mimetools.py", line 6, in <module>
    import tempfile
  File "/usr/lib/python2.7/tempfile.py", line 35, in <module>
    from random import Random as _Random
ImportError: cannot import name Random

That error message could be changed to something like

ImportError: cannot import name Random from module 'random'
(/home/oscar/random.py)

Attribute errors would be more problematic.

--
Oscar


More information about the Python-ideas mailing list