[New-bugs-announce] [issue19211] from relative_module import seems to import wrong module

Ethan Glasser-Camp report at bugs.python.org
Wed Oct 9 20:18:04 CEST 2013


New submission from Ethan Glasser-Camp:

I have a library lib. lib/__init__.py does the following:

    from .subdir import a
    from . import a
    import sys

    print(sys.modules['lib.a'])

This code fails with a KeyError. It seems that despite the second line, lib.a does not get imported. Printing a.__name__ shows lib.subdir.a. Commenting out the first line makes the code work fine, as does moving the code from lib/__init__.py to lib/api.py. This happens under both Python 2.7 and Python 3.3.

Here is my sys.path, sys.meta_path, and sys.path_hooks under Python 2.7:

(['/home/ethan/tmp/import-madness', '/home/ethan/tmp/software/notmuch/bindings/python', '/home/ethan/local/lib/python2.7/site-packages', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/ethan/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7'], [], [<type 'zipimport.zipimporter'>])

The same under Python 3:

['/home/ethan/tmp/import-madness', '/home/ethan/local/lib/python2.7/site-packages', '/usr/lib/python3.3', '/usr/lib/python3.3/plat-x86_64-linux-gnu', '/usr/lib/python3.3/lib-dynload', '/usr/local/lib/python3.3/dist-packages', '/usr/lib/python3/dist-packages'] [<class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib.PathFinder'>] [<class 'zipimport.zipimporter'>, <function FileFinder.path_hook.<locals>.path_hook_for_FileFinder at 0x7f0728babdd0>]

----------
components: Interpreter Core
files: __init__.py
messages: 199320
nosy: ethan.glasser.camp
priority: normal
severity: normal
status: open
title: from relative_module import seems to import wrong module
type: behavior
versions: Python 2.7, Python 3.3
Added file: http://bugs.python.org/file32022/__init__.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19211>
_______________________________________


More information about the New-bugs-announce mailing list