Python报错:ImportError: No module named 'requests.packages.urllib3' 解决方法

it2022-05-05  137

Python报错:ImportError:No module named requests.packages.urllib3 解决方法


对于公网服务器,我们常常需要在其上运行Python脚本,由于服务器操作系统上默认的Python版本不同,在运行过程中常出现报错,今天主要介绍关于“ImportError: No module named requests.packages.urllib3 ”报错的解决方法。

[root@104 ~]# python run620.py Traceback (most recent call last): File "run620.py", line 7, in <module> import json, requests File "/usr/lib/python2.6/site-packages/requests/__init__.py", line 58, in <module> from . import utils File "/usr/lib/python2.6/site-packages/requests/utils.py", line 32, in <module> from .exceptions import InvalidURL File "/usr/lib/python2.6/site-packages/requests/exceptions.py", line 10, in <module> from .packages.urllib3.exceptions import HTTPError as BaseHTTPError File "/usr/lib/python2.6/site-packages/requests/packages/__init__.py", line 99, in load_module raise ImportError("No module named '%s'" % (name,)) ImportError: No module named 'requests.packages.urllib3'

在报错后,得知缺少’requests.packages.urllib3’ 库,尝试安装:

[root@104 ~]# pip install requests.packages.urllib3 DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6 Collecting requests.packages.urllib3 /usr/lib/python2.6/site-packages/pip/_vendor/urllib3/util/ssl_.py:339: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings SNIMissingWarning /usr/lib/python2.6/site-packages/pip/_vendor/urllib3/util/ssl_.py:137: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecurePlatformWarning /usr/lib/python2.6/site-packages/pip/_vendor/urllib3/util/ssl_.py:137: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecurePlatformWarning Could not find a version that satisfies the requirement requests.packages.urllib3 (from versions: ) No matching distribution found for requests.packages.urllib3 /usr/lib/python2.6/site-packages/pip/_vendor/urllib3/util/ssl_.py:137: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecurePlatformWarning You are using pip version 9.0.3, however version 19.1.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.

可以发现 pip Could not find a version that satisfies the requirement requests.packages.urllib3 ,这是由于requests 的版本需要为 2.6.0。


解决方法:

pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3

[root@104 ~]# pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3 DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6 Collecting requests==2.6.0 /usr/lib/python2.6/site-packages/pip/_vendor/urllib3/util/ssl_.py:339: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings SNIMissingWarning /usr/lib/python2.6/site-packages/pip/_vendor/urllib3/util/ssl_.py:137: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecurePlatformWarning /usr/lib/python2.6/site-packages/pip/_vendor/urllib3/util/ssl_.py:137: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecurePlatformWarning /usr/lib/python2.6/site-packages/pip/_vendor/urllib3/util/ssl_.py:137: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecurePlatformWarning Downloading https://files.pythonhosted.org/packages/73/63/b0729be549494a3e31316437053bc4e0a8bb71a07a6ee6059434b8f1cd5f/requests-2.6.0-py2.py3-none-any.whl (469kB) 100% |████████████████████████████████| 471kB 1.6MB/s Collecting urllib3 Using cached https://files.pythonhosted.org/packages/bd/c9/6fdd990019071a4a32a5e7cb78a1d92c53851ef4f56f62a3486e6a7d8ffb/urllib3-1.23-py2.py3-none-any.whl Installing collected packages: requests, urllib3 Found existing installation: requests 2.6.0 DEPRECATION: Uninstalling a distutils installed project (requests) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project. Uninstalling requests-2.6.0: Successfully uninstalled requests-2.6.0 Found existing installation: urllib3 1.23 Uninstalling urllib3-1.23: Successfully uninstalled urllib3-1.23 Successfully installed requests-2.6.0 urllib3-1.23 You are using pip version 9.0.3, however version 19.1.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. [root@104 ~]# python run620.py 现在使用的是第1页代理IP 第1次访问 ......

最新回复(0)