无法安装 pytz 库

cannot install the pytz library

所以我想创建一个按时区获取时间的应用程序。我读过 pytz 库是用于时区的,为了获取日期和时间,我们使用 datetime 库。但我无法导入 pytz 库。网上说它不是标准 python 库之一,所以我用这个命令手动下载了它:

pip install pytz

但它给出了这个错误:

ValueError: check_hostname requires server_hostname

我认为这个错误与我的网络有关。 我搜索了这个错误,发现是因为代理或 VPN。但我的是关闭的。 你能帮我吗?感激不尽

编辑

我的意思是你能检查它是否正确安装在你的设备上吗?用我上面说的命令。

编辑@MisterMiyagi

ERROR: Exception:
Traceback (most recent call last):
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\cli\base_command.py", line 173, in _main
    status = self.run(options, args)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\cli\req_command.py", line 203, in wrapper
    return func(self, options, args)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\commands\install.py", line 315, in run
    requirement_set = resolver.resolve(
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\resolution\resolvelib\resolver.py", line 94, in resolve
    result = self._result = resolver.resolve(
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 472, in resolve       
    state = resolution.resolve(requirements, max_rounds=max_rounds)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 341, in resolve       
    self._add_to_criteria(self.state.criteria, r, parent=None)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 172, in _add_to_criteria
    if not criterion.candidates:
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\resolvelib\structs.py", line 151, in __bool__        
    return bool(self._sequence)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 140, in __bool__
    return any(self)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 128, in <genexpr>
    return (c for c in iterator if id(c) not in self._incompatible_ids)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 29, in _iter_built
    for version, func in infos:
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\resolution\resolvelib\factory.py", line 272, in iter_index_candidate_infos
    result = self._finder.find_best_candidate(
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\index\package_finder.py", line 851, in find_best_candidate
    candidates = self.find_all_candidates(project_name)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\index\package_finder.py", line 798, in find_all_candidates
    page_candidates = list(page_candidates_it)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\index\sources.py", line 134, in page_candidates    
    yield from self._candidates_from_page(self._link)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\index\package_finder.py", line 758, in process_project_url
    html_page = self._link_collector.fetch_page(project_url)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\index\collector.py", line 490, in fetch_page       
    return _get_html_page(location, session=self.session)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\index\collector.py", line 400, in _get_html_page   
    resp = _get_html_response(url, session=session)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\index\collector.py", line 115, in _get_html_response
    resp = session.get(
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\requests\sessions.py", line 555, in get
    return self.request('GET', url, **kwargs)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\network\session.py", line 454, in request
    return super().request(method, url, *args, **kwargs)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\cachecontrol\adapter.py", line 53, in send
    resp = super(CacheControlAdapter, self).send(request, **kw)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\urllib3\connectionpool.py", line 696, in urlopen     
    self._prepare_proxy(conn)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\urllib3\connectionpool.py", line 964, in _prepare_proxy
    conn.connect()
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\urllib3\connection.py", line 359, in connect
    conn = self._connect_tls_proxy(hostname, conn)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\urllib3\connection.py", line 500, in _connect_tls_proxy
    return ssl_wrap_socket(
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\urllib3\util\ssl_.py", line 453, in ssl_wrap_socket  
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_vendor\urllib3\util\ssl_.py", line 495, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock)
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 512, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1027, in _create
    raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname

命令 pip install pytz 给出以下输出并使用 python3 环境安装正常:

$ pip install pytz
Collecting pytz
  Downloading pytz-2022.1-py2.py3-none-any.whl (503 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 503.5/503.5 KB 7.6 MB/s eta 0:00:00
Installing collected packages: pytz
Successfully installed pytz-2022.1

您可以选择在离线模式下下载并安装 pytz 作为解决方法 (inspired by this guide):

  1. 下载 .whl file of pytz 并将其放入名为 requirements.
  2. 的目录中
mkdir requirements
cd requirements
wget https://files.pythonhosted.org/packages/60/2e/dec1cc18c51b8df33c7c4d0a321b084cf38e1733b98f9d15018880fb4970/pytz-2022.1-py2.py3-none-any.whl
  1. 制作requirements.txt内容为pytz:
echo "pytz" > requirements.txt
  1. 使用 pip 安装 pytz 包:
pip install --no-deps --no-index --find-links=. -r requirements.txt

其他解决方案可能会在 中找到。最值得注意的解决方案可能是:

pip install pytz -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

请注意,这会使 pip 使用 HTTP 连接而不是 HTTPS 连接。虽然 PyPi 不支持 HTTP,we can exploit douban's mirror which supports HTTP to circumvent this limitation.