有时候为了教学的需要,机房的网络很慢或者机房的机器不允许上外网,而有需要在这些机房进行PyTorch的教学或实验,则需要以离线安装PyTorch的方式。
第一部分:首先,先找一台能够上网的机器去离线下载PyTorch所需要的安装包。
当然,首先需要在此台能够上网的机器上安装相关的python环境,本文是Python3.10.14版本。
接着,打开python.exe文件所在的位置
接着,输入在资源管理器中输入cmd命令
进入命令提示符窗口,如下图:
继续输入命令:pip download torch torchvision torchaudio -d ./pytorch_packages
将所有包下载当前python.exe所在的子目录pytorch_packages中
最后等待下载完成,如提示“Successfully downloaded.......”表示成功下载了所有的包。
下载完成后,目录pytorch_packages如下图所示:
然后,将上述整个目录复制到其它无法上网的机器上。
注意:以下操作是在无法上网的即离线的机器上操作。
首先需要在此台机器上安装Anaconda3和python3.10.14(需要的包可以在网上找到并复制到离线的机器上安装即可。)
本文以Anaconda3(Anaconda3-2021.05-Windows-x86_64.exe)为例说明。
一、安装Anaconda及python3.10虚拟环境
1、安装Anaconda3-2023.09-0-Windows-x86_64.exe,下载该文件到本地机器,然后双击打开该文件,按照提示下一步下一步即可。可以到以下国内镜像下载即可
2、安装python3.10虚拟环境
首先,打开Anaconda Prompt(Anaconda3)如下图所示:
接着,创建python=3.10虚拟环境
conda create -n pytorch python=3.10
(base) C:\Users\Administrator>conda create -n pytorch python=3.10
Collecting package metadata (current_repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.10.1
latest version: 24.3.0
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: d:\Anaconda3\envs\pytorch
added / updated specs:
- python=3.10
The following packages will be downloaded:
package | build
---------------------------|-----------------
bzip2-1.0.8 | h2bbff1b_5 78 KB
openssl-3.0.13 | h2bbff1b_0 7.4 MB
pip-23.3.1 | py310haa95532_0 2.9 MB
python-3.10.14 | he1021f5_0 15.9 MB
setuptools-68.2.2 | py310haa95532_0 942 KB
sqlite-3.41.2 | h2bbff1b_0 894 KB
tk-8.6.12 | h2bbff1b_0 3.1 MB
tzdata-2024a | h04d1e81_0 116 KB
wheel-0.41.2 | py310haa95532_0 127 KB
xz-5.4.6 | h8cc25b3_0 587 KB
zlib-1.2.13 | h8cc25b3_0 113 KB
------------------------------------------------------------
Total: 32.1 MB
The following NEW packages will be INSTALLED:
bzip2 pkgs/main/win-64::bzip2-1.0.8-h2bbff1b_5
ca-certificates pkgs/main/win-64::ca-certificates-2024.3.11-haa95532_0
libffi pkgs/main/win-64::libffi-3.4.4-hd77b12b_0
openssl pkgs/main/win-64::openssl-3.0.13-h2bbff1b_0
pip pkgs/main/win-64::pip-23.3.1-py310haa95532_0
python pkgs/main/win-64::python-3.10.14-he1021f5_0
setuptools pkgs/main/win-64::setuptools-68.2.2-py310haa95532_0
sqlite pkgs/main/win-64::sqlite-3.41.2-h2bbff1b_0
tk pkgs/main/win-64::tk-8.6.12-h2bbff1b_0
tzdata pkgs/main/noarch::tzdata-2024a-h04d1e81_0
vc pkgs/main/win-64::vc-14.2-h21ff451_1
vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.27.29016-h5e58377_2
wheel pkgs/main/win-64::wheel-0.41.2-py310haa95532_0
xz pkgs/main/win-64::xz-5.4.6-h8cc25b3_0
zlib pkgs/main/win-64::zlib-1.2.13-h8cc25b3_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
pip-23.3.1 | 2.9 MB | ############################################################################ | 100%
wheel-0.41.2 | 127 KB | ############################################################################ | 100%
bzip2-1.0.8 | 78 KB | ############################################################################ | 100%
openssl-3.0.13 | 7.4 MB | ############################################################################ | 100%
xz-5.4.6 | 587 KB | ############################################################################ | 100%
tzdata-2024a | 116 KB | ############################################################################ | 100%
setuptools-68.2.2 | 942 KB | ############################################################################ | 100%
python-3.10.14 | 15.9 MB | ############################################################################ | 100%
zlib-1.2.13 | 113 KB | ############################################################################ | 100%
tk-8.6.12 | 3.1 MB | ############################################################################ | 100%
sqlite-3.41.2 | 894 KB | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate pytorch
#
# To deactivate an active environment, use
#
# $ conda deactivate
- 创建环境结束后,使用以下指令查看已有的环境:conda env list
(base) C:\Users\Administrator>conda env list
# conda environments:
#
base * d:\Anaconda3
pytorch d:\Anaconda3\envs\pytorch
(base) C:\Users\Administrator>
- 进入已有的环境,输入以下语句切换环境:conda activate pytorch
(base) C:\Users\Administrator>conda activate pytorch
(pytorch) C:\Users\Administrator>
第二部分:安装torch依赖包及torch
1、安装依赖包filelock
(pytorch) D:\pytorch_packages>
(pytorch) D:\pytorch_packages>pip install filelock-3.13.1-py3-none-any.whl
Processing d:\pytorch_packages\filelock-3.13.1-py3-none-any.whl
Installing collected packages: filelock
Successfully installed filelock-3.13.1
(pytorch) D:\pytorch_packages>
2、安装 typing_extensions
(pytorch) D:\pytorch_packages>pip install typing_extensions-4.10.0-py3-none-any.whl
Processing d:\pytorch_packages\typing_extensions-4.10.0-py3-none-any.whl
Installing collected packages: typing-extensions
Successfully installed typing-extensions-4.10.0
3、安装mpmath
(pytorch) D:\pytorch_packages>pip install mpmath-1.3.0-py3-none-any.whl
Processing d:\pytorch_packages\mpmath-1.3.0-py3-none-any.whl
Installing collected packages: mpmath
Successfully installed mpmath-1.3.0
4、 安装sympy
(pytorch) D:\pytorch_packages>pip install sympy-1.12-py3-none-any.whl
Processing d:\pytorch_packages\sympy-1.12-py3-none-any.whl
Requirement already satisfied: mpmath>=0.19 in d:\anaconda3\envs\pytorch\lib\site-packages (from sympy==1.12) (1.3.0)
Installing collected packages: sympy
Successfully installed sympy-1.12
5、安装networkx
(pytorch) D:\pytorch_packages>pip install networkx-3.2.1-py3-none-any.whl
Processing d:\pytorch_packages\networkx-3.2.1-py3-none-any.whl
Installing collected packages: networkx
Successfully installed networkx-3.2.1
6、安装MarkupSafe
(pytorch) D:\pytorch_packages>pip install MarkupSafe-2.1.5-cp310-cp310-win_amd64.whl
Processing d:\pytorch_packages\markupsafe-2.1.5-cp310-cp310-win_amd64.whl
Installing collected packages: MarkupSafe
Successfully installed MarkupSafe-2.1.5
(pytorch) D:\pytorch_packages>
7、安装Jinja
(pytorch) D:\pytorch_packages>pip install Jinja2-3.1.3-py3-none-any.whl
Processing d:\pytorch_packages\jinja2-3.1.3-py3-none-any.whl
Requirement already satisfied: MarkupSafe>=2.0 in d:\anaconda3\envs\pytorch\lib\site-packages (from Jinja2==3.1.3) (2.1.5)
Installing collected packages: Jinja2
Successfully installed Jinja2-3.1.3
8、安装fsspec
(pytorch) D:\pytorch_packages>pip install fsspec-2024.3.1-py3-none-any.whl
Processing d:\pytorch_packages\fsspec-2024.3.1-py3-none-any.whl
Installing collected packages: fsspec
Successfully installed fsspec-2024.3.1
9、安装torch
(pytorch) D:\pytorch_packages>pip install torch-2.2.1-cp310-cp310-win_amd64.whl
Processing d:\pytorch_packages\torch-2.2.1-cp310-cp310-win_amd64.whl
Requirement already satisfied: filelock in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1) (3.13.1)
Requirement already satisfied: typing-extensions>=4.8.0 in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1) (4.10.0)
Requirement already satisfied: sympy in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1) (1.12)
Requirement already satisfied: networkx in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1) (3.2.1)
Requirement already satisfied: jinja2 in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1) (3.1.3)
Requirement already satisfied: fsspec in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1) (2024.3.1)
Requirement already satisfied: MarkupSafe>=2.0 in d:\anaconda3\envs\pytorch\lib\site-packages (from jinja2->torch==2.2.1) (2.1.5)
Requirement already satisfied: mpmath>=0.19 in d:\anaconda3\envs\pytorch\lib\site-packages (from sympy->torch==2.2.1) (1.3.0)
Installing collected packages: torch
Successfully installed torch-2.2.1
(pytorch) D:\pytorch_packages>
第三部分:安装torchvision
1、安装依赖numpy-1.26.4-cp310-cp310-win_amd64.whl
(pytorch) D:\pytorch_packages>pip install numpy-1.26.4-cp310-cp310-win_amd64.whl
Processing d:\pytorch_packages\numpy-1.26.4-cp310-cp310-win_amd64.whl
Installing collected packages: numpy
Successfully installed numpy-1.26.4
- 安装依赖pillow-10.2.0-cp310-cp310-win_amd64.whl
(pytorch) D:\pytorch_packages>pip install pillow-10.2.0-cp310-cp310-win_amd64.whl
Processing d:\pytorch_packages\pillow-10.2.0-cp310-cp310-win_amd64.whl
Installing collected packages: pillow
Successfully installed pillow-10.2.0
- 安装torchvision
(pytorch) D:\pytorch_packages>pip install torchvision-0.17.1-cp310-cp310-win_amd64.whl
Processing d:\pytorch_packages\torchvision-0.17.1-cp310-cp310-win_amd64.whl
Requirement already satisfied: numpy in d:\anaconda3\envs\pytorch\lib\site-packages (from torchvision==0.17.1) (1.26.4)
Requirement already satisfied: torch==2.2.1 in d:\anaconda3\envs\pytorch\lib\site-packages (from torchvision==0.17.1) (2.2.1)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in d:\anaconda3\envs\pytorch\lib\site-packages (from torchvision==0.17.1) (10.2.0)
Requirement already satisfied: filelock in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1->torchvision==0.17.1) (3.13.1)
Requirement already satisfied: typing-extensions>=4.8.0 in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1->torchvision==0.17.1) (4.10.0)
Requirement already satisfied: sympy in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1->torchvision==0.17.1) (1.12)
Requirement already satisfied: networkx in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1->torchvision==0.17.1) (3.2.1)
Requirement already satisfied: jinja2 in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1->torchvision==0.17.1) (3.1.3)
Requirement already satisfied: fsspec in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1->torchvision==0.17.1) (2024.3.1)
Requirement already satisfied: MarkupSafe>=2.0 in d:\anaconda3\envs\pytorch\lib\site-packages (from jinja2->torch==2.2.1->torchvision==0.17.1) (2.1.5)
Requirement already satisfied: mpmath>=0.19 in d:\anaconda3\envs\pytorch\lib\site-packages (from sympy->torch==2.2.1->torchvision==0.17.1) (1.3.0)
Installing collected packages: torchvision
Successfully installed torchvision-0.17.1
第四部分:安装torchaudio-2.2.1-cp310-cp310-win_amd64.whl
- 直接安装torchaudio
(pytorch) D:\pytorch_packages>pip install torchaudio-2.2.1-cp310-cp310-win_amd64.whl
Processing d:\pytorch_packages\torchaudio-2.2.1-cp310-cp310-win_amd64.whl
Requirement already satisfied: torch==2.2.1 in d:\anaconda3\envs\pytorch\lib\site-packages (from torchaudio==2.2.1) (2.2.1)
Requirement already satisfied: filelock in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1->torchaudio==2.2.1) (3.13.1)
Requirement already satisfied: typing-extensions>=4.8.0 in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1->torchaudio==2.2.1) (4.10.0)
Requirement already satisfied: sympy in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1->torchaudio==2.2.1) (1.12)
Requirement already satisfied: networkx in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1->torchaudio==2.2.1) (3.2.1)
Requirement already satisfied: jinja2 in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1->torchaudio==2.2.1) (3.1.3)
Requirement already satisfied: fsspec in d:\anaconda3\envs\pytorch\lib\site-packages (from torch==2.2.1->torchaudio==2.2.1) (2024.3.1)
Requirement already satisfied: MarkupSafe>=2.0 in d:\anaconda3\envs\pytorch\lib\site-packages (from jinja2->torch==2.2.1->torchaudio==2.2.1) (2.1.5)
Requirement already satisfied: mpmath>=0.19 in d:\anaconda3\envs\pytorch\lib\site-packages (from sympy->torch==2.2.1->torchaudio==2.2.1) (1.3.0)
Installing collected packages: torchaudio
Successfully installed torchaudio-2.2.1
第五部分:验证Pytorch是否安装成功
1、输入python查看版本
(pytorch) D:\pytorch_packages>python
Python 3.10.14 | packaged by Anaconda, Inc. | (main, Mar 21 2024, 16:20:14) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
2、导入torche,输入代码定义一个张量并输出张量的维度及张量的类型,如果现实如下结果表示环境验证成功。
(pytorch) D:\pytorch_packages>python
Python 3.10.14 | packaged by Anaconda, Inc. | (main, Mar 21 2024, 16:20:14) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> t=torch.tensor([[1,2,3],[4,5,6]])
>>> print(t,t.shape,t.dtype)
tensor([[1, 2, 3],
[4, 5, 6]]) torch.Size([2, 3]) torch.int64