How to install PyTorch with pipenv and save it to Pipfile and Pipfile.lock?
I’m currently using Pipenv to maintain the Python packages used in a specific project. Most of the downloads I’ve tried so far have worked as intended; that is, I enter pipenv install [package] and it installs the package into the virtual environment, then records the package information into both the Pipfile and Pipfile.lock.
However, I’m running into some problems installing PyTorch.
I’ve tried running pipenv install torch, but every time the locking step fails. Instead, I’ve tried forcing a download directly from the PyTorch website using
pipenv run pip install torch===1.6.0 torchvision===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

And it actually installs! If I run pipenv graph it displays both torch and torchvision with their dependencies. But one problem remains: neither torch nor torchvision are being saved into Pipfile and Pipfile.lock.
Any idea on how I can make this happen?

When you use pipenv run pip install &lt;package&gt;, that skips the custom pipenv operations of updating the Pipfile and the Pipfile.lock. It is basically equivalent to doing a plain pip install &lt;package&gt; as if you did not have/use pipenv.
The only way to also update the Pipfile's is to use pipenv install.
Unfortunately, as I'm posting this, pipenv does not have an equivalent for pip's -f/--find-links option. The best solution is to specify pytorch's &quot;https://download.pytorch.org/whl/&quot; URLs as an  alternative package index, by adding it as a [[source]] in your Pipfile. See this answer from Mohamad and this answer from Mitch McMabers that describes how to do it. I recommend trying out those answers instead.
A less elegant and quite bad alternative is to manually find the correct torch wheel (.whl) links you need, which usually means looking for the correct link from https://download.pytorch.org/whl/torch_stable.html. Then, create/modify the Pipfile with the specific package versions and URLs to the wheels:
[[source]]
name = &quot;pypi&quot;
url = &quot;https://pypi.org/simple&quot;
verify_ssl = true

[requires]
python_version = &quot;3.8&quot;

[packages]
torch = {version = &quot;==1.6.0&quot;, file = &quot;https://download.pytorch.org/whl/cpu/torch-1.6.0-cp38-none-macosx_10_9_x86_64.whl&quot;}
torchvision = {version = &quot;==0.7.0&quot;, file = &quot;https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl&quot;}

Then just do normal pipenv install.
You can confirm the installation with pipenv install --verbose:
Collecting torch==1.6.0
  ...
  Looking up &quot;https://download.pytorch.org/whl/cpu/torch-1.6.0-cp38-none-macosx_10_9_x86_64.whl&quot; in the cache
  Current age based on date: 8
  Starting new HTTPS connection (1): download.pytorch.org:443
  https://download.pytorch.org:443 &quot;GET /whl/cpu/torch-1.6.0-cp38-none-macosx_10_9_x86_64.whl HTTP/1.1&quot; 304 0
  ...
  Added torch==1.6.0 from https://download.pytorch.org/whl/cpu/torch-1.6.0-cp38-none-macosx_10_9_x86_64.whl#egg=torch 
...
Successfully installed torch-1.6.0

Collecting torchvision==0.7.0
  ...
  Looking up &quot;https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl&quot; in the cache
  Current age based on date: 8
  Starting new HTTPS connection (1): download.pytorch.org:443
  https://download.pytorch.org:443 &quot;GET /whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl HTTP/1.1&quot; 304 0
  ...
  Added torchvision==0.7.0 from https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl#egg=torchvision
...
Successfully installed torchvision-0.7.0

This also adds entries to Pipfile.lock:
&quot;torch&quot;: {
    &quot;file&quot;: &quot;https://download.pytorch.org/whl/cpu/torch-1.6.0-cp38-none-macosx_10_9_x86_64.whl&quot;,
    &quot;hashes&quot;: [
        ...
    ],
    &quot;index&quot;: &quot;pypi&quot;,
    &quot;version&quot;: &quot;==1.6.0&quot;
},
&quot;torchvision&quot;: {
    &quot;file&quot;: &quot;https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl&quot;,
    &quot;hashes&quot;: [
        ...
    ],
    &quot;index&quot;: &quot;pypi&quot;,
    &quot;version&quot;: &quot;==0.7.0&quot;
}

With that, you now have a Pipfile and Pipfile.lock that you can check-in/commit to version control and track/manage as you develop your application.
Instead of manually editing the Pipfile, you can also do it from the command line:
(temp) $ pipenv install --verbose &quot;https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl&quot;
Installing https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl...
...
Adding torchvision to Pipfile's [packages]...
✔ Installation Succeeded

That should also add an entry to the Pipfile:
[packages]
...
torchvision = {file = &quot;https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl&quot;}

Of course, this all depends on finding out which wheel you actually need. This can be done by first using a plain pip install &lt;package&gt; with the -f/--find-links option targeting the https://download.pytorch.org/whl/torch_stable.html URL, then checking which wheel it used.

First, let's get the correct .whl file with pip install
$ pipenv run pip install --verbose torchvision==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
Looking in links: https://download.pytorch.org/whl/torch_stable.html
...
Collecting torchvision==0.7.0
  Downloading torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl (387 kB)
...


Remove pip install-ed things from the virtual environment
$ pipenv clean


Repeat installation but using pipenv install
$ pipenv install --verbose &quot;https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl&quot;


Just combine &quot;https://download.pytorch.org/whl/&quot; + .whl filename from step 1



It might seem a bit backwards using pip install first then copying it over to pipenv, but the objective here is to let pipenv update the Pipfile and Pipfile.lock (to support deterministic builds) and to &quot;document&quot; your env for version control.
