You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Monkeypatching html.parser fails when importing with zipimporter #1132
When trying to monkeypatch a copy of html.parser, markdown fails when html.parser comes from a zip file and an instance of zipimporter is used as the loader.
$ python
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
>>> import markdown
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Rémy Roy\Projects\CDDA-Game-Launcher\build\test\markdown\__init__.py", line 29, in <module>
from .core import Markdown, markdown, markdownFromFile # noqa: E402
File "C:\Users\Rémy Roy\Projects\CDDA-Game-Launcher\build\test\markdown\core.py", line 27, in <module>
from .preprocessors import build_preprocessors
File "C:\Users\Rémy Roy\Projects\CDDA-Game-Launcher\build\test\markdown\preprocessors.py", line 29, in <module>
from .htmlparser import HTMLExtractor
File "C:\Users\Rémy Roy\Projects\CDDA-Game-Launcher\build\test\markdown\htmlparser.py", line 31, in <module>
spec.loader.exec_module(htmlparser)
AttributeError: 'zipimporter' object has no attribute 'exec_module'
The main issue is that the zipimporter loader does not have an exec_module method.
This puts us in a weird jam. We need to monkeypatch the html parser. However, we should never be changing the behavior of the default parser. For example, a user of Python-Markdown could import and run markdown and in the same script import and use an instance of the html parser from the standard lib. In that case, the user should not get our modified behavior. To avoid that, we specifically import the lib in a way that Python sees it as a separate package than the normally imported package. Is there a way to accomplish that which also works with zipimporter? If not, then we can't support zipimporter.
I'm not familiar enough with the importing internals. My guess is that there should be way to support that with the zipimporter since it's just the same source code in a zip file.
I just did some digging on this issue and it appears that this has been fixed upstream (in Python's zipimport module) in https://bugs.python.org/issue42131 (see the changes in d2e94bb0). It would appear that the changes are available in Python 3.10.
We could close this as an upstream issue. However, that does not address the issue for Python < 3.10. If someone was to provide a patch for that it would be considered. Therefore, I will leave this open for the time being.
i suggest this workaround.
you get patched version of zipimport and put it near the MD module. you rename it a little - eg to zipimport_fixed. you use renamed module instead. use relative import please, from . import zipimport_fixed
@Alexey-T we have certainly done that in the past. However, we don't call the zipimport lib at all. However, packages which use embeddable Python do. Therefore, the package which explicitly imports zipimport would need to take the steps you recommend.
By way of explanation, we use the new standard API defined in PEP 451 as found in the importlib module of the Python Standard Library. However, embeddable Python replaces calls to importlib with zipimport, which up until Python 3.10 still only defined the old API from PEP 302. Therefore, we don't have any control over which lib from the standard library is called.
I'm sure there is still some workaround that would address the issue, but it is not immediately clear what that would be and it would likely require extensive testing on a system we don't use (and are not familiar with). Therefore, it is not a high priority issue for us. However, if someone who is familiar with and uses a relevant system were to work up and submit a fix with tests, then we would be willing to review it.
Hi! Triaging older issues — this one looks resolved by the Python minimum-version bump: zipimport.zipimporter gained exec_module in Python 3.10, and Python-Markdown now requires Python >= 3.10.
Python 3.10 zipimport docs confirm exec_module() is now provided directly on zipimporter, alongside create_module(), so it implements the Loader protocol.
The original traceback was on Python 3.9.5, where zipimporter only had the legacy loader protocol. On any currently-supported runtime (3.10+) the spec.loader.exec_module(htmlparser) call in markdown/htmlparser.py L43 should work for the embeddable-package / zipped-stdlib scenario the OP described. If anyone hits this on a supported Python, a fresh reproducer would be welcome — otherwise this looks safe to close.
Disclosure: I drafted this comment with help from Claude Code while triaging stale issues; the references above were verified manually against markdown==3.10.2 on Python 3.11.
Activity
remyroy commentedon May 12, 2021
This is related to the work of @waylan in #803
waylan commentedon May 13, 2021
This puts us in a weird jam. We need to monkeypatch the html parser. However, we should never be changing the behavior of the default parser. For example, a user of Python-Markdown could import and run
markdownand in the same script import and use an instance of the html parser from the standard lib. In that case, the user should not get our modified behavior. To avoid that, we specifically import the lib in a way that Python sees it as a separate package than the normally imported package. Is there a way to accomplish that which also works withzipimporter? If not, then we can't supportzipimporter.remyroy commentedon May 13, 2021
I'm not familiar enough with the importing internals. My guess is that there should be way to support that with the
zipimportersince it's just the same source code in a zip file.waylan commentedon Aug 22, 2022
I just did some digging on this issue and it appears that this has been fixed upstream (in Python's
zipimportmodule) in https://bugs.python.org/issue42131 (see the changes in d2e94bb0). It would appear that the changes are available in Python 3.10.We could close this as an upstream issue. However, that does not address the issue for Python < 3.10. If someone was to provide a patch for that it would be considered. Therefore, I will leave this open for the time being.
Alexey-T commentedon Aug 22, 2022
@waylan
i suggest this workaround.
you get patched version of
zipimportand put it near the MD module. you rename it a little - eg tozipimport_fixed. you use renamed module instead. use relative import please,from . import zipimport_fixed@veksha FYI
waylan commentedon Aug 22, 2022
@Alexey-T we have certainly done that in the past. However, we don't call the
zipimportlib at all. However, packages which use embeddable Python do. Therefore, the package which explicitly importszipimportwould need to take the steps you recommend.By way of explanation, we use the new standard API defined in PEP 451 as found in the
importlibmodule of the Python Standard Library. However, embeddable Python replaces calls toimportlibwithzipimport, which up until Python 3.10 still only defined the old API from PEP 302. Therefore, we don't have any control over which lib from the standard library is called.I'm sure there is still some workaround that would address the issue, but it is not immediately clear what that would be and it would likely require extensive testing on a system we don't use (and are not familiar with). Therefore, it is not a high priority issue for us. However, if someone who is familiar with and uses a relevant system were to work up and submit a fix with tests, then we would be willing to review it.
jbbqqf commentedon May 24, 2026
Hi! Triaging older issues — this one looks resolved by the Python minimum-version bump:
zipimport.zipimportergainedexec_modulein Python 3.10, and Python-Markdown now requires Python >= 3.10.Evidence:
pyproject.tomlL22:requires-python = '>=3.10'.zipimportdocs confirmexec_module()is now provided directly onzipimporter, alongsidecreate_module(), so it implements theLoaderprotocol.markdown==3.10.2:The original traceback was on Python 3.9.5, where
zipimporteronly had the legacy loader protocol. On any currently-supported runtime (3.10+) thespec.loader.exec_module(htmlparser)call inmarkdown/htmlparser.pyL43 should work for the embeddable-package / zipped-stdlib scenario the OP described. If anyone hits this on a supported Python, a fresh reproducer would be welcome — otherwise this looks safe to close.Disclosure: I drafted this comment with help from Claude Code while triaging stale issues; the references above were verified manually against
markdown==3.10.2on Python 3.11.waylan commentedon May 24, 2026
Thanks for confirming this. I had intended to but never got to it.