__init__.py causes No module named 'adblock.adblock'
After building via maturin build and installing the wheel via pip, running python3 -c "import adblock" results in:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/florian/tmp/python-adblock/adblock/__init__.py", line 1, in <module>
from .adblock import __version__, Engine, FilterSet, BlockerResult, UrlSpecificResources
ModuleNotFoundError: No module named 'adblock.adblock'
This is because the adblock module gets imported, and due to from .adblock import, Python tries to import adblock.adblock.
When trying again outside of the git repository, the import works fine.
It looks like that adblock module is only required for type annotations? Is the __init__.py really required, and is the import even correct? Perhaps it could be moved to src/ somehow to not interfere with the native extension module?
Can you reproduce this behaviour with (run in root of git repository):
#!/bin/sh
set -xe
python3.9 -m pip uninstall -y adblock
maturin build --release --interpreter python3.9
python3.9 -m pip install --user target/wheels/adblock-0.4.0-cp39-*.whl
python3.9 -c "import adblock; print('success! version:', adblock.__version__)"
I can't, unfortunately.
It looks like that
adblockmodule is only required for type annotations?
That's right. It's the most straightforward way I found to make a PEP 561 compatible package.
Is the
__init__.pyreally required, and is the import even correct?
I'm not sure, but it is the pattern that a number of PyO3 examples seem to follow. See for example:
- https://github.com/PyO3/pyo3/blob/master/examples/word-count/word_count/init.py
- https://github.com/PyO3/setuptools-rust/blob/master/examples/html-py-ever/html_py_ever/init.py
- https://github.com/PyO3/maturin/blob/master/test-crates/pyo3-mixed/pyo3_mixed/init.py
I think this is fixed in https://github.com/ArniDagur/python-adblock/commit/dc33223fcb1583bf31b89474ec299d45d8f80ef0
I have the same issue, and dc33223 did not fix it for me.
I'm seeing this on the package installed system-wide via setup.py
@b3n @ddevault Ah, I had been trying to reproduce this when installing with --user. I can reproduce when:
- I install the package system-wide
- Current working directory is the
python-adblockgit repository. - I run
python3 -c "import adblock"
When I leave the git repository, import adblock works again. Does this match your experience, or does it also happen outside of the git repo?
I've never tried it from the git repository, and that's not how I would use it anyway - I am packaging this for Alpine Linux.
This seemed to surface for Alpine when we went from python 3.8 to 3.9...
+1 inside the git directory:
$ python3 -c "import adblock"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/stefan/projects/python-adblock/adblock/__init__.py", line 1, in <module>
from adblock.adblock import __version__, Engine, FilterSet, BlockerResult, UrlSpecificResources
ModuleNotFoundError: No module named 'adblock.adblock'
There is no abi3.so inside the adblock/ directory even after the build... (maybe it's not supposed to be there, I don't know)