凉城旧巷
Python从入门到自闭,Java从自闭到放弃,数据库从删库到跑路,Linux从rm -rf到完犊子!!!

Conda离线安装Python包

一、境况

在设备断网的情况下,手动下载Python包,然后上传到断网设备,再安装。很有可能出现依赖包下载不全,导致包无法使用的问题。为此,通过conda离线下载方式来解决该问题,conda同pip一样,可以解决依赖包问题。

 

二、实验

1、实验对象

一台联网设备(虚拟机)、一台断网设备(虚拟机、服务器等)

 

2、联网设备下载Python包

  • 为保证python包和Python版本兼容,所以最好创建一个同版本Python的虚拟环境

1)创建虚拟环境

# 例如Python版本为3.8.5

# 创建python 3.8.5 虚拟环境
conda create -n pytorch python==3.8.5

# 切换到虚拟环境
conda activate pytorch

 

2)下载Python包

  • 可配置下载国内镜像channel

    #查看当前conda配置
    conda config --show channels
    
    # 添加清华镜像channel
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud//pytorch/
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
     
    #设置搜索是显示通道地址
    conda config --set show_channel_urls yes
    

conda install pymysql --download-only

如图所示,pymysql及其依赖包都被下载保存。

 

3、打包Python包

1)挑选Python包所需文件

在下载完成以后,可以通过conda info 查看conda中包缓存的路径,在路径中,根据 2-2) 中提示的package和build去找出对应的包。

 

2)打包

tar -czvf pymysql.tar.gz cryxxxxx cryxxxx.conda pymysqlxxxx pymysqlxxxx.conda

 

4、上传断网设备

通过scp、硬盘拷贝等方式将包上传

 

5、断网设备解压

conda info  # 查看 package cache

tar -zxvf pymysq.tar.gz ./pkgs

rm -rf ./pkgs/cache ./pkgs/urls  ./pkgs/urls.txt

mv ./pkgs/*  /path/miniconda/pkgs/

 

6、断网设备安装包

conda install pymysql --offline   # 不联网安装
posted on 2022-07-01 15:16  凉城旧巷  阅读(5932)  评论(0编辑  收藏  举报