目录
解决AttributeError: module 'd2l.torch' has no attribute 'load_data_time_machine'_attributeerror: module 'torch' has no attribute 'l-CSDN博客文章浏览阅读5.5k次,点赞8次,收藏3次。文章描述了在使用D2L库时遇到的AttributeError,原因在于使用的d2l版本过高。解决方法是通过pip将d2l降级到0.17.5版本。https://blog.csdn.net/Rhett_Butler0922/article/details/137793716?fromshare=blogdetail&sharetype=blogdetail&sharerId=137793716&sharerefer=PC&sharesource=qq_52122048&sharefrom=from_link解决AttributeError: module 'd2l.torch' has no attribute 'load_data_time_machine'_attributeerror: module 'torch' has no attribute 'l-CSDN博客
报错
d2l相当于一个收集函数的包,只要在一个py文件中定义函数def func():时在冒号后加上了注释#@save,那就可以在另一个任意路径的py文件下以d2l.pytorch.func调用这个函数。

解决:
我的:

或者
(E:\deeplearning202604) C:\Users\Lenovo>python -c "import d2l; print(d2l.__version__)"
1.0.3
(E:\deeplearning202604) C:\Users\Lenovo>
| 项目 | 信息 |
|---|---|
| PyPI 包名 | d2l(即 pip install d2l) |
| 稳定版本 | 0.17.6(官方配套书版) |
| 最低推荐版本 | ≥ 0.17.5(低于此版缺失 d2l.Image等 API) |
| 安装命令 | pip install d2l==0.17.6 |
(E:\deeplearning202604) C:\Users\Lenovo>python --version
Python 3.9.25
(E:\deeplearning202604) C:\Users\Lenovo>
(E:\deeplearning202604) C:\Users\Lenovo>python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"
2.6.0+cu126
True
1. 只装 d2l 本体,不装任何依赖(torch、numpy 等都不动)否则会自动修改python和torch等版本
pip install d2l==0.17.5 --no-deps
python
(E:\deeplearning202604) C:\Users\Lenovo>pip install d2l==0.17.5 --no-deps
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting d2l==0.17.5
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/6a/be/eb453dbdd80d0563e2a6dac4aff8b76b0c24641d20b05e85b560f90282f9/d2l-0.17.5-py3-none-any.whl (82 kB)
Installing collected packages: d2l
Attempting uninstall: d2l
Found existing installation: d2l 1.0.3
Uninstalling d2l-1.0.3:
Successfully uninstalled d2l-1.0.3
Successfully installed d2l-0.17.5
(E:\deeplearning202604) C:\Users\Lenovo>python -c "import d2l; print(d2l.__version__)"
0.17.5
(E:\deeplearning202604) C:\Users\Lenovo>python -c "import torch; print(torch.__version__)"
2.6.0+cu126
使用:



自己也可以创建一个类似d2l这样子的模块

| 要点 | 说明 |
|---|---|
| 不要与 Python 内置名称冲突 | 比如别注册一个叫 list的函数,否则会把原生的 list覆盖掉。 |
| 模块热更新 | 如果在 Jupyter 里修改了 mylib.py,需要 import importlib; importlib.reload(mylib)才能生效。 |
| 跨项目共享 | 把 mylib.py放到项目根目录,或者做成 pip 包,就能到处用了。 |