python使用国内源安装&离线安装第三方库

python第三方包使用国内源安装&离线安装

一、将pip源更换到国内镜像

国内源下载速度较快

国内镜像 地址
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云 http://mirrors.aliyun.com/pypi/simple/
豆瓣 http://pypi.douban.com/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
华中科技大学 http://pypi.hustunique.com/

1.1 临时方法

命令行后面加上指定源

pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple/

1.2 永久方法

1.2.1 mac

mkdir ~/.pip

tee ~/.pip/pip.conf <<-'EOF'
[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=https://pypi.tuna.tsinghua.edu.cn
EOF

1.2.2 win

直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,然后新建文件pip.ini,即 %HOMEPATH%\pip\pip.ini,在pip.ini文件中输入以下内容:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn

1.2.3 Linux

修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)
内容如下:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn

二、第三方包离线下载及安装

2.1 导出当前环境已安装的包对应的版本信息

pip freeze > requirements.txt

requirements.txt文件内容格式

attrs==19.3.0
certifi==2019.11.28
importlib-metadata==1.5.0
more-itertools==8.2.0
packaging==20.3
pluggy==0.13.1
py==1.8.1
pyparsing==2.4.6
pytest==5.4.1
six==1.14.0
wcwidth==0.1.8
zipp==3.1.0

2.2 下载离线包

1.会同步下载相关到依赖包
2.默认下载到当前文件夹内
3.如果pip指定了国内源,可以会无法下载,亲测阿里和豆瓣的不行,清华的可以。

2.2.1 下载单个包

pip download pymysql

2.2.2 下载多个包(通过文件)

pip download -d 保存包的目录 -r requirements.txt

2.2.3下载到指定路径

pip download pymysql -d 路径

2.3 安装离线包(在requirements.txt所在目录下执行)

  1. 将离线包和包列表文件(requirements.txt)导入到需要安装包到电脑上
  2. 激活需要安装包到python环境
  3. 进入包列表文件(requirements.txt)所在的路径下(可以通过修改此文件来决定需要安装哪些包)
  4. 执行命令,安装requirements.txt内对应的包
pip install --no-index --find-links 离线包存放路径 -r requirements.txt

你可能感兴趣的:(python使用国内源安装&离线安装第三方库)