当你使用pipenv run pip install <package>
,跳过自定义pipenv
更新Pipfile和Pipfile.lock的操作。基本上相当于做一个简单的pip install <package>
就好像你没有/使用一样pipenv
.
更新 Pipfile 的唯一方法是使用pipenv install
.
不幸的是,当我发布这篇文章时,pipenv没有对应的pip's -f/--find-links option https://stackoverflow.com/q/57647989/2745495。最好的解决方案是将 pytorch 的“https://download.pytorch.org/whl/” URL 指定为替代包索引 https://pipenv-fork.readthedocs.io/en/latest/advanced.html#specifying-package-indexes,通过将其添加为[[source]]
在你的 Pipfile 中。看来自穆罕默德的回答 https://stackoverflow.com/a/68336073/2745495 and 米奇·麦克马伯斯的回答 https://stackoverflow.com/a/71181971/2745495描述了如何做到这一点。我建议尝试这些答案。
一个不太优雅且相当糟糕的替代方案是手动找到正确的火炬轮(.whl
)您需要的链接,这通常意味着从以下位置查找正确的链接https://download.pytorch.org/whl/torch_stable.html https://download.pytorch.org/whl/torch_stable.html。然后,使用以下命令创建/修改 Pipfile具体软件包版本 https://pipenv-fork.readthedocs.io/en/latest/basics.html#specifying-versions-of-a-package以及轮子的 URL:
[[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
:
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 添加条目:
"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,您可以在开发应用程序时将其签入/提交到版本控制并跟踪/管理。
您还可以从命令行执行此操作,而不是手动编辑 Pipfile:
(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 添加一个条目:
[packages]
...
torchvision = {file = "https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl"}
当然,这一切都取决于找出您真正需要的轮子。这可以通过首先使用普通的pip install <package>
与-f
/--find-links
选项针对https://download.pytorch.org/whl/torch_stable.html https://download.pytorch.org/whl/torch_stable.htmlURL,然后检查它使用哪个轮子。
- 首先我们先来了解一下正确的
.whl
文件与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
-来自虚拟环境的东西$ pipenv clean
- Repeat installation but using
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 中的文件名
使用它可能看起来有点倒退pip install
首先然后将其复制到pipenv
,但这里的目标是让pipenv
更新 Pipfile 和 Pipfile.lock(以支持确定性构建)并“记录”您的环境以进行版本控制。