Skip to content

Do not lazily import olefile - #10213

Merged
hugovk merged 2 commits into
python-pillow:mainfrom
radarhere:lazy
Oct 15, 2026
Merged

hugovk merged 2 commits into
python-pillow:mainfrom
radarhere:lazy

Conversation

@radarhere

Copy link
Copy Markdown
Member

#9733 added lazy imports, a new feature of Python 3.15.

However, https://docs.python.org/3.15/reference/simple_stmts.html#lazy

If an error occurs during module loading (such as ImportError or SyntaxError), it is raised at the point where the lazy import is first used, not at the import statement itself.

If there is going to be an import error, I don't think this plays well with our expectation that an ImportError would prevent MicImagePlugin from registering its format.

Pillow/src/PIL/Image.py

Lines 488 to 492 in 964d7bc

for plugin in _plugins:
try:
__import__(f"{__spec__.parent}.{plugin}", globals(), locals(), [])
except ImportError:
pass

__lazy_modules__ = {"olefile"}
import olefile

Image.register_open(MicImageFile.format, MicImageFile, _accept)

The unexpected failure that results can be seen at https://github.com/python-pillow/Pillow/actions/runs/38696429958/job/116294282528

My suggestion is that we don't lazily import olefile. I think that is the only optional module that we are lazily importing.

@codspeed

codspeed Bot commented Oct 15, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 11.52%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 3 regressed benchmarks
✅ 637 untouched benchmarks
⏩ 338 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_draw_lines_blend[1237x811-0] 1 ms 1.2 ms -11.59%
❌ test_draw_lines_blend[1237x811-255] 1 ms 1.2 ms -11.52%
❌ test_draw_lines_blend[1237x811-127] 1 ms 1.2 ms -11.44%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing radarhere:lazy (fb2c298) with main (bc7e6f5)2

Open in CodSpeed

Footnotes

  1. 338 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (964d7bc) during the generation of this report, so bc7e6f5 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@radarhere
radarhere requested a review from hugovk October 15, 2026 11:44
@radarhere radarhere added this to the 13.0.0 milestone Oct 15, 2026
@hugovk
hugovk merged commit c0229f7 into python-pillow:main Oct 15, 2026
51 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants