aliyun / aliyun-openapi-python-sdk

Alibaba Cloud SDK for Python
Other
1k stars 588 forks source link

Vendored six package is incompatible with Python 3.12. #491

Closed bdperkin closed 10 months ago

bdperkin commented 1 year ago

Code that we have been running under Python 3.{9,10,11,12} has been working fine. Recently, something changed where Python 3.12 (and none of the other versions) began to fail with:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/aliyun/aliyun-openapi-python-sdk/aliyun-python-sdk-core-v3/aliyunsdkcore/client.py", line 31, in <module>
    from aliyunsdkcore.vendored.six.moves.urllib.parse import urlencode
ModuleNotFoundError: No module named 'aliyunsdkcore.vendored.six.moves'

The issue appears to be the version of six that is included in aliyunsdkcore/vendored/six.py and aliyunsdkcore/vendored/requests/packages/urllib3/packages/six.py is causing the issue.

A simple reproducer is to just import aliyunsdkcore.client.

Here shows how it works with Python 3.11.4:

$ python3.11 -m venv venv_py311
$ source venv_py311/bin/activate
(venv_py311) $ pip --disable-pip-version-check --quiet install cryptography jmespath
(venv_py311) $ pushd aliyun-python-sdk-core-v3/
~/git/aliyun-openapi-python-sdk/aliyun-python-sdk-core-v3 ~/git/aliyun-openapi-python-sdk
(venv_py311) $ python --version
Python 3.11.4
(venv_py311) $ python -c "import aliyunsdkcore.client"
(venv_py311) $ echo $?
0
(venv_py311) $ popd
~/git/aliyun-openapi-python-sdk
(venv_py311) $ deactivate

Now, when we do the same thing with Python 3.12.0b3:

$ python3.12 -m venv venv_py312
$ source venv_py312/bin/activate
(venv_py312) $ pip --disable-pip-version-check --quiet install cryptography jmespath
(venv_py312) $ pushd aliyun-python-sdk-core-v3/
~/git/aliyun-openapi-python-sdk/aliyun-python-sdk-core-v3 ~/git/aliyun-openapi-python-sdk
(venv_py312) $ python --version
Python 3.12.0b3
(venv_py312) $ python -c "import aliyunsdkcore.client"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/aliyun/aliyun-openapi-python-sdk/aliyun-python-sdk-core-v3/aliyunsdkcore/client.py", line 31, in <module>
    from aliyunsdkcore.vendored.six.moves.urllib.parse import urlencode
ModuleNotFoundError: No module named 'aliyunsdkcore.vendored.six.moves'
(venv_py312) $ echo $?
1

We can then check the version of six that is installed, install a new version, and replace the vendored version:

(venv_py312) $ grep '^__version__' aliyunsdkcore/vendored/six.py
__version__ = "1.11.0"
(venv_py312) $ pip3 freeze
cffi==1.15.1
cryptography==41.0.2
jmespath==1.0.1
pycparser==2.21
(venv_py312) $ pip --disable-pip-version-check --quiet install six
(venv_py312) $ pip3 freeze
cffi==1.15.1
cryptography==41.0.2
jmespath==1.0.1
pycparser==2.21
six==1.16.0
(venv_py312) $ grep '^__version__' ../venv_py312/lib/python3.12/site-packages/six.py 
__version__ = "1.16.0"
(venv_py312) $ md5sum aliyunsdkcore/vendored/six.py ../venv_py312/lib/python3.12/site-packages/six.py
17586a552d872418b8998224011b1037  aliyunsdkcore/vendored/six.py
9379cf68c692d9a9f92e5d29f6a54549  ../venv_py312/lib/python3.12/site-packages/six.py
(venv_py312) $ cp -a ../venv_py312/lib/python3.12/site-packages/six.py aliyunsdkcore/vendored/six.py
(venv_py312) $ md5sum aliyunsdkcore/vendored/six.py ../venv_py312/lib/python3.12/site-packages/six.py
9379cf68c692d9a9f92e5d29f6a54549  aliyunsdkcore/vendored/six.py
9379cf68c692d9a9f92e5d29f6a54549  ../venv_py312/lib/python3.12/site-packages/six.py

If we try the same import again, we get:

(venv_py312) $ python -c "import aliyunsdkcore.client"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/aliyun/aliyun-openapi-python-sdk/aliyun-python-sdk-core-v3/aliyunsdkcore/client.py", line 32, in <module>
    from aliyunsdkcore.vendored.requests import codes
  File "/home/aliyun/aliyun-openapi-python-sdk/aliyun-python-sdk-core-v3/aliyunsdkcore/vendored/requests/__init__.py", line 54, in <module>
    from . import utils
  File "/home/aliyun/aliyun-openapi-python-sdk/aliyun-python-sdk-core-v3/aliyunsdkcore/vendored/requests/utils.py", line 23, in <module>
    from . import certs
  File "/home/aliyun/aliyun-openapi-python-sdk/aliyun-python-sdk-core-v3/aliyunsdkcore/vendored/requests/certs.py", line 17, in <module>
    from .packages.certifi import where
  File "/home/aliyun/aliyun-openapi-python-sdk/aliyun-python-sdk-core-v3/aliyunsdkcore/vendored/requests/packages/__init__.py", line 3, in <module>
    from . import urllib3
  File "/home/aliyun/aliyun-openapi-python-sdk/aliyun-python-sdk-core-v3/aliyunsdkcore/vendored/requests/packages/urllib3/__init__.py", line 8, in <module>
    from .connectionpool import (
  File "/home/aliyun/aliyun-openapi-python-sdk/aliyun-python-sdk-core-v3/aliyunsdkcore/vendored/requests/packages/urllib3/connectionpool.py", line 11, in <module>
    from .exceptions import (
  File "/home/aliyun/aliyun-openapi-python-sdk/aliyun-python-sdk-core-v3/aliyunsdkcore/vendored/requests/packages/urllib3/exceptions.py", line 2, in <module>
    from .packages.six.moves.http_client import (
ModuleNotFoundError: No module named 'aliyunsdkcore.vendored.requests.packages.urllib3.packages.six.moves'
(venv_py312) $ echo $?
1

We can then check the version of six that is installed and replace the vendored version:

(venv_py312) $ grep '^__version__' aliyunsdkcore/vendored/requests/packages/urllib3/packages/six.py
__version__ = "1.10.0"
(venv_py312) $ md5sum aliyunsdkcore/vendored/requests/packages/urllib3/packages/six.py ../venv_py312/lib/python3.12/site-packages/six.py
f01049871bac643b4d7221f6c5cad17a  aliyunsdkcore/vendored/requests/packages/urllib3/packages/six.py
9379cf68c692d9a9f92e5d29f6a54549  ../venv_py312/lib/python3.12/site-packages/six.py
(venv_py312) $ cp -a ../venv_py312/lib/python3.12/site-packages/six.py aliyunsdkcore/vendored/requests/packages/urllib3/packages/six.py
(venv_py312) $ md5sum aliyunsdkcore/vendored/requests/packages/urllib3/packages/six.py ../venv_py312/lib/python3.12/site-packages/six.py
9379cf68c692d9a9f92e5d29f6a54549  aliyunsdkcore/vendored/requests/packages/urllib3/packages/six.py
9379cf68c692d9a9f92e5d29f6a54549  ../venv_py312/lib/python3.12/site-packages/six.py

Now, if we try again, it works:

(venv_py312) $ python -c "import aliyunsdkcore.client"
(venv_py312) $ echo $?
0
(venv_py312) $ popd
~/git/aliyun-openapi-python-sdk
(venv_py312) $ deactivate

Note, I believe the better course of action is to actually update the requests package instead of just changing the bundled six package in it, but this was a quick way of exhibiting the problem.