Title: platform: import _wmi on first use · Issue #161513 · python/cpython · GitHub
Open Graph Title: platform: import _wmi on first use · Issue #161513 · python/cpython
X Title: platform: import _wmi on first use · Issue #161513 · python/cpython
Description: 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...
Open Graph Description: 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 proces...
X Description: 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 proces...
Opengraph URL: https://github.com/python/cpython/issues/161513
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"platform: import _wmi on first use","articleBody":"### Feature or enhancement\r\n\r\n`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.\r\n\r\nMoving the import into `_wmi_query()` (keeping the \"unavailable\" state so a failed import or query is not retried):\r\n\r\n| 60 runs each, perf_counter around the import | before | after | difference |\r\n|---|---:|---:|---:|\r\n| `import platform` | 18.6 ms | 12.9 ms | −5.5 ms [−6.0, −5.1] |\r\n| `import pytest` | 206 ms | 196 ms | −9.5 ms [−13.6, −4.7] |\r\n\r\n(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.\r\n\r\nThe change, against main (2781ffe1f2):\r\n\r\n```diff\r\ndiff --git a/Lib/platform.py b/Lib/platform.py\r\nindex c87bf2f..375acc2 100644\r\n--- a/Lib/platform.py\r\n+++ b/Lib/platform.py\r\n@@ -114,10 +114,10 @@ import sys\r\n import functools\r\n import itertools\r\n lazy import warnings\r\n-try:\r\n- import _wmi\r\n-except ImportError:\r\n- _wmi = None\r\n+# _wmi is imported on first use (see _wmi_query): loading it maps the OLE\r\n+# and property-system DLLs, which most users of this module never need.\r\n+# None means not imported yet, False means unavailable.\r\n+_wmi = None\r\n \r\n ### Globals & Constants\r\n \r\n@@ -332,6 +332,12 @@ def _syscmd_ver(system='', release='', version='',\r\n \r\n def _wmi_query(table, *keys):\r\n global _wmi\r\n+ if _wmi is None:\r\n+ try:\r\n+ import _wmi as wmi\r\n+ except ImportError:\r\n+ wmi = False\r\n+ _wmi = wmi\r\n if not _wmi:\r\n raise OSError(\"not supported\")\r\n table = {\r\n@@ -344,7 +350,7 @@ def _wmi_query(table, *keys):\r\n table,\r\n )).split(\"\\0\")\r\n except OSError:\r\n- _wmi = None\r\n+ _wmi = False\r\n raise OSError(\"not supported\")\r\n split_data = (i.partition(\"=\") for i in data)\r\n dict_data = {i[0]: i[2] for i in split_data}\r\n```\r\n\r\n(PEP 810's `lazy import` doesn't fit here because the import sits in `try/except ImportError`.)\r\n\r\nI 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.\r\n\r\n### Has this already been discussed elsewhere?\r\n\r\nThis is a minor feature, which does not need previous discussion elsewhere\r\n\r\n### Links to previous discussion of this feature:\r\n\r\n_No response_\r\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-161543\n* gh-161656\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/azchohfi","@type":"Person","name":"azchohfi"},"datePublished":"2026-11-06T18:43:37Z","url":"https://github.com/python/cpython/issues/161513"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:18161dd0-ec9e-5cf6-4cd2-7d8df7fbd233 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C85E:1CCECA:1427BBC:1AD0D30:6B02039D |
| html-safe-nonce | 145a82ea625581b7dc02fb8247fce7cc3da011d9afe4e5617322d8338874a6af |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDODVFOjFDQ0VDQToxNDI3QkJDOjFBRDBEMzA6NkIwMjAzOUQiLCJ2aXNpdG9yX2lkIjoiNjMyMTY2ODYyMjgyMTQ5MTYxMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8ce00ab43bdc5fbb7e9616751c43761a7a36934249d6cf90a1c878cfbeabd9c6 |
| hovercard-subject-tag | issue:6248597916 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/161513/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c71a7661bb62fef2dba6895f75da3a2dc34c36aa1100931f50459545782a9297/python/cpython/issues/161513 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c71a7661bb62fef2dba6895f75da3a2dc34c36aa1100931f50459545782a9297/python/cpython/issues/161513 |
| og:image:alt | 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 proces... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | azchohfi |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width