GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — platform: import _wmi on first use · Issue #161513 · python/cpython

platform: import _wmi on first use #161513

New issue
Copy link
New issue
Copy link
Open
Open
platform: import _wmi on first use#161513
Copy link
Labels
OS-windowsperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@azchohfi
azchohfi
opened on Nov 6, 2026
Issue body actions

Feature or enhancement

Lib/platform.py imports _wmi at module level on Windows. _wmi.pyd loads oleaut32.dll and propsys.dll (with _wmi.pyd itself and their dependencies, 7 more images in the process), but WMI is only queried by win32_ver(), version(), release(), uname() and the processor/machine helpers. Code that only imports platform, or calls python_version() or system(), pays for it anyway. pytest is one example: it imports platform on every run.

Moving the import into _wmi_query() (keeping the "unavailable" state so a failed import or query is not retried):

60 runs each, perf_counter around the import before after difference
import platform 18.6 ms 12.9 ms −5.5 ms [−6.0, −5.1]
import pytest 206 ms 196 ms −9.5 ms [−13.6, −4.7]

(python-build-standalone 3.14.4, Windows 11 26100.) win32_ver(), uname(), machine(), version() and processor() return the same values; test_platform and test_wmi pass.

The change, against main (2781ffe):

diff --git a/Lib/platform.py b/Lib/platform.py
index c87bf2f..375acc2 100644
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -114,10 +114,10 @@ import sys
 import functools
 import itertools
 lazy import warnings
-try:
-    import _wmi
-except ImportError:
-    _wmi = None
+# _wmi is imported on first use (see _wmi_query): loading it maps the OLE
+# and property-system DLLs, which most users of this module never need.
+# None means not imported yet, False means unavailable.
+_wmi = None
 
 ### Globals & Constants
 
@@ -332,6 +332,12 @@ def _syscmd_ver(system='', release='', version='',
 
 def _wmi_query(table, *keys):
     global _wmi
+    if _wmi is None:
+        try:
+            import _wmi as wmi
+        except ImportError:
+            wmi = False
+        _wmi = wmi
     if not _wmi:
         raise OSError("not supported")
     table = {
@@ -344,7 +350,7 @@ def _wmi_query(table, *keys):
             table,
         )).split("\0")
     except OSError:
-        _wmi = None
+        _wmi = False
         raise OSError("not supported")
     split_data = (i.partition("=") for i in data)
     dict_data = {i[0]: i[2] for i in split_data}

(PEP 810's lazy import doesn't fit here because the import sits in try/except ImportError.)

I used GitHub Copilot (Claude Opus 5.5 model) to trace this, build and time the prototype, and draft this issue. The measurements were run on my machine.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

  • gh-161513: Resolve _wmi lazily in platform #161543
  • gh-161513: Import _wmi lazily in platform #161656

Activity

  1. NekoAsakura
    added
    type-featureA feature request or enhancementA feature request or enhancement
    performancePerformance or resource usagePerformance or resource usage
    OS-windows
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Nov 6, 2026
  2. sobolevn commented on Nov 7, 2026

    @sobolevn
    sobolevn
    on Nov 7, 2026
    Member
    More actions

    You still can use lazy import _wmi, just catch the exception with something like:

        try:
            _wmi
        except ImportError:
            _wmi = None
  3. xixi-box
    added 7 commits that reference this issue on Nov 7, 2026

    pythongh-161513: Resolve _wmi lazily in platform.py

    22a4c30

    pythongh-161513: Resolve _wmi lazily in platform.py

    81c0bbe

    pythongh-161513: Resolve _wmi lazily in platform.py

    4128b12

    pythongh-161513: address review feedback

    07b9781

    pythongh-161513: address review suggestions

    a10fb85

    pythongh-161513: trim comments per review

    cb7ba0f

    pythongh-161513: simplify _wmi_query per review

    7181e56
  4. hugovk
    added a parent issue on Nov 8, 2026
    • Improve import time of various stdlib modules #154675
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowsperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.