pyyaml icon indicating copy to clipboard operation
pyyaml copied to clipboard

ImportWarning after building from source on PyPy 3.7

Open domdfcoding opened this issue 4 years ago • 2 comments

After installing PyYAML via pip on PyPy 3.7 (which builds a wheel behind the scenes), an ImportWarning is emitted when importing yaml:

$ python3 -Werror -c "import yaml"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/venv/site-packages/yaml/__init__.py", line 13, in <module>
    from .cyaml import *
  File "/tmp/venv/site-packages/yaml/cyaml.py", line 7, in <module>
    from yaml._yaml import CParser, CEmitter
  File "yaml/_yaml.pyx", line 2, in init yaml._yaml
ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__

This looks to be the same issue as https://github.com/cython/cython/issues/1720 and https://bugs.launchpad.net/lxml/+bug/1732505, but I can only reproduce this with PyPy 3.7, and not with CPython.

If I apply the following patch (per https://github.com/cython/cython/issues/1720) and build from source the warning goes away:

diff --git a/yaml/_yaml.pyx b/yaml/_yaml.pyx
index ff4efe8..f5be70e 100644
--- a/yaml/_yaml.pyx
+++ b/yaml/_yaml.pyx
@@ -1,3 +1,4 @@
+from __future__ import absolute_import
 
 import yaml
 

domdfcoding avatar Jul 10 '21 08:07 domdfcoding

Hrm, that's definitely a weird Cython behavior I've not noticed before. Adding the future statement in the pyx shouldn't hurt anything, so if you want to open a PR with that change (and a comment in the code with the link to the Cython issue so we don't delete it in the py2-ectomy ;) ), I'd be +1 for merging that.

Thanks for the details!

nitzmahone avatar Jul 12 '21 18:07 nitzmahone

In case anyone else comes across this warning, I like to turn warnings into errors with PYTHONWARNINGS=error. I tried ignoring this particular warning with:

PYTHONWARNINGS=error,ignore::ImportWarning:yaml._yaml
PYTHONWARNINGS=error,ignore::ImportWarning:yaml.cyaml
PYTHONWARNINGS=error,ignore::ImportWarning:yaml

Those didn't work. So I just ignored ImportWarning entirely with PYTHONWARNINGS=error,ignore::ImportWarning.

jpmckinney avatar Apr 06 '22 03:04 jpmckinney

Noting that this doesn't seem to occur on PyPy 3.9.

jpmckinney avatar Jun 06 '23 16:06 jpmckinney