Spurious ImportWarnings on 3.6+
See original GitHub issuehttp://bugs.python.org/issue25791 introduced a new warning when an import is attempted with neither __package__
nor __spec__
defined in the current globals dict. This warning now appears with many Cython-compiled extension modules (such as Pandas or multidict).
Diagnosing this is a bit tedious, but Andrew Svetlov’s multidict package (https://github.com/aio-libs/multidict/issues/79) is a good starting point. Apparently the warning isn’t displayed when importing the Cythonized module itself but when importing another module from the Cython module’s top-level. Case-in-point:
$ python -We -c "import multidict"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/antoine/multidict/multidict/__init__.py", line 24, in <module>
__import__('_multidict', globals=globals(), level=1)
File "multidict/_multidict.pyx", line 1, in init multidict._multidict (multidict/_multidict.c:14817)
import sys
ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__
If I move around the “import sys” line in _multidict.pyx
and re-compile the package, the line number in the traceback above also changes.
Cython may therefore need to define __spec__
or __package__
when executing top-level module code. The problem is where it should get it from, so perhaps this is really a CPython issue.
Issue Analytics
- State:
- Created 6 years ago
- Comments:30 (26 by maintainers)
Top GitHub Comments
Support for PEP 489 / #1715 is implemented in #1794
@jebob That’s probably because the default filter list for python excludes
ImportWarning
s. ‘default’ not to be confused with-Wdefault
, which simply means ‘warn once’. The default python warnings filter list is:Unfortunately, you can’t get around this warning without disabling
ImportWarning
s altogether (at least in my case, dealing with it while using Pandas), since theImportWarning
comes fromimportlib
itself.