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: Python 3.5.2 "from ... import" statement is different from official documentation
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: josh.r, woo yoo
Priority: normal Keywords:

Created on 2016-11-19 05:56 by woo yoo, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
捕获.PNG woo yoo, 2016-11-19 05:56 The semantics of the statement described by official documentation
Messages (6)
msg281202 - (view) Author: woo yoo (woo yoo) Date: 2016-11-19 05:56
I've experiment with the statement,however the result did not match the official description.
I created a namespace package named l007,  within which submodule named l009 was placed.I typed "from l007 import l009" in the interpreter, the execution was ok, while in which case a ImportError should have been raised.
Is my understanding wrong?
msg281203 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-11-19 06:05
Please don't post screen shots of text. Copy and paste the text into the bug report. Some people (those who are blind, visually impaired or using a screen-reader for some other reason) cannot see the screen shot, and even those who can prefer to deal with text that can be copied and pasted, not pixels.

Please show (in text, not a picture) the layout and contents of your package, the exact Python code you used, the result you expected, and the result you actually got. If an import succeeded that you expected to fail, please print module.__file__ to ensure that you have imported the module you expected to import.

Ideally anyone (fully sighted or not) should be able to copy and paste your code into the Python interpreter and determine whether or not they get the same results as you.

Thank you.
msg281204 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-11-19 06:07
If you're quoting from the docs, its a good idea to give the URL to *which* documentation you are reading, not just a copy of the text.
msg281205 - (view) Author: woo yoo (woo yoo) Date: 2016-11-19 06:25
The link associated with the documentation is https://docs.python.org/3/reference/simple_stmts.html#import.

My package layout is :
  --l007
    --l009.py
My code is :
  >from l007 import l009
The excution was ok, which was not the case i had expected.
msg281206 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2016-11-19 06:52
Why is this unexpected? Per the docs, the process is:

    find the module specified in the from clause, loading and initializing it if necessary;
    for each of the identifiers specified in the import clauses:
        check if the imported module has an attribute by that name
***     if not, attempt to import a submodule with that name and then check the imported module again for that attribute
        if the attribute is not found, ImportError is raised.
        otherwise, a reference to that value is stored in the local namespace, using the name in the as clause if it is present, otherwise using the attribute name

The *** is next to where it ends up in the tree; it found the l007 package, determined it had no attribute named l009, determined it did have a module of that name, and imported it. What were you expecting? For the record, it would be nice if you'd used a name that didn't begin with a lowercase L; it makes it look like the module is named entirely with digits (which would be illegal).
msg281208 - (view) Author: woo yoo (woo yoo) Date: 2016-11-19 07:27
Thanks for your advice.
History
Date User Action Args
2022-04-11 14:58:39adminsetgithub: 72931
2016-11-19 11:16:30eric.smithsetstatus: open -> closed
resolution: not a bug
stage: resolved
2016-11-19 07:27:09woo yoosetnosy: - steven.daprano, docs@python
messages: + msg281208
2016-11-19 06:52:25josh.rsetnosy: + josh.r
messages: + msg281206
2016-11-19 06:25:13woo yoosetmessages: + msg281205
2016-11-19 06:07:48steven.dapranosetmessages: + msg281204
2016-11-19 06:05:32steven.dapranosetnosy: + steven.daprano
messages: + msg281203
2016-11-19 05:56:25woo yoocreate