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

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

Open
jaraco opened this issue Sep 10, 2016 · 4 comments
Open
Labels
plugin: doctests related to the doctests builtin plugin type: enhancement new feature or API change, should be merged into features branch

Comments

@jaraco
Copy link
Contributor

jaraco commented Sep 10, 2016

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.

jaraco added a commit to pmxbot/pmxbot.rss that referenced this issue Sep 10, 2016
@RonnyPfannschmidt
Copy link
Member

Pytest is currently unaware of pep404
There already is an issue about that

I can't search for it on mobile

@Mortal
Copy link

Mortal commented Nov 29, 2016

I just made https://github.com/Mortal/doctestmod to work around this problem.

@nicoddemus
Copy link
Member

Thanks @Mortal, that can be used as basis for a solution in pytest!

@nicoddemus nicoddemus added plugin: doctests related to the doctests builtin plugin type: enhancement new feature or API change, should be merged into features branch labels Sep 29, 2017
@asottile
Copy link
Member

asottile commented Jul 8, 2018

the message has changed but still errors:

_________________________ ERROR collecting pkg/mod1.py _________________________
pkg/mod1.py:1: in <module>
    from . import mod2
E   ImportError: attempted relative import with no known parent package
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.09 seconds ============================

ttung pushed a commit to ttung/napari that referenced this issue Jan 27, 2020
I considered making all the tests namespace packages, but it turns out pytest [chokes](pytest-dev/pytest#3396) [on](pytest-dev/pytest#1927) [namespace packages](pytest-dev/pytest#478).  So this is the best solution for now.

Test plan: made a distribution (python setup.py sdist) then installed it in a separate virtualenv.  then ran tests (`pytest --pyargs napari`).
ttung pushed a commit to ttung/napari that referenced this issue Jan 27, 2020
I considered making all the tests namespace packages, but it turns out pytest [chokes](pytest-dev/pytest#3396) [on](pytest-dev/pytest#1927) [namespace packages](pytest-dev/pytest#478).  So this is the best solution for now.

Test plan: made a distribution (python setup.py sdist) then installed it in a separate virtualenv.  then ran tests (`pytest --pyargs napari`).

Fixes napari#875
ttung pushed a commit to ttung/napari that referenced this issue Jan 27, 2020
I considered making all the tests namespace packages, but it turns out pytest [chokes](pytest-dev/pytest#3396) [on](pytest-dev/pytest#1927) [namespace packages](pytest-dev/pytest#478).  So this is the best solution for now.

Test plan: made a distribution (python setup.py sdist) then installed it in a separate virtualenv.  then ran tests (`pytest --pyargs napari`).

Fixes napari#875
ttung pushed a commit to ttung/napari that referenced this issue Jan 28, 2020
I considered making all the tests namespace packages, but it turns out pytest [chokes](pytest-dev/pytest#3396) [on](pytest-dev/pytest#1927) [namespace packages](pytest-dev/pytest#478).  So this is the best solution for now.

Test plan: made a distribution (python setup.py sdist) then installed it in a separate virtualenv.  then ran tests (`pytest --pyargs napari`).

Fixes napari#875
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: doctests related to the doctests builtin plugin type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

No branches or pull requests

5 participants