[issue21202] Naming a file` io.py` causes cryptic error message

New submission from Madison May:
Naming a file `io.py` in the root directory of a project causes the following error on 2.7:
AttributeError: 'module' object has no attribute 'BufferedIOBase'
Similar issues arise on 3.x., although the error message is a bit more useful:
Fatal Python error: Py_Initialize: can't initialize sys standard streams AttributeError: 'module' object has no attribute 'OpenWrapper'
At the very least we should ensure that the error message is a bit more straightforward and clear, as I imagine its not all that uncommon to cause this kind of conflict.
---------- assignee: docs@python components: Documentation, IO messages: 215940 nosy: docs@python, madison.may priority: normal severity: normal status: open title: Naming a file` io.py` causes cryptic error message versions: Python 2.7
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

Changes by Madison May madison.may@students.olin.edu:
---------- versions: +Python 3.5
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

Ned Deily added the comment:
Using a local module name that shadows one in the standard library is a very common "import trap". See, for example, https://ncoghlan_devs-python-notes.readthedocs.org/en/latest/python_concepts.... I did a quick search through the documentation and didn't find an explicit reference to the general topic. If it isn't already, it probably should be mentioned in the tutorial and the FAQ. I don't think that 'io' should be special-cased.
---------- components: -IO nosy: +ned.deily
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

Brett Cannon added the comment:
While mentioning something in the FAQ and/or tutorial is fine, I wouldn't want to change Python's message too much to deal with this unless it was extremely fast (e.g. we pre-generated a set and check that on ImportError and then modified the message only in those instances).
---------- nosy: +brett.cannon
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

Changes by Ezio Melotti ezio.melotti@gmail.com:
---------- nosy: +ezio.melotti, ncoghlan type: -> behavior
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

Madison May added the comment:
I definitely agree that io shouldn't be special cased, as it's more about the name shadowing issue that this specific example.
A simple docs addition would make me happy, to be honest.
----------
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

Changes by Florent Xicluna florent.xicluna@gmail.com:
---------- nosy: +flox
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

Martin v. Löwis added the comment:
I think the AttributeError message should improve, and special-case certain common types, in particular modules. E.g. it could read
AttributeError: module 'io' has no attribute 'BufferedIOBase'
or even
AttributeError: <module 'io' from 'C:\Program Files\Python34\lib\io.py'> has no attribute 'BufferedIOBase'
IIUC, the first version would already have helped the OP, and the second version would have been dead-clear (except that it is a little verbose).
---------- nosy: +loewis
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

Changes by Christian Theune ct@gocept.com:
---------- hgrepos: +238
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

Christian Theune added the comment:
I managed to create a patch that relies (in hopefully reasonably safe manner) on embedding an object repr for identification in this and similar cases. This is basically what implements what Martin suggested.
Caveat emptor: my C knowledge is only good enough to be dangerous. Thomas Wouters and RDM helped me through it.
I haven't adapted the tests yet (lots of failures due to the output change) but I managed them to stop crashing.
The 'repr.py' in the root is my current testbed to see what's going on.
---------- nosy: +ctheune, r.david.murray, twouters
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

Changes by Christian Theune ct@gocept.com:
---------- keywords: +patch Added file: http://bugs.python.org/file34937/4ae151db1bd9.diff
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

Changes by Jesús Cea Avión jcea@jcea.es:
---------- nosy: +jcea
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

Changes by Ezio Melotti ezio.melotti@gmail.com:
---------- stage: -> test needed
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue21202 _______________________________________

STINNER Victor vstinner@redhat.com added the comment:
No activity for 5 years, I close the issue.
---------- nosy: +vstinner resolution: -> out of date stage: test needed -> resolved status: open -> closed
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue21202 _______________________________________
participants (9)
-
Brett Cannon
-
Christian Theune
-
Ezio Melotti
-
Florent Xicluna
-
Jesús Cea Avión
-
Madison May
-
Martin v. Löwis
-
Ned Deily
-
STINNER Victor