在使用Python编程的时候,通常需要添加一些扩展库。但是又不知如何安装,按照网上说的运行以后还是报错。
前言
本文章来自博主 SkyMax(スキマックス) 编写 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明
正文
在编程的时候,需要添加一些扩展库。但是又不知如何安装,网上说的都是:
pip install openpyxl tqdmIMPORTANT上面代码只对MacOS&Linux有效👆
可是对如0基础的人太难了吧?通用的解决办法是:在命令行终端( Windows 是 CMD 或 PowerShell,macOS/Linux 是 Terminal)中输入以下命令来安装所需的库。
报错
但如果你是Windows系统(Mac&Linux可跳过,除非想学知识)就会报错(只针对WindowsShell)
pip : The term 'pip' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. + pip install msoffcrypto-tool -i https://pypi.tuna.tsinghua.edu.cn/sim ... + FullyQualifiedErrorId : CommandNotFoundException这个报错说白了就是WindowsShell不能直接认到这个’pip’命令,也没有将’pip’添加至环境变量中。 那我们应如何解决呢?
怎样解决
在前面加上py或python(代表声明Python)后面加上“-m pip”,加上install代表我要安装库还是其他的镜像来源……(最好不要超过3个,用空格代表第一个库的名称已经完结)
合起来为:
py -m pip install openpyxl tqdm按下“Enter键”后,也可能像我一样报错:
Downloading openpyxl-3.1.5-py2.py3-none-any.whl (250 kB) ━━━━╸━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 30.7/250.9 kB 5.9 kB/s eta 0:00:38ERROR: Exception:Traceback (most recent call last): File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 438, in _error_catcher yield File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 561, in read data = self._fp_read(amt) if not fp_closed else b"" ^^^^^^^^^^^^^^^^^^ File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 527, in _fp_read return self._fp.read(amt) if amt is not None else self._fp.read() ^^^^^^^^^^^^^^^^^^ File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 90, in read data = self.__fp.read(amt) ^^^^^^^^^^^^^^^^^^^ File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\http\client.py", line 479, in read s = self.fp.read(amt) ^^^^^^^^^^^^^^^^^ File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\socket.py", line 707, in readinto return self._sock.recv_into(b) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\ssl.py", line 1253, in recv_into return self.read(nbytes, buffer) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\ssl.py", line 1105, in read return self._sslobj.read(len, buffer) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^TimeoutError: The read operation timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\cli\base_command.py", line 180, in exc_logging_wrapper status = run_func(*args) ^^^^^^^^^^^^^^^ File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\cli\req_command.py", line 248, in wrapper return func(self, options, args) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\commands\install.py", line 377, in run requirement_set = resolver.resolve( ^^^^^^^^^^^^^^^^^ File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\resolver.py", line 161, in resolve self.factory.preparer.prepare_linked_requirements_more(reqs) File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\operations\prepare.py", line 565, in prepare_linked_requirements_more self._complete_partial_requirements( File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\operations\prepare.py", line 479, in _complete_partial_requirements for link, (filepath, _) in batch_download: File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\network\download.py", line 183, in __call__ for chunk in chunks: File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\cli\progress_bars.py", line 53, in _rich_progress_bar for chunk in iterable: File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\network\utils.py", line 63, in response_chunks for chunk in response.raw.stream( File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 622, in stream data = self.read(amt=amt, decode_content=decode_content) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 560, in read with self._error_catcher(): File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 158, in __exit__ self.gen.throw(value) File "C:\Users\Asushosu\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 443, in _error_catcher raise ReadTimeoutError(self._pool, None, "Read timed out.")pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
[notice] A new release of pip is available: 23.2.1 -> 25.2[notice] To update, run: python.exe -m pip install --upgrade pip这代表:
-
要么你的网络有问题,网速不佳(现在可以去检查一下了)
-
由于默认的 PyPI 服务器( https://pypi.org ) 在国外,国内访问可能较慢,就超时了。
总的来说:这个错误表明 pip 在下载 openpyxl 时超时(Timeout),可能是由于网络连接不稳定*、下载速度过慢或 Python 包服务器(PyPI)暂时不可访问导致的**。
解决办法1
可以改用国内镜像源(如清华、阿里云、豆瓣等),这里就不详细说了。
操作方法:
在py -m pip install openpyxl tqdm后面接上:-i https://pypi.tuna.tsinghua.edu.cn/simple
如下:
py -m pip install openpyxl tqdm -i https://pypi.tuna.tsinghua.edu.cn/simple或:
py -m pip install openpyxl tqdm -i https://mirrors.aliyun.com/pypi/simple/py -m pip install openpyxl tqdm -i https://pypi.douban.com/simple/解决办法2
增加超时时间
如果网络较慢,可以手动增加 --timeout 参数(默认 15 秒):
WARNING手动(请留意)
py -m pip install --timeout=100 openpyxl tqdmCAUTION默认(谨慎操作)
py -m pip install --default-timeout=100 openpyxl tqdm解决办法3
我个人实际上并不推荐此办法,因为如果你稍微没留意,万一操作错了,那就不可挽救了。我个人实际上并不推荐此办法,因为如果你稍微没留意,万一操作错了,那就不可挽救了。
请手动下载并安装 如果仍然失败,可以 手动下载
.whl 文件安装: 1. 访问Python Package Index (PyPI)搜索openpyxl和tqdm。 2. 下载 .whl 文件(如 openpyxl-3.1.5-py2.py3-none-any.whl)。
3. 进入下载目录,运行: py -m pip install openpyxl-3.1.5-py2.py3-none-any.whl py -m pip install tqdm-4.66.1-py3-none-any.whl py -m pip install openpyxl-3.1.5-py2.py3-none-any.whl py -m pip install tqdm-4.66.1-py3-none-any.whl后记
好了这就是这期文章要分享的全部内容了!非常感谢你能够阅读这篇文章,生活愉快哦~咱们下篇再见~~~
本文来自SkyMax(スキマックス),转载请附上原文出处链接和本声明。