Skip to content

Parent module '' not loaded - relative import in PEP 420 namespace package #1927

Open
@jaraco

Description

@jaraco

When pytest is collecting packages for the purpose of doctesting modules, it can encounter an error when that module performs a relative import in a PEP-420 namespace package. Consider this trivially simple example:

$ mkdir tmp
$ cd tmp
$ mkdir -p dir1/pkg        
$ mkdir -p dir2/pkg
$ cat > dir2/pkg/mod2.py
print('import mod2')
$ cat > dir1/pkg/mod1.py
from . import mod2
$ cd dir1
$ PYTHONPATH=../dir2
$ PYTHONPATH=../dir2 python -c "import pkg.mod1"
import mod2
$ PYTHONPATH=../dir2 python -m rwt pytest -- -m pytest
Loading requirements using pytest
======================================= test session starts =======================================
platform darwin -- Python 3.5.2, pytest-3.0.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/jaraco/tmp/dir1, inifile: 
collected 0 items 

================================== no tests ran in 0.00 seconds ===================================
$ PYTHONPATH=../dir2 python -m rwt pytest -- -m pytest --doctest-modules
Loading requirements using pytest
======================================= test session starts =======================================
platform darwin -- Python 3.5.2, pytest-3.0.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/jaraco/tmp/dir1, inifile: 
collected 0 items / 1 errors 

============================================= ERRORS ==============================================
__________________________________ ERROR collecting pkg/mod1.py ___________________________________
pkg/mod1.py:1: in <module>
    from . import mod2
E   SystemError: Parent module '' not loaded, cannot perform relative import
!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================== 1 error in 0.18 seconds =====================================

The namespace package is pkg and because it has no __init__.py and because both . and ../dir2 are on sys.path, the two modules pkg.mod1 and pkg.mod2 share the namespace. As you can see, when imported naturally, the relative import works just fine. When imported for the purpose of discovering doctests, the namespace loader is not used, so __package__ isn't defined, and relative imports won't work.

That's as much as I understand right now, so I'm just registering this issue to capture my findings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: doctestsrelated to the doctests builtin plugintype: enhancementnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions