Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inconsistent ImportError message executing same import statement #76660

Closed
zhangyangyu opened this issue Jan 2, 2018 · 3 comments
Closed

inconsistent ImportError message executing same import statement #76660

zhangyangyu opened this issue Jan 2, 2018 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@zhangyangyu
Copy link
Member

BPO 32479
Nosy @brettcannon, @ncoghlan, @ericsnowcurrently, @zhangyangyu

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-06-08.14:26:04.412>
created_at = <Date 2018-01-02.08:48:08.133>
labels = ['interpreter-core', 'type-bug']
title = 'inconsistent ImportError message executing same import statement'
updated_at = <Date 2018-06-08.14:26:04.411>
user = 'https://github.com/zhangyangyu'

bugs.python.org fields:

activity = <Date 2018-06-08.14:26:04.411>
actor = 'xiang.zhang'
assignee = 'none'
closed = True
closed_date = <Date 2018-06-08.14:26:04.412>
closer = 'xiang.zhang'
components = ['Interpreter Core']
creation = <Date 2018-01-02.08:48:08.133>
creator = 'xiang.zhang'
dependencies = []
files = []
hgrepos = []
issue_num = 32479
keywords = []
message_count = 3.0
messages = ['309360', '309381', '309397']
nosy_count = 4.0
nosy_names = ['brett.cannon', 'ncoghlan', 'eric.snow', 'xiang.zhang']
pr_nums = []
priority = 'low'
resolution = 'wont fix'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue32479'
versions = ['Python 2.7']

@zhangyangyu
Copy link
Member Author

While debugging a problem in my job I find an odd case about import, it could be reduced to the following tracebacks:

>>> from keystone.assignment import schema
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/keystone/assignment/__init__.py", line 15, in <module>
    from keystone.assignment import controllers  # noqa
  File "/usr/lib/python2.7/site-packages/keystone/assignment/controllers.py", line 26, in <module>
    from keystone.common import controller
  File "/usr/lib/python2.7/site-packages/keystone/common/controller.py", line 24, in <module>
    from keystone.common import authorization
  File "/usr/lib/python2.7/site-packages/keystone/common/authorization.py", line 23, in <module>
    from keystone.models import token_model
  File "/usr/lib/python2.7/site-packages/keystone/models/token_model.py", line 15, in <module>
    from keystoneclient.common import cms
ImportError: No module named keystoneclient.common
>>> from keystone.assignment import schema
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/keystone/assignment/__init__.py", line 15, in <module>
    from keystone.assignment import controllers  # noqa
  File "/usr/lib/python2.7/site-packages/keystone/assignment/controllers.py", line 25, in <module>
    from keystone.assignment import schema
ImportError: cannot import name schema

keystoneclient is deliberately not installed. I think it should always report keystoneclient.common could not be found which reveals the root causes. But only the first time it does so. Later tries it always report cannot import schema.

The reason of this behaviour is that first time although keystone.assignment import fails but keystone.assignment.schema is successfully installed in sys.modules. And then, in ensure_fromlist, it calls import_submodule which returns the module in sys.modules, without set schema attribute to keystone.assignment. So it then fails in import_from for not able to get the attribute.

It could be simply reproduced by the following package structure:

test
|-- a.py
|-- b.py
`-- __init__.py

__init__.py:
from test import a

a.py:
from test import b
import modulenotexisting

b.py: #emtpy

>>> import test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test/__init__.py", line 1, in <module>
    from test import a
  File "test/a.py", line 2, in <module>
    import dddddddddd
ImportError: No module named dddddddddd
>>> import test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test/__init__.py", line 1, in <module>
    from test import a
  File "test/a.py", line 1, in <module>
    from test import b
ImportError: cannot import name b

Python3 doesn't suffer this problem, at least for 3.7.

I don't know this should be identified as a bug or not. But this does bring me trouble debugging problems and lead to confusions because it doesn't tell users the real module can't be imported.

@zhangyangyu zhangyangyu added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Jan 2, 2018
@ncoghlan
Copy link
Contributor

ncoghlan commented Jan 2, 2018

Since the problem is specific to Python 2.7 and has been resolved in the importlib based import implementation, I'm inclined to close this as "Won't Fix".

The only reason I haven't is that if someone really wanted to dig into the Python 2.7 import implementation and figure out how to improve the situation, I'd actually be willing to review the patch.

Alternatively, it might be worthwhile for OpenStack to investigate migrating over to importlib2 for their Python 2.7 support (or at least supporting it as a debugging option).

@zhangyangyu
Copy link
Member Author

It's fine. Let's keep this issue open for some time before close it.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants