Python 学习记录Pycharm导入第三方包报错问题……ValueError: check_hostname requires server_hostname
Posted 空中旋转篮球
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 学习记录Pycharm导入第三方包报错问题……ValueError: check_hostname requires server_hostname相关的知识,希望对你有一定的参考价值。
1.问题描述
导入任何包都包以下错误:
Try to run this command from the system terminal. Make sure that you use the correct version of 'pip' installed for your Python interpreter located at 'D:\\pythonProject\\venv\\Scripts\\python.exe'.
ERROR: Exception:
Traceback (most recent call last):
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\cli\\base_command.py", line 180, in _main
status = self.run(options, args)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\cli\\req_command.py", line 204, in wrapper
return func(self, options, args)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\commands\\install.py", line 319, in run
reqs, check_supported_wheels=not options.target_dir
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\resolver.py", line 128, in resolve
requirements, max_rounds=try_to_avoid_resolution_too_deep
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\resolvelib\\resolvers.py", line 473, in resolve
state = resolution.resolve(requirements, max_rounds=max_rounds)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\resolvelib\\resolvers.py", line 341, in resolve
name, crit = self._merge_into_criterion(r, parent=None)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\resolvelib\\resolvers.py", line 172, in _merge_into_criterion
if not criterion.candidates:
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\resolvelib\\structs.py", line 139, in __bool__
return bool(self._sequence)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\found_candidates.py", line 143, in __bool__
return any(self)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\found_candidates.py", line 129, in <genexpr>
return (c for c in iterator if id(c) not in self._incompatible_ids)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\found_candidates.py", line 30, in _iter_built
for version, func in infos:
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\resolution\\resolvelib\\factory.py", line 272, in iter_index_candidate_infos
hashes=hashes,
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\index\\package_finder.py", line 879, in find_best_candidate
candidates = self.find_all_candidates(project_name)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\index\\package_finder.py", line 824, in find_all_candidates
page_candidates = list(page_candidates_it)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\index\\sources.py", line 134, in page_candidates
yield from self._candidates_from_page(self._link)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\index\\package_finder.py", line 783, in process_project_url
html_page = self._link_collector.fetch_page(project_url)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\index\\collector.py", line 512, in fetch_page
return _get_html_page(location, session=self.session)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\index\\collector.py", line 422, in _get_html_page
resp = _get_html_response(url, session=session)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\index\\collector.py", line 137, in _get_html_response
"Cache-Control": "max-age=0",
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\requests\\sessions.py", line 555, in get
return self.request('GET', url, **kwargs)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_internal\\network\\session.py", line 449, in request
return super().request(method, url, *args, **kwargs)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\requests\\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\requests\\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\cachecontrol\\adapter.py", line 53, in send
resp = super(CacheControlAdapter, self).send(request, **kw)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\requests\\adapters.py", line 449, in send
timeout=timeout
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\urllib3\\connectionpool.py", line 696, in urlopen
self._prepare_proxy(conn)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\urllib3\\connectionpool.py", line 964, in _prepare_proxy
conn.connect()
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\urllib3\\connection.py", line 359, in connect
conn = self._connect_tls_proxy(hostname, conn)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\urllib3\\connection.py", line 506, in _connect_tls_proxy
ssl_context=ssl_context,
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\urllib3\\util\\ssl_.py", line 432, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
File "D:\\pythonProject\\venv\\lib\\site-packages\\pip\\_vendor\\urllib3\\util\\ssl_.py", line 474, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock)
File "C:\\Users\\RS001\\AppData\\Local\\Programs\\Python\\Python37\\lib\\ssl.py", line 423, in wrap_socket
session=session
File "C:\\Users\\RS001\\AppData\\Local\\Programs\\Python\\Python37\\lib\\ssl.py", line 827, in _create
raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname
2.问题来源
搜索相关资料显示是因为打开了vpn引起的,电脑确实默认开启了vpn,因此关闭vpn再测试一下是否确是由此引起。参考:python pip安装包时出现 ValueError: check_hostname requires server_hostname 错误 解决方法
点击退出vpn程序,关闭vpn代理,继续再次试一下:
确实进入下载状态了
不过继续报另外错误:
具体如下:
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x0000018D893221C8>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))': /simple/numpy/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x0000018D89322848>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))': /simple/numpy/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x0000018D89322D08>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))': /simple/numpy/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x0000018D8933E1C8>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))': /simple/numpy/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x0000018D8933E5C8>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))': /simple/numpy/
ERROR: Could not find a version that satisfies the requirement numpy==1.21.0rc2 (from versions: none)
ERROR: No matching distribution found for numpy==1.21.0rc2
这里选择是默认版本:
由于python用的3.7. 根据错误提示,可能是版本原因,这里选择较低版本继续测试以上问题:
就是用之前安装成功做的1.19.5版本试试:
依然报同样错误。
换一个数据源试试,问题依旧。
使用多个第三方库都不行,这些库的网址能够正常访问。根据以上测试估计应该还是网络的问题。
3.解决办法
3.1使用--trusted-host pypi.python.org”允许此警告。
参考:在python中安装包出现Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))
在三方库连接后面加上以上代码,如下:
如果是命令行窗口可直接使用:
pip install numpy -i https://pypi.python.org/simple --trusted-host pypi.python.org
依旧不行。
看来还是可能被网络中断了吧,用的校园网(教育网),断了校园网,usb连接手机共享网络,重复操作,手机网络是中国移动网络。
直接就安装成功了,没再报错,校园网是个坑,但是又有个问题,有的电脑又安装成功了,因此怀疑还可能和电脑的安全设置有关系。
结论就是:网络原因引起该问题
查看一下防火墙:
默认未勾选,都勾选上试试:
依旧不行,关闭防火墙也不行。
以上是关于Python 学习记录Pycharm导入第三方包报错问题……ValueError: check_hostname requires server_hostname的主要内容,如果未能解决你的问题,请参考以下文章