[New-bugs-announce] [issue13912] ImportError using __import__ and relative level 1

Jason R. Coombs report at bugs.python.org
Mon Jan 30 18:00:47 CET 2012

New submission from Jason R. Coombs <jaraco at jaraco.com>:

The Python 2.7.2 docs say this about __import__:

    Positive values for level indicate the number of parent directories to search relative to the directory of the module calling __import__().

But I find that even when setting level=1, the parent directory is not searched.

I've created this project tree to replicate the issue:

    jaraco at devjaraco:~$ tree master
    ├── __init__.py
    ├── pkgA
    │   ├── foo.py
    │   └── __init__.py
    └── pkgB
        ├── bar.py
        └── __init.py

    2 directories, 5 files
    jaraco at devjaraco:~$ cat master/pkgA/foo.py

    bar = __import__('pkgB', level=1).bar
    jaraco at devjaraco:~$ cat master/pkgB/bar.py
    var = "success"

It fails as so with python 2.7.2:

    jaraco at devjaraco:~$ python2.7 -c "import master.pkgA.foo"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "master/pkgA/foo.py", line 2, in <module>
        bar = __import__('pkgB', level=1).bar
    ImportError: No module named pkgB

It's conceivable I'm not using this correctly, but if so, I'm unable to find my mistake. I've confirmed that foo.__name__ is 'master.pkgA.foo'. I've tried using level=2 (in case I was off by one, but that wasn't the case).

components: Interpreter Core
messages: 152331
nosy: jason.coombs
priority: normal
severity: normal
status: open
title: ImportError using __import__ and relative level 1
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list