This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Naming a file` io.py` causes cryptic error message
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.5, Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: docs@python Nosy List: brett.cannon, ctheune, docs@python, ezio.melotti, flox, jcea, loewis, madison.may, ncoghlan, ned.deily, r.david.murray, twouters, vstinner
Priority: normal Keywords: patch

Created on 2014-04-11 18:05 by madison.may, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
4ae151db1bd9.diff ctheune, 2014-04-16 23:15 review
Repositories containing patches
https://bitbucket.org/ctheune/cpython#3.4-21202
Messages (7)
msg215940 - (view) Author: Madison May (madison.may) * Date: 2014-04-11 18:05
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.
msg215946 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2014-04-11 18:58
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/import_traps.html#the-name-shadowing-trap.  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.
msg215947 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2014-04-11 19:01
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).
msg215952 - (view) Author: Madison May (madison.may) * Date: 2014-04-11 20:03
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.
msg216001 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2014-04-13 08:50
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).
msg216619 - (view) Author: Christian Theune (ctheune) * Date: 2014-04-16 23:15
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.
msg342546 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-05-15 03:03
No activity for 5 years, I close the issue.
History
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65401
2019-05-15 03:03:55vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg342546

resolution: out of date
stage: test needed -> resolved
2014-07-01 03:15:07ezio.melottisetstage: test needed
2014-04-18 01:55:57jceasetnosy: + jcea
2014-04-16 23:15:23ctheunesetfiles: + 4ae151db1bd9.diff
keywords: + patch
2014-04-16 23:15:06ctheunesetnosy: + ctheune, r.david.murray, twouters
messages: + msg216619
2014-04-16 22:49:00ctheunesethgrepos: + hgrepo238
2014-04-13 08:50:51loewissetnosy: + loewis
messages: + msg216001
2014-04-12 09:58:57floxsetnosy: + flox
2014-04-11 20:03:17madison.maysetmessages: + msg215952
2014-04-11 19:07:01ezio.melottisetnosy: + ncoghlan, ezio.melotti
type: behavior
2014-04-11 19:01:04brett.cannonsetnosy: + brett.cannon
messages: + msg215947
2014-04-11 18:58:02ned.deilysetnosy: + ned.deily
messages: + msg215946
components: - IO
2014-04-11 18:06:04madison.maysetversions: + Python 3.5
2014-04-11 18:05:51madison.maycreate