Mat*_*bow 2 python windows pytorch pipenv torchvision
我目前正在使用 Pipenv 来维护特定项目中使用的 Python 包。到目前为止,我尝试过的大多数下载都按预期工作;也就是我进入pipenv install [package],它把包安装到虚拟环境中,然后将包信息记录到Pipfile和Pipfile.lock中。
但是,我在安装 PyTorch 时遇到了一些问题。
我试过运行pipenv install torch,但每次锁定步骤失败。相反,我尝试使用以下命令直接从 PyTorch 网站强制下载
pipenv run pip install torch===1.6.0 torchvision===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
Run Code Online (Sandbox Code Playgroud)
它实际上安装了!如果我运行pipenv graph它,它会同时显示 torch 和 torchvision 及其依赖项。但是仍然存在一个问题:torch 和 torchvision 都没有保存到 Pipfile 和 Pipfile.lock 中。
关于如何实现这一点的任何想法?
使用 时pipenv run pip install <package>,会跳过pipenv更新 Pipfile 和 Pipfile.lock的自定义操作。它基本上相当于做一个普通的pip install <package>,就好像你没有/使用一样pipenv。
更新 Pipfiles 的唯一方法是使用pipenv install.
不幸的是,我张贴这一点,pipenv不具有同等pip的-f/--find-links选项。一种解决方法是手动找到.whl您需要的正确火炬轮 ( ) 链接,这对于 pytorch,通常意味着从https://download.pytorch.org/whl/torch_stable.html 中寻找正确的链接(我将展示一个提示如何在下面执行此操作)。
然后,使用特定的包版本和轮子的 URL创建/修改 Pipfile :
[[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"}
Run Code Online (Sandbox Code Playgroud)
然后就正常了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
Run Code Online (Sandbox Code Playgroud)
这也会向 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"
}
Run Code Online (Sandbox Code Playgroud)
有了它,您现在就有了一个 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
Run Code Online (Sandbox Code Playgroud)
这还应该向 Pipfile 添加一个条目:
[packages]
...
torchvision = {file = "https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl"}
Run Code Online (Sandbox Code Playgroud)
当然,这一切都取决于找出您真正需要的轮子。这可以通过首先使用针对https://download.pytorch.org/whl/torch_stable.html URL 的pip install <package>带有-f/--find-links选项的纯文本来完成,然后检查它使用了哪个轮子。
.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)
...
Run Code Online (Sandbox Code Playgroud)
pip install从虚拟环境中删除-ed 的东西
$ pipenv clean
Run Code Online (Sandbox Code Playgroud)
pipenv install
$ pipenv install --verbose "https://download.pytorch.org/whl/torchvision-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl"
Run Code Online (Sandbox Code Playgroud)
.whl步骤 1 中的“https://download.pytorch.org/whl/”+文件名首先使用pip install然后将其复制到似乎有点倒退pipenv,但这里的目标是让pipenv更新 Pipfile 和 Pipfile.lock (以支持确定性构建)并“记录”您的 env 以进行版本控制。
小智 5
您可以通过将 PyTorch 源添加到 pip 文件来安装它,如下所示:
[[source]]
name = "pytorch"
url = "https://download.pytorch.org/whl/"
verify_ssl = true
[packages]
torch = {index = "pytorch",version = "==1.9.0"}
torchvision = {index ="pytorch", version= "==0.10.0"}
torchaudio = {index ="pytorch", version= "==0.9.0"}
[requires]
python_version = "3.7"
Run Code Online (Sandbox Code Playgroud)
然后运行pipenv install
注:索引指的是源名称。
您可以对任何类似的情况执行此操作:例如:安装 pytorch-geometric:
[[source]]
name = "pytorch-geometric"
url = "https://pytorch-geometric.com/whl/torch-1.9.0+cu111.html"
verify_ssl = true
[packages]
torch-scatter = {index= "pytorch-geometric", version= "==2.0.7"}
torch-sparse = {index= "pytorch-geometric", version= "==0.6.10"}
torch-cluster = {index= "pytorch-geometric", version= "==1.5.9"}
torch-geometric = {index= "pytorch-geometric", version= "==1.3.2"}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1346 次 |
| 最近记录: |