ArniDagur / python-adblock

Brave's adblock library in Python
https://pypi.org/project/adblock/
Apache License 2.0
58 stars 4 forks source link

__init__.py causes No module named 'adblock.adblock' #17

Open The-Compiler opened 3 years ago

The-Compiler commented 3 years ago

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?

ArniDagur commented 3 years ago

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 adblock module 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__.py really 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:

ArniDagur commented 3 years ago

I think this is fixed in https://github.com/ArniDagur/python-adblock/commit/dc33223fcb1583bf31b89474ec299d45d8f80ef0

b3n commented 3 years ago

I have the same issue, and dc33223 did not fix it for me.

ddevault commented 3 years ago

I'm seeing this on the package installed system-wide via setup.py

ArniDagur commented 3 years ago

@b3n @ddevault Ah, I had been trying to reproduce this when installing with --user. I can reproduce when:

  1. I install the package system-wide
  2. Current working directory is the python-adblock git repository.
  3. 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?

ddevault commented 3 years ago

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.

omnivagant commented 3 years ago

This seemed to surface for Alpine when we went from python 3.8 to 3.9...

steils commented 3 years ago

+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)