如何使用 pipenv 安装 PyTorch 并将其保存到 Pipfile 和 Pipfile.lock? [英] How to install PyTorch with pipenv and save it to Pipfile and Pipfile.lock?

查看:110
本文介绍了如何使用 pipenv 安装 PyTorch 并将其保存到 Pipfile 和 Pipfile.lock?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用 Pipenv 来维护特定项目中使用的 Python 包.到目前为止,我尝试过的大多数下载都按预期工作;也就是我输入pipenv install [package],它把包安装到虚拟环境中,然后将包信息记录到Pipfile和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.

但是,我在安装 PyTorch 时遇到了一些问题.

However, I’m running into some problems installing PyTorch.

我试过运行 pipenv install torch,但每次锁定步骤都失败了.相反,我尝试使用

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

它确实安装了!如果我运行 pipenv graph,它会同时显示 torch 和 torchvision 及其依赖项.但是仍然存在一个问题:torch 和 torchvision 都没有保存到 Pipfile 和 Pipfile.lock 中.

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?

推荐答案

当你使用 pipenv run pip install 时,会跳过自定义的 pipenv 操作更新 Pipfile 和 Pipfile.lock.它基本上等同于做一个普通的 pip install ,就好像你没有/使用 pipenv 一样.

When you use pipenv run pip install <package>, that skips the custom pipenv operations of updating the Pipfile and the Pipfile.lock. It is basically equivalent to doing a plain pip install <package> as if you did not have/use pipenv.

更新 Pipfiles 的唯一方法是使用 pipenv install.

The only way to also update the Pipfiles is to use pipenv install.

不幸的是,当我发布此内容时,pipenv 没有 pip 的等效项-f/--find-links 选项.一种解决方法是手动找到您需要的正确火炬轮 (.whl) 链接,对于 pytorch,这通常意味着从 https://download.pytorch.org/whl/torch_stable.html(我将在下面展示如何执行此操作的提示).

Unfortunately, as I'm posting this, pipenv does not have an equivalent for pip's -f/--find-links option. One workaround is to manually find the correct torch wheel (.whl) links you need, which with pytorch, usually means looking for the correct link from https://download.pytorch.org/whl/torch_stable.html (I'll show a tip how to do this below).

然后,使用 特定的软件包版本 和轮子的 URL:

Then, create/modify the Pipfile with the specific package versions and URLs to the wheels:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[requires]
python_version = "3.8"

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

然后就做正常的pipenv install.

您可以使用 pipenv install --verbose 确认安装:

You can confirm the installation with pipenv install --verbose:

Collecting torch==1.6.0
  ...
  Looking up "https://download.pytorch.org/whl/cpu/torch-1.6.0-cp38-none-macosx_10_9_x86_64.whl" in the cache
  Current age based on date: 8
  Starting new HTTPS connection (1): download.pytorch.org:443
  https://download.pytorch.org:443 "GET /whl/cpu/torch-1.6.0-cp38-none-macosx_10_9_x86_64.whl HTTP/1.1" 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 "https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl" in the cache
  Current age based on date: 8
  Starting new HTTPS connection (1): download.pytorch.org:443
  https://download.pytorch.org:443 "GET /whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl HTTP/1.1" 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

这也会向 Pipfile.lock 添加条目:

This also adds entries to Pipfile.lock:

"torch": {
    "file": "https://download.pytorch.org/whl/cpu/torch-1.6.0-cp38-none-macosx_10_9_x86_64.whl",
    "hashes": [
        ...
    ],
    "index": "pypi",
    "version": "==1.6.0"
},
"torchvision": {
    "file": "https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl",
    "hashes": [
        ...
    ],
    "index": "pypi",
    "version": "==0.7.0"
}

这样,您现在就有了一个 Pipfile 和 Pipfile.lock,您可以在开发应用程序时签入/提交到版本控制和跟踪/管理.

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.

除了手动编辑 Pipfile,您还可以从命令行进行:

Instead of manually editing the Pipfile, you can also do it from the command line:

(temp) $ pipenv install --verbose "https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl"
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

这还应该在 Pipfile 中添加一个条目:

That should also add an entry to the Pipfile:

[packages]
...
torchvision = {file = "https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl"}

当然,这一切都取决于找出您真正需要哪个轮子.这可以通过首先使用一个普通的 pip install -f/--find-links 选项来实现 --find-linksa href="https://download.pytorch.org/whl/torch_stable.html" rel="nofollow noreferrer">https://download.pytorch.org/whl/torch_stable.html URL,然后检查用的是哪个轮子.

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

  1. 首先,让我们使用 pip install 获取正确的 .whl 文件
$ 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)
...

  • 从虚拟环境中删除 pip install 相关的东西

    $ pipenv clean
    

  • 重复安装但使用 pipenv install

    $ pipenv install --verbose "https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl"
    

    • 只需结合https://download.pytorch.org/whl/"即可+ .whl 步骤 1 中的文件名
      • Just combine "https://download.pytorch.org/whl/" + .whl filename from step 1
      • 首先使用 pip install 然后将其复制到 pipenv 似乎有点倒退,但这里的目标是让 pipenv 更新Pipfile 和 Pipfile.lock(支持确定性构建)并记录"您的版本控制环境.

        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 "document" your env for version control.

        这篇关于如何使用 pipenv 安装 PyTorch 并将其保存到 Pipfile 和 Pipfile.lock?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆