业务场景
导入requests库时报错,单独离线下载安装requests,发现仍然报错,问题在于requests库有其他依赖库。
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x0000026068302890>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed')': /simple/chardet/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x0000026068303E50>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed')': /simple/chardet/
INFO: pip is looking at multiple versions of requests to determine which version is compatible with other requirements. This could take a while.
ERROR: Could not find a version that satisfies the requirement chardet<3.1.0,>=3.0.2 (from requests) (from versions: none)
ERROR: No matching distribution found for chardet<3.1.0,>=3.0.2
分析
然而怎么指定requests运行库需要哪些依赖项,依赖项的版本又是什么要求呢?
使用以下命令查看运行库的版本
pip list
找到需要查看依赖关系的库,输入以下命令,查看其依赖关系:
pip show +依赖库名称
例如:
python310 -m pip show requests ==2.28.2
python310 -m pip show requests
根据输出信息,requests
库的版本号是2.28.2
。而requests
所依赖的其他库为certifi
、charset-normalizer
、idna
和urllib3
。这些信息显示在输出的Requires
字段中。
在输出结果中,查找Requires
字段,即可看到该库的依赖关系。例如:
Requires: certifi, charset-normalizer, idna, urllib3
那么具体的版本号是多少呢??certifi, charset-normalizer, idna, urllib3
可以继续使用以下命令
pip show certifi, charset-normalizer, idna, urllib3
C:\Users\Administrator\Desktop>python310 -m pip show certifi
Name: certifi
Version: 2023.5.7
Summary: Python package for providing Mozilla's CA Bundle.
Home-page: https://github.com/certifi/python-certifi
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: MPL-2.0
Location: d:\program files (x86)\python310\lib\site-packages
Requires:
Required-by: requests, sentry-sdk
C:\Users\Administrator\Desktop>python310 -m pip show charset-normalizer
Name: charset-normalizer
Version: 2.1.1
Summary: The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet.
Home-page: https://github.com/ousret/charset_normalizer
Author: Ahmed TAHRI @Ousret
Author-email: ahmed.tahri@cloudnursery.dev
License: MIT
Location: d:\program files (x86)\python310\lib\site-packages
Requires:
Required-by: aiohttp, requests
C:\Users\Administrator\Desktop>python310 -m pip show idna
Name: idna
Version: 3.4
Summary: Internationalized Domain Names in Applications (IDNA)
Home-page:
Author:
Author-email: Kim Davies <kim@cynosure.com.au>
License:
Location: d:\program files (x86)\python310\lib\site-packages
Requires:
Required-by: requests, yarl
C:\Users\Administrator\Desktop>python310 -m pip show urllib3
Name: urllib3
Version: 1.26.12
Summary: HTTP library with thread-safe connection pooling, file post, and more.
Home-page: https://urllib3.readthedocs.io/
Author: Andrey Petrov
Author-email: andrey.petrov@shazow.net
License: MIT
Location: c:\users\administrator\appdata\roaming\python\python310\site-packages
Requires:
Required-by: botocore, requests, responses, sentry-sdk
C:\Users\Administrator\Desktop>
整理下来的版本号:
Name: requests
Version: 2.28.2
Name: certifi
Version: 2023.5.7
Name: charset-normalizer
Version: 2.1.1
Name: idna
Version: 3.4
Name: urllib3
Version: 1.26.12
另一种解决办法
如果你不知道具体的版本号,但需要下载并安装requests
及其依赖项,可以采用以下方法:
- 使用
pip
的download
命令下载requests
及其依赖项的whl文件。在联网的计算机上执行以下命令:
pip download requests--only-binary=:all: --platform any --python-version 3.10
保存到指定目录
可以通过在pip download
命令中使用-d
(或--dest
)选项来指定下载的whl文件保存的目录。正确的命令如下:
pip download requests --only-binary=:all: --platform any --python-version 3.10 -d C:\path\to\destination\directory
pip download requests --only-binary=:all: --platform any --python-version 3.10 -d C:\requests0725
在这个命令中,-d
选项后面的C:\path\to\destination\directory
应该替换为希望保存whl文件的实际目录路径。
这样,pip
会下载requests
库及其所有依赖的whl文件,并将它们保存在指定的目录中。然后,你可以将这些whl文件复制到离线计算机上,并按正确的依赖关系顺序手动安装它们,以确保requests
及其所有依赖可以在离线环境中正常工作。
根据命令输出,下载requests
库及其依赖项出现了错误。错误信息显示找不到满足条件的requests
版本。这可能是由于指定的--only-binary=:all:
选项导致的,它会限制只下载二进制wheel文件,而requests
库通常是一个源代码包,可能不包含预编译的wheel文件。
为了解决这个问题,可以使用以下命令来下载requests
及其依赖项的源代码包:
python310 -m pip download requests -d C:\requests0725
这个命令会下载requests
及其依赖项的源代码包,并保存在C:\requests0725
目录中。
然后,在离线计算机上,可以进入保存whl文件的目录,并按正确的依赖关系顺序手动安装这些包
-
pip
会下载requests
及其依赖项的whl文件,并保存在当前目录。你需要找到所有下载的whl文件并将它们复制到离线计算机的一个目录中。 -
在离线计算机上,进入保存whl文件的目录,并按正确的依赖关系顺序手动安装这些包:
python -m pip install urllib3-xxx.whl # 根据实际下载的文件名进行替换
python -m pip install certifi-xxx.whl # 根据实际下载的文件名进行替换
python -m pip install idna-xxx.whl # 根据实际下载的文件名进行替换
python -m pip install charset-normalizer-xxx.whl # 根据实际下载的文件名进行替换
python -m pip install requests-xxx.whl # 根据实际下载的文件名进行替换
注意:这里的xxx
应该是实际下载的文件名,根据你下载的文件名进行替换。
通过以上步骤,你应该能够在离线计算机上安装requests
及其所有依赖项,从而成功使用该库。如果需要的话,你也可以手动查找最新版本的requests
及其依赖项的whl文件进行下载和安装。
结果
E:\software\python\Lib\site-packages\user-package2023\requests2.28.2>python
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
E:\software\python\Lib\site-packages\user-package2023\requests2.28.2>python -m pip install requests-2.28.2-py3-none-any.whl
Processing e:\software\python\lib\site-packages\user-package2023\requests2.28.2\requests-2.28.2-py3-none-any.whl
Requirement already satisfied: charset-normalizer<4,>=2 in e:\software\python\lib\site-packages (from requests==2.28.2) (2.0.6)
Requirement already satisfied: idna<4,>=2.5 in e:\software\python\lib\site-packages (from requests==2.28.2) (2.8)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in e:\software\python\lib\site-packages (from requests==2.28.2) (1.26.3)
Requirement already satisfied: certifi>=2017.4.17 in e:\software\python\lib\site-packages (from requests==2.28.2) (2020.12.5)
Installing collected packages: requests
Successfully installed requests-2.28.2