Package requests==2.32.0 fails to import package ssl is not available
larsmoa opened this issue · 5 comments
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() 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.
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.
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:
- Open the online demo.
- Wait for the application to install Streamlit.
@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.
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)
I've verified that my original example works with this version of the package. Suggesting to close this issue as won't fix.
Thanks!