Skip to content

ImportWarning: can't resolve package from __spec__ or __package__ #3061

@blueyed

Description

@blueyed

Given the following directory structuce:

.
└── tests
    ├── conftest.py
    ├── __init__.py
    ├── relative.py
    └── test_foo.py

With tests/conftest.py:

from .relative import relative

print("conftest")

tests/relative.py:

relative = 1

tests/test_foo.py:

def test_foo():
    pass

Running PYTHONWARNINGS=all pytest tests -s shows:

…/pyenv/tmp-system-ImportWarning-ZniFiE/lib/python3.6/site.py:165: DeprecationWarning: 'U' mode is deprecated
  f = open(fullname, "rU")
…/Vcs/pytest/_pytest/assertion/rewrite.py:7: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
…/Vcs/pytest/ImportWarning/tests/conftest.py:1: ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__
  from .relative import relative
conftest
==================================== test session starts ====================================
platform linux -- Python 3.6.3, pytest-3.3.2.dev14+gd3b54d03.d20171225, py-1.5.2, pluggy-0.6.0
rootdir: …/Vcs/pytest, inifile: tox.ini
collected 1 item                                                                            

tests/test_foo.py .                                                                   [100%]

================================= 1 passed in 0.01 seconds ==================================

The ImportWarning is:

tests/conftest.py:1: ImportWarning: can't resolve package from spec or package, falling back on name and path
from .relative import relative

It only shows up when using -s.

This came up for pytest-django.
See pytest-dev/pytest-django#546 (comment).

Defining __package__ explicitly fixes it, but it looks like this should not be necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: debuggingrelated to the debugging builtin pluginplugin: warningsrelated to the warnings builtin plugintopic: collectionrelated to the collection phase

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions