pyodide / pyodide/pyodide

Add a post initialization step for optional stldib modules

Open
#3,856 1 comment 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
14.8k
Forks
1k
Avg merge
20h 29m
Merged PRs (30d)
19

Description

The stdlib modules have some number of optional components that we also allow to be optionally installed.

Unfortunately, CPython does not operate under the assumption that such modules can be installed at runtime. Modules would have a
```py
try:
import optional_feature
except ImportError:
feature_available = False
```
and if the optional_feature is installed after this module is imported, the module would still behave as though it's not installed. A good example of this can be found in https://github.com/koenvo/pyodide-http/issues/33#issuecomment-1546865267 for the SSL module.

A solution is to minimize the import of modules before the installation, but it's not always possible in downstream applications.

We could raise this issue with CPython but it's unlikely that would want to make this feature detection more dynamic, instead of pre-determined at import time.

The other alternative is to have a list of modules that do this and reload them (if they are already imported and present in `sys.modules`) when such an optional stdlib module is installed. For instance, looking, at
```bash
rg -U "try:\n\s+import ssl" cpython/Lib
```
gives some idea of the impacted modules. This dependency tree could probably be determined automatically by parsing the code.

It would help, but probably not address more complex dependencies as outlined in the above-linked issue.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.