The d2l.ai textbook includes its own Python library, d2l
. Their instructions recommend installing with conda
, a Python package manager that also functions as a general library manager. But I've been burned by conda
a couple times, and prefer pure pip
. How can I install d2l
on Windows with pure pip
? I only need PyTorch, not MxNet or Tensorflow.

- 3,321
- 4
- 40
- 58
1 Answers
Using pip
This is kind of an embarrassing answer to my own question, but I recommend you follow the CPU or GPU instructions provided, following just the PYTORCH
tab:
"C:\Program Files\Python37\Scripts\pip" install torch==1.5.1 torchvision -f https://download.pytorch.org/whl/torch_stable.html
and
"C:\Program Files\Python37\Scripts\pip" install -U d2l
I include the full paths to Python37 here because Python38 is in my path, and torch
, and therefore d2l.torch
only work with 3.5 <= python < 3.8.
Earlier, I had trouble with mxnet being missing, but when I ran pip install -U d2l
again (with the full path to my prefered Python distribution), from d2l import torch as d2l
worked just fine for me.
As standalone script
Sometimes it is convenient just to use the direct files. For example, for installing with torch
, you can just save this file, renaming it to d2l.py
or d2l_torch.py
to distinguish it from the main torch
library. Place it besides your other python script, and then you can import it with import d2l_torch
or whatever you called the script.
If you read the contents of this python file, you will see it is simply a concatenation of all the code samples in the book, with a few convenience aliases at the end of the file that make it easier for the authors to write their book in three languages at the same time. You can often get by without importing d2l at all and just copy-paste the example code that you need into your own code (with citation of course), or learn what torch
commands they are aliasing.
Earlier instructions
In order to import d2l
, you will need torch
installed. Do NOT try to install these with a simple pip install torch
. You need specific versions of both libraries for d2l
to work.
In this example, I use Python 3.7. These instructions assume that you have another version of Python as your default version and show how to use full paths to pip
to ensure the right installation.
To install torch
, go to https://pytorch.org/, in the colorful grid, click on pip, copy the command, open a command prompt as an administrator (right-click and select "Run as Administrator") then paste the command, which should look something like:
pip install torch===1.5.1 torchvision===0.6.1 -f https://download.pytorch.org/whl/torch_stable.html
Then, edit the command to replace pip replaced with the full path to your version of pip, e.g.:
"C:\Program Files\Python37\Scripts\pip" install torch===1.5.1 torchvision===0.6.1 -f https://download.pytorch.org/whl/torch_stable.html
(You don't need to edit the command as long as Python 3.7 is in your path.)
I expanded my own answer to a PyTorch-specific question in writing this answer.

- 3,321
- 4
- 40
- 58