New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AttributeError: module 'importlib' has no attribute 'util' with python-markdown 3.4 on macOS/Windows #1274
Comments
Note this is also affecting GitHub actions on macOS and Windows (but not Ubuntu): https://github.com/mikepqr/resume.md/actions/runs/2678077124. |
What version of |
These are installations into fresh virtual environments with 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. |
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? |
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? |
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... |
Is this running with a brew python or an official python distribution? |
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. |
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. |
Thanks! I will also try to narrow this down more later. |
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 |
3.3.7. |
Make an empty repo with workflow to attempt to bisect. Here's what happens with Here's the same action with python-markdown pinned to 3.3.7 (works on all tested platforms). |
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. |
Actually, we don't require |
Sorry, I may have stated it backward. |
@mikepqr if you install |
I can confirm on macOS that Python 3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib
>>> importlib.util
<module 'importlib.util' from '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/util.py'>
>>> |
Do these work with markdown 3.4 though? |
I can check later, but I feel like 3.9 vs. 3.10 is probably a red herring, given the error is the same on both versions. |
We'll know shortly: https://github.com/facelessuser/pymdown-extensions/actions/runs/2678476922 |
is this relevant, i.e. are you trying to access |
It's a submodule, nothing is wrong with that: Python 3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib
>>> importlib.util.find_spec
<function find_spec at 0x108703d00> |
Actions passed on py3.9 and py3.10 on Windows. I don't test in CI on mac, but as I stated, I cannot reproduce locally on my mac. |
🤷🏻 Configured the action to just access importlib.util.find_spec. Same error. And locally in fresh environments:
|
There is something very unique about your environment and how you are doing things. I have many, many projects that rely on Markdown for their documentation. I've demonstrated that PY3.10 does in fact access I do not deny you are having an issue, but I cannot reproduce it in any way. You appear to be an outlier. Maybe try using |
I don't think that's generally true. It depends on how
vs.
I'm wondering if a change in |
eh, I'm validating this behavior by doing
I will try later. |
Following the advice on https://bugs.python.org/issue41958 that describes
|
Okay, I can reproduce the error sometimes, but not every time.
A few things to note:
However, I can do this with no errors:
@mikepqr I'm wondering if this change will work for you: diff --git a/markdown/htmlparser.py b/markdown/htmlparser.py
index 7ca858e..cfdc49c 100644
--- a/markdown/htmlparser.py
+++ b/markdown/htmlparser.py
@@ -20,14 +20,14 @@ License: BSD (see LICENSE.md for details).
"""
import re
-import importlib
import sys
+from importlib.util import find_spec, module_from_spec
# Import a copy of the html.parser lib as `htmlparser` so we can monkeypatch it.
# Users can still do `from html import parser` and get the default behavior.
-spec = importlib.util.find_spec('html.parser')
-htmlparser = importlib.util.module_from_spec(spec)
+spec = find_spec('html.parser')
+htmlparser = module_from_spec(spec)
spec.loader.exec_module(htmlparser)
sys.modules['htmlparser'] = htmlparser 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 ( |
I think the difference you're seeing is because
The order is not the issue. It's whether or not you do
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.) |
diff --git a/markdown/htmlparser.py b/markdown/htmlparser.py
index 7ca858e..3512d1a 100644
--- a/markdown/htmlparser.py
+++ b/markdown/htmlparser.py
@@ -20,7 +20,7 @@ License: BSD (see LICENSE.md for details).
"""
import re
-import importlib
+import importlib.util
import sys 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. |
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. |
Me neither! |
Well, this has been a weird and interesting bug. Nice to see a resolution. |
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 ;-) |
Given that Python documentation has an example of importing |
My builds are green again. Thanks folks! |
With python3.9 on macOS:
With python3.10 on macOS:
pip install "markdown<3.4"
works, so this is perhaps a regression in the 3.4 release?The text was updated successfully, but these errors were encountered: