python 打包离线环境 pip安装

1、有网并且网速快时:生成的依赖文件

pip freeze > requirements.txt

在新环境直接

pip install -r requirements.txt

pip install -r requestment.txt路径 --target=安装目录

2、新环境没网,网络不佳

旧环境中 有些情况下我们需要下载N个第三方包,或者下载的包依赖其它包,一个个下载非常浪费时间。这时我们可以通过如下两种方式的命令批量下载。:

pip download -r requirement.txt  -d ./python_package

方式1
pip download -d /tmp/packagesdir <packagename>
方式2
pip download -d /tmp/packagesdir -r requirements.txt

就将requirement中的依赖 全部下载成whl文件 放在python_package中

在新环境安装时:

pip install -r requirements.txt --no-index --find-links=./python_package

单个安装:

pip install XXX.whl

pip install 包名==版本号 --target=指定安装路径
pip download 包名==版本号 -d 指定下载路径

3、更换python的pip下载国内源

参考https://www.cnblogs.com/xiaocaitailang/p/16657680.html

在默认情况下 pip 使用的是国外的软件包源,在下载的时候的速度很慢,花费的时间比较长。故而需要给anaconda和pip安装国内镜像源。这里就简单的记录下我了解的方法,方便我,也方便大家使用。

 常见的国内源如下:

清华:https://pypi.tuna.tsinghua.edu.cn/simple
中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
阿里云:http://mirrors.aliyun.com/pypi/simple/
豆瓣:http://pypi.douban.com/simple/
百度:https://mirror.baidu.com/pypi/simple

3.1、临时使用国内源

平时在安装的时候都是直接使用pip install xxx来安装python包,这里如果是临时使用国内原只需要在后面补上-i xxx,这里xxx为国内源的网址。以使用清华源下载pandas为例,如下:

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

3.2、永久修改

方法一:修改配置文件

(1)Mac/Linux

在Mac/Linux系统下:配置文件位置在 ~/.pip/pip.conf

如果是新安装的就没有这个文件,需要自己创建.pip目录:

mkdir ~/.pip

完成后,打开pip.conf修改为(以清华源为例)

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

(2)Windows

win环境pip的配置文件在C:\Users\xxx\AppData\Roaming\pip\pip.ini里面,可以打开此文件(没有就自己创建一个)直接修改,同样以清华源为例:

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

 

来源https://blog.csdn.net/weixin_55249340/article/details/124911171

 





posted @ 2022-10-18 17:04  浩浩学习  阅读(1170)  评论(0编辑  收藏  举报