Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package requests==2.32.0 fails to import package ssl is not available #918

Closed
larsmoa opened this issue May 22, 2024 · 5 comments
Closed

Comments

@larsmoa
Copy link

larsmoa commented May 22, 2024

The latest release of requests (2.32.x) fails to work with error TypeError: Can't create an SSLContext object without an ssl module.

Minimal reproducible example:

streamlit_app.py:

import requests

requirements.txt:

requests==2.32.2

Available on share.stlite.net

Discussion

I've done some investigation and this seems to happen because of the code that patches requests (

pyodide_http.patch_all() # Patch all libraries
):

  • pyodide_http.patch_all() calls patch_requests()
  • patch_requests() imports "from requests.adapters import BaseAdapter"
  • requests.adapters imports "from urllib3.util.ssl_ import create_urllib3_context"
    and calls create_urllib3_context()
  • create_urllib3_context() throws exception from this code snippet:
    if SSLContext is None:
        raise TypeError("Can't create an SSLContext object without an ssl module")
    
  • This happens because there SSLContext is initialized as None in the "static initializer"
    on import
  • SSLContext is overriden by code snippet from ssl_.py:
    try:  # Do we have ssl at all?
        import ssl
        from ssl import (  # type: ignore[assignment]
            CERT_REQUIRED,
            HAS_NEVER_CHECK_COMMON_NAME,
            OP_NO_COMPRESSION,
            OP_NO_TICKET,
            OPENSSL_VERSION,
            OPENSSL_VERSION_NUMBER,
            PROTOCOL_TLS,
            PROTOCOL_TLS_CLIENT,
            OP_NO_SSLv2,
            OP_NO_SSLv3,
            SSLContext,
            TLSVersion,
        )
        ...
    

Since ssl is not installed, the import fails.

@whitphx
Copy link
Owner

whitphx commented May 26, 2024

Thank you for reporting this.

You can workaround it by adding ssl to the requirements list explicitly.

Let me consider how we should fix it for a while.
Pyodide made ssl optional (https://pyodide.org/en/stable/usage/wasm-constraints.html) so you need to add it to the requirements list to use, then the issue is whether we should install it automatically for requests or leave it as-is only documenting it.

@aotombielecki
Copy link

When attempting to use the online demo linked from the readme, the application fails with the following error message:

Error during booting up

Traceback (most recent call last):
  File "/lib/python3.10/asyncio/futures.py", line 201, in result
    raise self._exception
  File "/lib/python3.10/asyncio/tasks.py", line 232, in __step
    result = coro.send(None)
  File "/lib/python3.10/_pyodide/_base.py", line 532, in eval_code_async
    await CodeRunner(
  File "/lib/python3.10/_pyodide/_base.py", line 355, in run_async
    coroutine = eval(self.code, globals, locals)
  File "<exec>", line 2, in <module>
  File "/lib/python3.10/site-packages/streamlit/__init__.py", line 50, in <module>
    from streamlit.version import STREAMLIT_VERSION_STRING as _STREAMLIT_VERSION_STRING
  File "/lib/python3.10/site-packages/streamlit/version.py", line 19, in <module>
    import requests
  File "/lib/python3.10/site-packages/requests/__init__.py", line 164, in <module>
    from .api import delete, get, head, options, patch, post, put, request
  File "/lib/python3.10/site-packages/requests/api.py", line 11, in <module>
    from . import sessions
  File "/lib/python3.10/site-packages/requests/sessions.py", line 15, in <module>
    from .adapters import HTTPAdapter
  File "/lib/python3.10/site-packages/requests/adapters.py", line 76, in <module>
    _preloaded_ssl_context = create_urllib3_context()
  File "/lib/python3.10/site-packages/urllib3/util/ssl_.py", line 252, in create_urllib3_context
    raise TypeError("Can't create an SSLContext object without an ssl module")
TypeError: Can't create an SSLContext object without an ssl module

Steps to Reproduce:

  1. Open the online demo.
  2. Wait for the application to install Streamlit.

@whitphx
Copy link
Owner

whitphx commented May 29, 2024

@aotombielecki Thanks for the catch! It's been fixed now 👍
Just updating the stlite version to 0.55.1 from 0.31.0 solved it somehow.

@larsmoa
Copy link
Author

larsmoa commented Jun 2, 2024

This seems to be resolved with requests==2.32.3:

Fixed issue where Requests started failing to run on Python versions compiled without the ssl module. (#6724)

(Changelog)

I've verified that my original example works with this version of the package. Suggesting to close this issue as won't fix.

@whitphx
Copy link
Owner

whitphx commented Jun 3, 2024

Thanks!

@whitphx whitphx closed this as completed Jun 3, 2024
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

No branches or pull requests

3 participants