目录
一、Linux安装编译依赖
1、Linux安装GCC编译器
2、Linux安装Make工具
3、Linux安装OpenSSL依赖
二、Linux安装python3.6.8
1、Python3.6.8下载
2、Python3.6.8编译安装
3、环境变量配置
4、验证Python环境
三、pip3 离线安装依赖库和pytorch
1、离线下载依赖库
2、离线安装依赖库和pytorch
3、在线安装pytorch
四、常见依赖问题解决
1、_bz2模块依赖问题
2、_lzma 模块依赖问题
一、Linux安装编译依赖
1、Linux安装GCC编译器
Linux离线编译编译Python需要gcc编译器编译,如果没有安装直接编译会出现以下错误
代码语言:javascript复制configure: error: no acceptable C compiler found in $PATH
yum -y install gcc
2、Linux安装Make工具
Linux离线编译编译Python需要Make工具安装,如果没有安装直接编译会出现以下错误
代码语言:javascript复制-bash: make: command not found
yum -y install make
3、Linux安装OpenSSL依赖
pytorch 需要依赖OpenSSL模块,如果没有OpenSSL依赖,直接安装会出现以下错误
Can't connect to HTTPS URL because the SSL module is not available
验证OpenSSL模块
代码语言:javascript复制openssl version
yum安装OpenSSL
代码语言:javascript复制yum -y install openssl openssl-devel
rpm查看OpenSSL模块
代码语言:javascript复制rpm -q openssl
rpm -q openssl-devel
二、Linux安装python3.6.8
1、Python3.6.8下载
Python官方网站:Welcome to Python.org
Python历史版本:Index of /ftp/python/
Python3.6.8下载:https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
2、Python3.6.8编译安装
代码语言:javascript复制yum -y install zlib*
代码语言:javascript复制tar zxvf Python-3.6.8.tgz -C /usr/local/
代码语言:javascript复制cd /usr/local/Python-3.6.8 && make distclean
python3.6.6以下需要加 --with-ssl 参数,python3.6.8可加可不加,加了无伤大雅不影响编译安装
代码语言:javascript复制./configure --prefix=/usr/local/python3.6.8 --with-ssl --enable-optimizations
代码语言:javascript复制make && make install
提示以下信息则表示编译成功
Collecting setuptools Collecting pip Installing collected packages: setuptools, pip Successfully installed pip-18.1 setuptools-40.6.2
3、环境变量配置
创建软连接
代码语言:javascript复制ln -s /usr/local/python3.6.8 /usr/local/python3
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
配置环境变量
代码语言:javascript复制echo '
# python3 config
export PYTHON3_HOME=/usr/local/python3
export PATH=$PATH:$PYTHON3_HOME/bin' >> /etc/profile
代码语言:javascript复制source /etc/profile
tail -n 6 /etc/profile
4、验证Python环境
测试python安装
代码语言:javascript复制python -V
python3 -V
更新 pip 模块
代码语言:javascript复制pip3 install --upgrade pip
查看已安装模块
代码语言:javascript复制pip3 list
三、pip3 离线安装依赖库和pytorch
1、离线下载依赖库
功能 | 模块 | 官方地址 | 安装包链接 |
---|---|---|---|
numpy | numpy | numpy · PyPI | numpy-1.19.5-cp36-cp36m-manylinux2010_x86_64.whl |
Pillow | PIL | Pillow · PyPI | Pillow-8.4.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
typing_extensions | typing_extensions | typing-extensions · PyPI | typing_extensions-4.0.1-py3-none-any.whl |
dataclasses | dataclasses | dataclasses · PyPI | dataclasses-0.8-py3-none-any.whl |
torchaudio | torchaudio | torchaudio · PyPI | torchaudio-0.10.1-cp36-cp36m-manylinux1_x86_64.whl |
torchvision | torchvision | torchvision · PyPI | torchvision-0.11.2-cp36-cp36m-manylinux1_x86_64.whl |
torch | torch | torch · PyPI | torch-1.10.1-cp36-cp36m-manylinux1_x86_64.whl |
2、离线安装依赖库和pytorch
代码语言:javascript复制pip3 install numpy-1.19.5-cp36-cp36m-manylinux2010_x86_64.whl
pip3 install Pillow-8.4.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
pip3 install typing_extensions-4.0.1-py3-none-any.whl
pip3 install dataclasses-0.8-py3-none-any.whl
pip3 install torch-1.10.1-cp36-cp36m-manylinux1_x86_64.whl
pip3 install torchaudio-0.10.1-cp36-cp36m-manylinux1_x86_64.whl
pip3 install torchvision-0.11.2-cp36-cp36m-manylinux1_x86_64.whl
3、在线安装pytorch
Start Locally | PyTorch
代码语言:javascript复制pip3 install torch==1.10.1 cpu torchvision==0.11.2 cpu torchaudio==0.10.1 cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html
四、常见依赖问题解决
1、_bz2模块依赖问题
pytorch 需要依赖bz2模块,如果没有bz2依赖,在导入torchvision 模块时会出现以下错误
from _bz2 import BZ2Compressor, BZ2Decompressor ModuleNotFoundError: No module named '_bz2'
yum 安装 bz2 依赖
代码语言:javascript复制yum -y install bzip2 bzip2
下载 bz2 依赖:_bz2.cpython-36m-x86_64-linux-gnu.so
代码语言:javascript复制wget https://gitee.com/AlEinstein/utils/raw/master/linux/python/_bz2.cpython-36m-x86_64-linux-gnu.so
代码语言:javascript复制cp _bz2.cpython-36m-x86_64-linux-gnu.so /usr/local/python3/lib/python3.6/lib-dynload/
代码语言:javascript复制python3
代码语言:javascript复制from _bz2 import BZ2Compressor, BZ2Decompressor
2、_lzma 模块依赖问题
File "/usr/local/python3/lib/python3.6/lzma.py", line 27, in <module> from _lzma import * ModuleNotFoundError: No module named '_lzma'
Linux 和 Python安装 lzma 依赖
代码语言:javascript复制yum -y install xz-devel python-backports-lzma
代码语言:javascript复制pip3 install backports.lzma
修改 lzma.py 模块文件内容(在27、28行)
代码语言:javascript复制cp /usr/local/python3/lib/python3.6/lzma.py /usr/local/python3/lib/python3.6/lzma.py.init
代码语言:javascript复制vim /usr/local/python3/lib/python3.6/lzma.py
代码语言:javascript复制try:
from _lzma import *
from _lzma import _encode_filter_properties, _decode_filter_properties
except ImportError:
from backports.lzma import *
from backports.lzma import _encode_filter_properties, _decode_filter_properties
测试验证
代码语言:javascript复制python3
代码语言:javascript复制import torchvision
Win10 Python3.6.8 安装 PyTorch:Win10 Python3.6.8 安装 PyTorch_星辰大数据的博客-CSDN博客
Linux安装机器学习框架pytorch:https://blog.csdn.net/qq262593421/article/details/109994870