buriy / python-readability

fast python port of arc90's readability tool, updated to match latest readability.js!
https://github.com/buriy/python-readability
Apache License 2.0
2.67k stars 349 forks source link

Circular import error for pip install readability-lxml #173

Closed stevenplatt closed 2 years ago

stevenplatt commented 2 years ago

When installing the python-readability package via pip, the following import error is returned during use: ImportError: cannot import name 'Document' from partially initialized module 'readability' (most likely due to a circular import)

I am using the following lines from the example snippet from the repo readme:

import requests
from readability import Document

response = requests.get('https://www.theverge.com/2022/4/8/23016861/youtube-comment-spam-testing-moderation')
doc = Document(response.text)

print(doc.title())

I am using an M1 mac for the installation. The prior releases at pypi appears to have dependencies for an older version of python, so I was unable to test with the older releases.

buriy commented 2 years ago

What is your python version? (I think the culprit is __init__.py handling that has changed in python 3.10) The typical use to avoid this is from readability.readability import Document

stevenplatt commented 2 years ago

On the Mac I am python version 3.8.9 (Mac OS Monterey). I tried from readability.readability import Document, but this returns a different error for not finding a module with name readability.

Stevens-MacBook-Pro:~ steven$ python3 --version
Python 3.8.9

I tried running on a Ubuntu 20.04 ARM VM (Running in Parallels on M1 mac) - it works as normal with no issue:

parallels@ubuntu-linux-20-04-desktop:~$ python3 --version
Python 3.8.10

I don't know enough about the python version difference to track down the culprit (and was planning to install into an ubuntu Docker container for my use). I am ok to close this one, if you don't have an M1 Mac test system. What do you think?

buriy commented 2 years ago
python3
Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import readability
>>> d=readability.Document(input='hello world')

It works. So please ensure you don't have a local file named "readability.py" , this is the most typical explanation of this error.

stevenplatt commented 2 years ago

You are correct, sorry about that. I indeed had a file with readability.py on my local machine. After removing that files, all is working as normal on M1 Mac. Thank you for helping.