前言

上一篇文章 深度学习环境配置——Miniconda(for Windows)
介绍了基本的安装流程,但是本人在后续使用过程中,发现有些包无法导入,其根本原因是安装版本不适配。本篇博客给出解决办法。

一、版本要求

动手学深度学习——安装 中,想要运行网站中给出的pytorch代码,需要的相关安装配置以及版本如下:

conda create --name d2l python=3.9 -y
pip install torch==1.12.0
pip install torchvision==0.13.0
pip install d2l==0.17.6
from setuptools import setup, find_packages
import d2l

requirements = [
    'jupyter==1.0.0',
    'numpy==1.21.5',
    'matplotlib==3.5.1',
    'requests==2.25.1',
    'pandas==1.2.4'
]

setup(
    name='d2l',
    version=d2l.__version__,
    python_requires='>=3.5',
    author='D2L Developers',
    author_email='d2l.devs@gmail.com',
    url='https://d2l.ai',
    description='Dive into Deep Learning',
    license='MIT-0',
    packages=find_packages(),
    zip_safe=True,
    install_requires=requirements,
)

二、安装步骤

1)创建一个名为d2l_py39的虚拟环境,python版本号为3.9

conda create -n d2l_py39 python=3.9 -y

2)激活虚拟环境

conda activate d2l_py39

3)安装核心的深度学习库,把 PyTorch 和 torchvision 核心库装好

pip install torch==1.12.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install torchvision==0.13.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

4)安装jupyter notebook和用于注册内核的 ipykernel

pip install jupyter notebook -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install ipykernel -i https://pypi.tuna.tsinghua.edu.cn/simple

5)在 Jupyter 工具和核心库都安装好后,再将当前环境注册为一个可用的内核

#这里要注意的是,D:\whydown\miniconda3\envs\d2l_py39是你当前虚拟环境的位置,需要使用conda env list命令去查看当前环境路径,记得修改成自己对应的
python -m ipykernel install --name d2l_py39 --display-name "Python (d2l_py39)" --prefix D:\whydown\miniconda3\envs\d2l_py39

6)安装课程库 d2l,d2l 库依赖于前面的所有组件,放在最后安装,可以减少出错概率

pip install d2l==0.17.6 -i https://pypi.tuna.tsinghua.edu.cn/simple

7)启动jupyter notebook

jupyter notebook

8) 启动jupyter notebook后可能出现的问题及解决办法

有可能出现版本 traitlets 包版本过高的问题,需要使用以下命令降低 traitlets 版本,并再次启动jupyter notebook,就能正常使用了

pip install "traitlets<5.10.0" -i https://pypi.tuna.tsinghua.edu.cn/simple

三、其他命令,帮助理解

查看已安装的 Jupyter 内核

jupyter kernelspec list

查看所有conda环境

conda env list

查看已安装包torch

conda list | findstr torch  

查看conda版本号

conda --version

激活环境

conda activate d2l_py39

激活环境

conda deactivate

验证是否关联正确环境,在 Jupyter Notebook 中新建一个代码单元格,运行以下代码:

import sys
print(sys.executable)  # 查看当前使用的Python路径

将当前环境注册为 Jupyter 内核(名称自定义)

python -m ipykernel install --user --name=你的环境名

验证环境与库的对应关系

如果输出的解释器路径包含 envs\d2l_py39,且能正常打印 d2l_py39 版本,说明环境和库已成功对应。

import sys
import d2l
print("Python 解释器路径:", sys.executable)
print("d2l 库版本:", d2l.__version__)

查看已安装包

若用 pip 安装过 torch:bash

pip list | findstr torch  # Windows

若用 conda 安装过 torch:bash

conda list | findstr torch  # Windows

如果输出中包含 torch 及其版本号,说明已安装


祝大家安装成功!

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐