ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__

Bug #1732505 reported by Marius Gedminas
18
This bug affects 3 people
Affects Status Importance Assigned to Milestone
lxml
Fix Released
Low
scoder

Bug Description

Importing lxml.etree produces an ImportWarning on Python 3.6. These warnings are suppressed by default, so to see them you have to set PYTHONWARNINGS=default.

Steps to reproduce:

    $ python3.6 -Wd
    Python 3.6.0+ (default, Dec 27 2016, 08:48:59)
    [GCC 6.2.0 20161005] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import lxml.etree
    .../lib/python3.6/importlib/_bootstrap.py:205: ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__
      return f(*args, **kwds)

Version information:

Python : sys.version_info(major=3, minor=6, micro=0, releaselevel='final', serial=0)
lxml.etree : (4, 1, 1, 0)
libxml used : (2, 9, 7)
libxml compiled : (2, 9, 7)
libxslt used : (1, 1, 32)
libxslt compiled : (1, 1, 32)

Revision history for this message
Marius Gedminas (mgedmin) wrote :

If I set PYTHONWARNINGS=error::ImportWarning, I can see a full traceback, which is:

  File "<stdin>", line 1, in <module>
  File "src/lxml/etree.pyx", line 93, in init lxml.etree
  File "src/lxml/_elementpath.py", line 56, in init lxml._elementpath

Revision history for this message
Chris Jerdonek (chris-jerdonek) wrote :

This is probably related to this Cython issue ("Spurious ImportWarnings on 3.6+"):
https://github.com/cython/cython/issues/1720

Revision history for this message
Marius Gedminas (mgedmin) wrote :

Summary of that Cython issue:

- it's a Cython bug where it's trying to emulate Python 2 style relative imports on Python 3
- it's fixed in master, but not in any released Cython yet
- you can work around it if you use `from __future__ import absolute_import` in your Cython modules
  and avoid explicit relative imports as well.
- an alternative fix is to make sure you compile the Cython module using language_level=3

Revision history for this message
Chris Jerdonek (chris-jerdonek) wrote :

Are there any workarounds for users of lxml aside from wrapping lxml imports in blocks like the following?

with warnings.catch_warnings():
    warnings.simplefilter('ignore', category=ImportWarning)
    import lxml.etree

Revision history for this message
scoder (scoder) wrote :

I added an "absolute_imports" future line to the files. Will be released when I get to it.

Changed in lxml:
assignee: nobody → scoder (scoder)
importance: Undecided → Low
status: New → Fix Committed
scoder (scoder)
Changed in lxml:
milestone: none → 4.2.6
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.