PyCharm安装PyTorch时的一些坑

文章描述了检查和安装GPU版PyTorch的步骤,包括查看CUDA版本,更新NVIDIA驱动,以及解决安装过程中遇到的`ValueError:check_hostnamerequiresserver_hostname`错误,该错误通常由网络代理引起,关闭代理后问题可解决。
摘要由CSDN通过智能技术生成

先判断下是否可以安装GPU版本的pytorch

计算机右击–>管理–>设备管理器–>显示适配器
在这里插入图片描述

定CUDA版本

最好先更新驱动 win+R键 -> 输入cmd -> 输入下面指令查看相关信息

nvidia-smi

如果提示非内部外部指令,cd到C:\Program Files\NVIDIA Corporation\NVSMI目录,再执行命令

cd C:\Program Files\NVIDIA Corporation\NVSMI
nvidia-smi

在这里插入图片描述记住这个版本号。
我的一开始根本没有红框里的内容,如果你也是,就需要更新下NVIDIA驱动

安装CUDA/更新NVIDIA驱动

(如果前一步你看到了CUDA Version,这一步就可以不做,除非Version版本太低)
根据之前计算机右击–>管理–>设备管理器–>显示适配器,明确电脑自带的显卡版本
在这里插入图片描述
然后到官网下载合适的驱动
驱动下载网址:https://www.nvidia.cn/geforce/drivers/
对应自己的显卡信息,查找对应的驱动版本,下载前,先浏览产品的支持列表,再搜索全部选择一个下载
以管理员身份运行下载的exe文件,这是临时解压文件的路径,后续安装完成后会删除,所以就直接按照默认路径即可,按ok。
在这里插入图片描述下载临时文件完成后,进入安装界面,然后一路按照默认选项按继续或下一步即可
在这里插入图片描述在这里插入图片描述在这里插入图片描述(其实我自己安装失败了,好像是NVIDIA update code安装失败,NVIDIA GeForce Experience直接未安装,但是好像不妨碍。之后试了一下nvidia-smi发现可以看到 CUDA Version了。)

然后,在桌面空白处右键,选择NVIDIA控制面板。
在这里插入图片描述

然后选择管理3D设置,再选择高性能NVIDIA处理器,点击应用即可。

在这里插入图片描述NVIDIV驱动程序安装完成,可以通过nvidia-smi命令查看本机CUDA版本。如下:
在这里插入图片描述

选择Pytorch版本

打开Pytorch 官网,选择合适的版本。官网地址:https://pytorch.org/get-started/locally/
因为我们环境用的不是anaconda,那第三行我们就选择Pip,第五行的Computer Platform选择比自己版本号低的就可以了,复制最下面的指令。
在这里插入图片描述

PyCharm安装PyTorch

打开pycharm,有建好的项目,或者新建一个项目都可以,点最下面一栏的terminal,把上面复制的指令粘贴上去回车执行就好了。
在这里插入图片描述尽量和我一样,尤其是红框里的,不要是 venv D:\pycharmprojection\deep_learn>。
如果是venv D:\pycharmprojection\deep_learn>,可以试着关掉Local,再打开一次Terminal试试。

我一开始有一个报错: ValueError: check_hostname requires server_hostname
具体如下:

D:\pycharmprojection\deep_learn>pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
Looking in indexes: https://download.pytorch.org/whl/cu117
ERROR: Exception:
Traceback (most recent call last):
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\cli\base_command.py", line 173, in _main
    status = self.run(options, args)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\cli\req_command.py", line 203, in wrapper
    return func(self, options, args)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\commands\install.py", line 315, in run
    requirement_set = resolver.resolve(
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\resolution\resolvelib\resolver.py", line 94, in resolve
    result = self._result = resolver.resolve(
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 472, in resolve
    state = resolution.resolve(requirements, max_rounds=max_rounds)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\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\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 172, in _add_to_criteria
    if not criterion.candidates:
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\resolvelib\structs.py", line 151, in __bool__
    return bool(self._sequence)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 140, in __bool__
    return any(self)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\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\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 29, in _iter_built
    for version, func in infos:
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\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\emma.zrf\appdata\local\programs\python\python38\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\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\index\package_finder.py", line 798, in find_all_candidates
    page_candidates = list(page_candidates_it)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\index\sources.py", line 134, in page_candidates
    yield from self._candidates_from_page(self._link)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\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\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\index\collector.py", line 490, in fetch_page
    return _get_html_page(location, session=self.session)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\index\collector.py", line 400, in _get_html_page
    resp = _get_html_response(url, session=session)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\index\collector.py", line 115, in _get_html_response
    resp = session.get(
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\requests\sessions.py", line 555, in get
    return self.request('GET', url, **kwargs)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_internal\network\session.py", line 454, in request
    return super().request(method, url, *args, **kwargs)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\cachecontrol\adapter.py", line 53, in send
    resp = super(CacheControlAdapter, self).send(request, **kw)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\urllib3\connectionpool.py", line 696, in urlopen
    self._prepare_proxy(conn)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\urllib3\connectionpool.py", line 964, in _prepare_proxy
    conn.connect()
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\urllib3\connection.py", line 359, in connect
    conn = self._connect_tls_proxy(hostname, conn)
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\site-packages\pip\_vendor\urllib3\connection.py", line 500, in _connect_tls_proxy
    return ssl_wrap_socket(
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\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\emma.zrf\appdata\local\programs\python\python38\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\emma.zrf\appdata\local\programs\python\python38\lib\ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "c:\users\emma.zrf\appdata\local\programs\python\python38\lib\ssl.py", line 997, in _create
    raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname

ValueError: check_hostname requires server_hostname通常是因为版本冲突等原因,查遍大神们总结的经验后,发现是由网络代理导致的问题(也就是vpn,我当时开了梯子)。

关掉VPN后问题解决
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值