解决Python因网络问题导致安装依赖库时的报错问题

现象

在终端运行以下指令

pip install pyautogui pillow keyboard

出现报错,终端信息如下:

PS D:\code\Python> pip install pyautogui pillow keyboard
Collecting pyautogui
  Using cached PyAutoGUI-0.9.54.tar.gz (61 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting pillow
  Using cached pillow-11.1.0-cp39-cp39-win_amd64.whl.metadata (9.3 kB)
Collecting keyboard
  Using cached keyboard-0.13.5-py3-none-any.whl.metadata (4.0 kB)
Collecting pymsgbox (from pyautogui)
  Using cached PyMsgBox-1.0.9.tar.gz (18 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting pytweening>=1.0.4 (from pyautogui)
  Using cached pytweening-1.2.0.tar.gz (171 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting pyscreeze>=0.1.21 (from pyautogui)
  Using cached pyscreeze-1.0.1.tar.gz (27 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting pygetwindow>=0.0.5 (from pyautogui)
  Downloading PyGetWindow-0.0.9.tar.gz (9.7 kB)

[notice] A new release of pip is available: 24.3.1 -> 25.0
[notice] To update, run: python.exe -m pip install --upgrade pip
ERROR: Exception:
Traceback (most recent call last):
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_vendor\urllib3\response.py", line 438, in _error_catcher
    yield
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\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\zando\AppData\Local\Programs\Python\Python39\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\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 98, in read
    data: bytes = self.__fp.read(amt)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 463, in read
    n = self.readinto(b)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 507, in readinto
    n = self.fp.readinto(b)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\socket.py", line 704, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\cli\base_command.py", line 105, in _run_wrapper
    status = _inner_run()
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\cli\base_command.py", line 96, in _inner_run
    return self.run(options, args)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\cli\req_command.py", line 67, in wrapper
    return func(self, options, args)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\commands\install.py", line 379, in run
    requirement_set = resolver.resolve(
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\resolution\resolvelib\resolver.py", line 95, in resolve
    result = self._result = resolver.resolve(
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 546, in resolve
    state = resolution.resolve(requirements, max_rounds=max_rounds)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 427, in resolve
    failure_causes = self._attempt_to_pin_criterion(name)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 239, in _attempt_to_pin_criterion
    criteria = self._get_updated_criteria(candidate)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 230, in _get_updated_criteria
    self._add_to_criteria(criteria, requirement, parent=candidate)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 173, in _add_to_criteria
    if not criterion.candidates:
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_vendor\resolvelib\structs.py", line 156, in __bool__
    return bool(self._sequence)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 174, in __bool__
    return any(self)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 162, in <genexpr>
    return (c for c in iterator if id(c) not in self._incompatible_ids)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 53, in _iter_built        
    candidate = func()
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\resolution\resolvelib\factory.py", line 187, in _make_candidate_from_link  
    base: Optional[BaseCandidate] = self._make_base_candidate_from_link(
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\resolution\resolvelib\factory.py", line 233, in _make_base_candidate_from_link
    self._link_candidate_cache[link] = LinkCandidate(
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 304, in __init__
    super().__init__(
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 159, in __init__
    self.dist = self._prepare()
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 236, in _prepare
    dist = self._prepare_distribution()
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 315, in _prepare_distribution   
    return preparer.prepare_linked_requirement(self._ireq, parallel_builds=True)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\operations\prepare.py", line 527, in prepare_linked_requirement
    return self._prepare_linked_requirement(req, parallel_builds)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\operations\prepare.py", line 598, in _prepare_linked_requirement
    local_file = unpack_url(
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\operations\prepare.py", line 170, in unpack_url
    file = get_http_url(
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\operations\prepare.py", line 111, in get_http_url
    from_path, content_type = download(link, temp_dir.path)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\network\download.py", line 148, in __call__
    for chunk in chunks:
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_internal\network\utils.py", line 65, in response_chunks
    for chunk in response.raw.stream(
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_vendor\urllib3\response.py", line 622, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\site-packages\pip\_vendor\urllib3\response.py", line 587, in read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\lib\contextlib.py", line 137, in __exit__
    self.gen.throw(typ, value, traceback)
  File "C:\Users\zando\AppData\Local\Programs\Python\Python39\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.

原因

默认的pypi.org服务器在国外,国内访问可能较慢

改为执行如下命令,使用国内镜像源加速下载:

pip install pyautogui pillow keyboard -i https://pypi.tuna.tsinghua.edu.cn/simple

改镜像源为清华镜像源,还有常用镜像源如下:

阿里云:

https://mirrors.aliyun.com/pypi/simple

豆瓣:

https://pypi.douban.com/simple

换源之后成功安装依赖库,终端信息如下:

PS D:\code\Python> pip install pyautogui pillow keyboard -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: pyautogui in c:\users\zando\appdata\local\programs\python\python39\lib\site-packages (0.9.54)
Requirement already satisfied: pillow in c:\users\zando\appdata\local\programs\python\python39\lib\site-packages (11.1.0)
Collecting keyboard
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/55/88/287159903c5b3fc6d47b651c7ab65a54dcf9c9916de546188a7f62870d6d/keyboard-0.13.5-py3-none-any.whl (58 kB)
Requirement already satisfied: pymsgbox in c:\users\zando\appdata\local\programs\python\python39\lib\site-packages (from pyautogui) (1.0.9)
Requirement already satisfied: pytweening>=1.0.4 in c:\users\zando\appdata\local\programs\python\python39\lib\site-packages (from pyautogui) (1.2.0)
Requirement already satisfied: pyscreeze>=0.1.21 in c:\users\zando\appdata\local\programs\python\python39\lib\site-packages (from pyautogui) (1.0.1)
Requirement already satisfied: pygetwindow>=0.0.5 in c:\users\zando\appdata\local\programs\python\python39\lib\site-packages (from pyautogui) (0.0.9)
Requirement already satisfied: mouseinfo in c:\users\zando\appdata\local\programs\python\python39\lib\site-packages (from pyautogui) (0.1.3)
Requirement already satisfied: pyrect in c:\users\zando\appdata\local\programs\python\python39\lib\site-packages (from pygetwindow>=0.0.5->pyautogui) (0.2.0)
Requirement already satisfied: pyperclip in c:\users\zando\appdata\local\programs\python\python39\lib\site-packages (from mouseinfo->pyautogui) (1.9.0)
Installing collected packages: keyboard
Successfully installed keyboard-0.13.5

[notice] A new release of pip is available: 24.3.1 -> 25.0
[notice] To update, run: python.exe -m pip install --upgrade pip

作者:zeandon

物联沃分享整理
物联沃-IOTWORD物联网 » 解决Python因网络问题导致安装依赖库时的报错问题

发表回复