Author xiang.zhang
Recipients brett.cannon, eric.snow, ncoghlan, xiang.zhang
Date 2018-01-02.08:48:06
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1514882888.25.0.467229070634.issue32479@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2018-01-02 08:48:08xiang.zhangsetrecipients: + xiang.zhang, brett.cannon, ncoghlan, eric.snow
2018-01-02 08:48:08xiang.zhangsetmessageid: <1514882888.25.0.467229070634.issue32479@psf.upfronthosting.co.za>
2018-01-02 08:48:08xiang.zhanglinkissue32479 messages
2018-01-02 08:48:06xiang.zhangcreate