Repository navigation
AttributeError: module 'importlib' has no attribute 'util' with python-markdown 3.4 on macOS/Windows #1274
Copy link
Copy link
Closed
Description
With python3.9 on macOS:
$ python3.9 -m venv venv
$ source venv/bin/activate
$ pip install markdown
Collecting markdown
Using cached Markdown-3.4-py3-none-any.whl (93 kB)
Collecting importlib-metadata>=4.4; python_version < "3.10"
Using cached importlib_metadata-4.12.0-py3-none-any.whl (21 kB)
Collecting zipp>=0.5
Using cached zipp-3.8.1-py3-none-any.whl (5.6 kB)
Installing collected packages: zipp, importlib-metadata, markdown
Successfully installed importlib-metadata-4.12.0 markdown-3.4 zipp-3.8.1
WARNING: You are using pip version 20.2.3; however, version 22.1.2 is available.
You should consider upgrading via the '/Users/mike/tmp/resume.md/venv/bin/python3.9 -m pip install --upgrade pip' command.
$ python
Python 3.9.4 (default, Apr 16 2021, 21:18:07)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import markdown
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mike/tmp/resume.md/venv/lib/python3.9/site-packages/markdown/__init__.py", line 22, in <module>
from .core import Markdown, markdown, markdownFromFile
File "/Users/mike/tmp/resume.md/venv/lib/python3.9/site-packages/markdown/core.py", line 27, in <module>
from .preprocessors import build_preprocessors
File "/Users/mike/tmp/resume.md/venv/lib/python3.9/site-packages/markdown/preprocessors.py", line 29, in <module>
from .htmlparser import HTMLExtractor
File "/Users/mike/tmp/resume.md/venv/lib/python3.9/site-packages/markdown/htmlparser.py", line 29, in <module>
spec = importlib.util.find_spec('html.parser')
AttributeError: module 'importlib' has no attribute 'util'
>>>
With python3.10 on macOS:
$ python3.10 -m venv 3.10
$ source 3.10/bin/activate
$ pip install markdown
Collecting markdown
Using cached Markdown-3.4-py3-none-any.whl (93 kB)
Installing collected packages: markdown
Successfully installed markdown-3.4
WARNING: You are using pip version 22.0.4; however, version 22.1.2 is available.
You should consider upgrading via the '/Users/mike/tmp/resume.md/3.10/bin/python3.10 -m pip install --upgrade pip' command.
$ python
Python 3.10.3 (main, Mar 25 2022, 22:16:41) [Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import markdown
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mike/tmp/resume.md/3.10/lib/python3.10/site-packages/markdown/__init__.py", line 22, in <module>
from .core import Markdown, markdown, markdownFromFile
File "/Users/mike/tmp/resume.md/3.10/lib/python3.10/site-packages/markdown/core.py", line 27, in <module>
from .preprocessors import build_preprocessors
File "/Users/mike/tmp/resume.md/3.10/lib/python3.10/site-packages/markdown/preprocessors.py", line 29, in <module>
from .htmlparser import HTMLExtractor
File "/Users/mike/tmp/resume.md/3.10/lib/python3.10/site-packages/markdown/htmlparser.py", line 29, in <module>
spec = importlib.util.find_spec('html.parser')
AttributeError: module 'importlib' has no attribute 'util'
pip install "markdown<3.4" works, so this is perhaps a regression in the 3.4 release?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
mikepqr commentedon Jul 15, 2022
Note this is also affecting GitHub actions on macOS and Windows (but not Ubuntu): https://github.com/mikepqr/resume.md/actions/runs/2678077124.
facelessuser commentedon Jul 15, 2022
What version of
importlib-metadataare you using, we can see here that Python Markdown requires:importlib-metadata>=4.4for Python < 3.10.mikepqr commentedon Jul 15, 2022
These are installations into fresh virtual environments with
pip install python-markdown. I therefore get whatever packages markdown declares as dependencies.As you can see above, that results in:
in python 3.9, and just
in python 3.10.
Note also that I get the same error on python3.10, which has no importlib-metadata dependency.
facelessuser commentedon Jul 15, 2022
I wonder if something changed in 4.12.0...I'm on 4.11.3 on Python 3.10 and I wasn't experiencing any issues. Tests were run on Py3.10 as well with no issues: https://github.com/Python-Markdown/markdown/runs/7359376261?check_suite_focus=true. If you downgrade importlib-metadata to 4.11.X do you still get the issue?
mikepqr commentedon Jul 15, 2022
Away from test environment now, will check later. But downgrading importlib-metadata on 3.9 won't help on python 3.10. It seems like the current release of python-markdown is broken out of the box on 3.10 on macOS and Windows. Looks like the CI suite only covers Linux?
facelessuser commentedon Jul 15, 2022
Hmm, I'm running macOS and Python 3.10 importlib-metadata 4.12.0 and Python Markdown 3.4 with no issues. I cannot seem to reproduce...
facelessuser commentedon Jul 15, 2022
Is this running with a brew python or an official python distribution?
mikepqr commentedon Jul 15, 2022
Locally it happens to be running in pyenv-installed fresh builds of official python 3.9.6 and 3.10.3, and fresh virtual environments. brew is not involved.
But please note I am seeing the exact same error in Github actions on macOS and Windows.
facelessuser commentedon Jul 15, 2022
I understand, but there is something else going on here. I should be able to reproduce this issue on macOS...I'll have to spend some time this evening and see isolate things in a virtual environment. I should note that I'm on Python 3.10.4. Not sure if that makes a difference either.
mikepqr commentedon Jul 15, 2022
Thanks! I will also try to narrow this down more later.
waylan commentedon Jul 15, 2022
The code which is raising this error has not changed in two years and was part of the 3.3.0 release. There were no changes in any release from 3.3.1 through 3.3.7. If it works in any of those but not in 3.4.0, then the issue is something other than our code.
When you do
pip install "markdown<3.4", which version do you get?mikepqr commentedon Jul 15, 2022
3.3.7.
mikepqr commentedon Jul 15, 2022
Make an empty repo with workflow to attempt to bisect.
Here's what happens with
pip install markdown(i.e.import markdown@ 3.4.0 fails on macOS and Windows withAttributeError: module 'importlib' has no attribute 'util').Here's the same action with python-markdown pinned to 3.3.7 (works on all tested platforms).
facelessuser commentedon Jul 15, 2022
I also have plenty of Windows actions run with Python 3.9 and 3.10 with the version right before Markdown 3.4: https://github.com/facelessuser/pymdown-extensions/actions/runs/2652028392. So I am hesitant to think there is an issue.
waylan commentedon Jul 15, 2022
Actually, we don't require
importlib-metadataat all for Python 3.10, only for any previous Python version. It was my understanding that by default, Python 3.10 was supposed to matchimportlib-metadataversion 4.4 or later. Perhaps that is not true in some cases. Although, it seems strange that an import path for a standard lib would only exist on some platforms.13 remaining items
mikepqr commentedon Jul 15, 2022
I don't think that's generally true. It depends on how
importlib/__init__.pyis set up. You getimportlib.utilin your namespace iff that__init__.pyexplicitly gives it to you:vs.
I'm wondering if a change in
importlib/__init__.pyexplains what we're seeing?mikepqr commentedon Jul 15, 2022
eh, I'm validating this behavior by doing
pip install markdown; python -c "import markdown"with the default version of python provided in the Windows and macOS Github CI runners, and I found out about this problem because a user reported it to me.I will try later.
mikepqr commentedon Jul 15, 2022
Following the advice on https://bugs.python.org/issue41958 that describes
import importlib; importlib.util.find_specas user error:waylan commentedon Jul 15, 2022
Okay, I can reproduce the error sometimes, but not every time.
A few things to note:
utilis imported directly, then it can be referenced fromimportlib.util, but not before.However, I can do this with no errors:
@mikepqr I'm wondering if this change will work for you:
It seems to work for me.
I can't understand why this makes a difference. And weirdly, now I suddenly can't get Markdown to work without the change when I import it from within a Python session. However, when I call Markdown from the command line (
python -m markdown), I get no error.mikepqr commentedon Jul 15, 2022
I think the difference you're seeing is because
import importlibimportsimportlib/__init__.py(which does not (always?) addutilto its namespace) andfrom importlib import utilimportsimportlib/util.py. See this comment for more.The order is not the issue. It's whether or not you do
import importlib.util(or, equivalently,from importlib import util).importlib/__init__.pyis very, very complicated, and I can't figure out what it's doing at all. It's certainly doing some weird stuff. But importingimportlib.utildirectly circumvents this complexity entirely so yes, that diff would fix it for me.So would the diff I posted, which matches what Markdown already does in its setup.py.
I suggest you add that change, assuming it doesn't break already working environments?! (Although I totally understand if you want to get to the root cause of this before landing.)
waylan commentedon Jul 15, 2022
That also resolves it for me. There is some weird funny-business going on here. Why do I need the fix from the Python prompt, but not when calling a script from the command line? And why did this suddenly start happening today, but I've never encountered it before. And why does downgrading Markdown make a difference when there is no difference to the offending code? So weird.
waylan commentedon Jul 15, 2022
Well, I guess that should be the route we take to fix it then. I still have no idea why the error didn't appear before now or why it suddenly started appearing now.
mikepqr commentedon Jul 15, 2022
Me neither!
facelessuser commentedon Jul 15, 2022
Well, this has been a weird and interesting bug. Nice to see a resolution.
mikepqr commentedon Jul 15, 2022
Well, I can't promise the fix doesn't break currently working environments, given that it seems like none of us understand the problem! But the fix unblocks me, so I'm all for it ;-)
mitya57 commentedon Jul 15, 2022
Given that Python documentation has an example of importing
importlib.util, I don't think it can break any currently working environment. Of course, unless someone relied on us raisingAttributeError:)Fix import issue with importlib.util
Fix import issue with importlib.util
mikepqr commentedon Jul 15, 2022
My builds are green again. Thanks folks!