Title: Please restore _Py_InitializeMain or provide an alternate API for it · Issue #142417 · python/cpython · GitHub
Open Graph Title: Please restore _Py_InitializeMain or provide an alternate API for it · Issue #142417 · python/cpython
X Title: Please restore _Py_InitializeMain or provide an alternate API for it · Issue #142417 · python/cpython
Description: As discussed in #129033, opening a new issue at the request of @vstinner. See #129033 (comment) for the reasons this API is needed. In short, a way to run python code and insert a meta importer into sys.meta_path before the "main" phase ...
Open Graph Description: As discussed in #129033, opening a new issue at the request of @vstinner. See #129033 (comment) for the reasons this API is needed. In short, a way to run python code and insert a meta importer int...
X Description: As discussed in #129033, opening a new issue at the request of @vstinner. See #129033 (comment) for the reasons this API is needed. In short, a way to run python code and insert a meta importer int...
Opengraph URL: https://github.com/python/cpython/issues/142417
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Please restore _Py_InitializeMain or provide an alternate API for it","articleBody":"As discussed in #129033, opening a new issue at the request of @vstinner. See https://github.com/python/cpython/issues/129033#issuecomment-3626066338 for the reasons this API is needed. \n\nIn short, a way to run python code and insert a meta importer into sys.meta_path *before* the \"main\" phase of initialization runs is needed. This allows, for instance, to bundle all python code as pre-compiled bytecode into a single mmapped file and directly load it from there for improved performance and hygiene.\n\nI am aware that the state of the interpreter after \"core\" initialization is not entirely specified, however, in practice it is complete enough to write a meta path importer.\n\nThis facility has been used for many years by at least two projects that I know of: bypy (link to source code) https://github.com/kovidgoyal/bypy/blob/master/bypy/freeze/bypy-freeze.h#L835 and PyOxidizer: https://github.com/indygreg/PyOxidizer/blob/1ceca8664c71f39e849ce4873e00d821504b32bd/pyembed/src/technotes.rs#L155 \n\nI dont much care about this API in particular, but I would like some way to hook into the initialization process after \"core\" initialization and before \"main\" initialization.\n\nSome context about use case. bypy is used in the calibre and kitty programs, both of which embed a \"frozen\" Python interpreter. _Py_InitializeMain() allows that frozen importer to import all its python code, including stdlib code from a single mmaped file contianing pre-compiled bytecode. This has obvious performance benefits especially on windows.\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-142420\n* gh-143877\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/kovidgoyal","@type":"Person","name":"kovidgoyal"},"datePublished":"2025-12-08T13:22:01.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/142417/cpython/issues/142417"}
| 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:a09e64ce-af79-0760-e89c-4f924046800d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E03C:144C84:BD5A86:109791F:696A2A41 |
| html-safe-nonce | c0a5ade92f531918d58a33cc645b147a1f5e116a4b3600b056e639dbd699cc49 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMDNDOjE0NEM4NDpCRDVBODY6MTA5NzkxRjo2OTZBMkE0MSIsInZpc2l0b3JfaWQiOiI3MDk5OTgxOTc2MDM3ODk0NzIxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f560b9b6610b7a118ec91174e05cb3cf56d87cfa2b45d0657ad3075beb8f82c5 |
| hovercard-subject-tag | issue:3706385020 |
| 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/142417/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6f3ae20c516099596f209df5835a10c71a88688fad4d605b960bff2f8922a4fc/python/cpython/issues/142417 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6f3ae20c516099596f209df5835a10c71a88688fad4d605b960bff2f8922a4fc/python/cpython/issues/142417 |
| og:image:alt | As discussed in #129033, opening a new issue at the request of @vstinner. See #129033 (comment) for the reasons this API is needed. In short, a way to run python code and insert a meta importer int... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kovidgoyal |
| hostname | github.com |
| expected-hostname | github.com |
| None | a1022f03e4f0d91ea173e4e5dac892c982e0588c62f1ce56121d755a320a3569 |
| 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 | f472b8e6c7b3fdd5d0354972a3f4c516289bf0be |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width