python笔记40-环境迁移freeze生成requirements.txt

前言

我们用python在本地电脑上开发完成一个python自动化项目用例,或者开发完成一个django项目。
需要部署到另外一台电脑或者服务器上的时候,需要导入python相关的依赖包,可以用freeze一键生成requirements.txt文件

  • pip freeze >requirements.txt  #  生成一个迁移文件

  • pip install -r requirements.txt           #   安装依赖包

freeze生成文件

比如我在本地电脑开发完成了python的一个项目,会涉及到很多第三方的包,并且版本号都得一一对应,这样才能保证迁移过去不会有问题。
在不知道freeze这个功能的时候,我是先pip list 查看所有的第三方包,然后一个个pip安装,感觉挺傻的。
自从看到别人项目里面有个requirements.txt文件,才知道原来可以通过pip freeze一键生成

pip freeze >requirements.txt

[root@yoyo ~]# pip freeze >requirements.txt
[root@yoyo ~]# cat requirements.txt
APScheduler==3.5.3
asn1crypto==0.24.0
# ....太多省略了
xlrd==1.2.0
xlwt==1.3.0
[root@yoyo ~]#

这样在当前目录就会生成一个requirements.txt文件,包当前项目的所有第三方包和版本号都会导出来

pip 安装

requirements.txt文件生成后,我们不需要一个个pip安装,可以在requirements.txt文件当前目录使用pip一键安装

pip install -r requirements.txt

[root@yoyo ~]# pip install -r requirements.txt
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: APScheduler==3.5.3 in /usr/local/python3/lib/python3.6/site-packages (from -r requirements.txt (line 1)) (3.5.3)
Requirement already satisfied: asn1crypto==0.24.0 in /usr/local/python3/lib/python3.6/site-packages (from -r requirements.txt (line 2)) (0.24.0)
Requirement already satisfied: atomicwrites==1.3.0 in /usr/local/python3/lib/python3.6/site-packages (from -r requirements.txt (line 3)) (1.3.0)
# ....太多省略了
Requirement already satisfied: xlwt==1.3.0 in /usr/local/python3/lib/python3.6/site-packages (from -r requirements.txt (line 62)) (1.3.0)
Requirement already satisfied: setuptools>=0.7 in /usr/local/python3/lib/python3.6/site-packages (from APScheduler==3.5.3->-r requirements.txt (line 1)) (40.6.2)
[root@yoyo ~]#

本地安装

如果你们公司的服务器限制了网络下载,那么只能通过本地安装了,freeze也可以下载本地包安装。
假设A服务器是没有网络的,你需要在A服务器上安装python第三方包,那么你先找个可以连网络的服务器B,在服务器B上先下载需要的安装包

服务器B上下载安装包

先使用pip freeze到处需要安装的包,比如我想安装以下包

[root@yoyo site-pkg]# cat requirements.txt
ix==1.12.0
attrs==18.2.0
py==1.7.0
pluggy==0.6.0
atomicwrites==1.3.0
more-itertools==6.0.0
pytest==3.6.3

pip download下载相关安装包

pip download -r requirements.txt

如果是阿里云服务器,会出现如下报错,那么加个参数—trusted-host mirrors.aliyun.com即可

[root@yoyo site-pkg]# pip download -r requirements.txt
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting ix==1.12.0 (from -r requirements.txt (line 1))
The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored.
If this repository is available via HTTPS we recommend you use HTTPS instead,
otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.aliyun.com'.
Could not find a version that satisfies the requirement ix==1.12.0 (from -r requirements.txt (line 1)) (from versions: )
No matching distribution found for ix==1.12.0 (from -r requirements.txt (line 1))
You are using pip version 18.1, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

加上—trusted-host mirrors.aliyun.com参数

pip download -r requirements.txt —trusted-host mirrors.aliyun.com

[root@yoyo site-pkg]# pip download -r requirements.txt --trusted-host mirrors.aliyun.com
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting six==1.12.0 (from -r requirements.txt (line 1))
File was already downloaded /root/site-pkg/six-1.12.0-py2.py3-none-any.whl
Collecting attrs==18.2.0 (from -r requirements.txt (line 2))
Downloading http://mirrors.aliyun.com/pypi/packages/3a/e1/5f9023cc983f1a628a8c2fd051ad19e76ff7b142a0faf329336f9a62a514/attrs-18.2.0-py2.py3-none-any.whl
Saved ./attrs-18.2.0-py2.py3-none-any.whl
Successfully downloaded six attrs py pluggy atomicwrites more-itertools pytest setuptools
[root@yoyo site-pkg]# ll
total 968
-rw-r--r-- 1 root root 5885 Aug 25 23:17 atomicwrites-1.3.0-py2.py3-none-any.whl
-rw-r--r-- 1 root root 34713 Aug 25 23:17 attrs-18.2.0-py2.py3-none-any.whl
-rw-r--r-- 1 root root 52353 Aug 25 23:17 more_itertools-6.0.0-py3-none-any.whl
-rw-r--r-- 1 root root 13723 Aug 25 23:17 pluggy-0.6.0-py3-none-any.whl
-rw-r--r-- 1 root root 83960 Aug 25 23:17 py-1.7.0-py2.py3-none-any.whl
-rw-r--r-- 1 root root 195787 Aug 25 23:17 pytest-3.6.3-py2.py3-none-any.whl
-rw-r--r-- 1 root root 106 Aug 25 23:16 requirements.txt
-rw-r--r-- 1 root root 576332 Aug 25 23:17 setuptools-41.2.0-py2.py3-none-any.whl
-rw-r--r-- 1 root root 10586 Aug 25 23:17 six-1.12.0-py2.py3-none-any.whl
[root@yoyo site-pkg]#

这样就可以下载到.whl后缀的本地包了

服务器A上本地安装

由于服务器A上无法联网,可以先把上面下载到的相关包传到服务器A上,然后就可以使用pip本地安装了.
.whl后缀的包是可以直接使用pip本地安装的。

pip install pytest-3.6.3-py2.py3-none-any.whl

[root@yoyo site-pkg]# pip install pytest-3.6.3-py2.py3-none-any.whl
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: pytest==3.6.3 from file:///root/site-pkg/pytest-3.6.3-py2.py3-none-any.whl in /usr/local/python3/lib/python3.6/site-packages (3.6.3)
Requirement already satisfied: pluggy<0.7,>=0.5 in /usr/local/python3/lib/python3.6/site-packages (from pytest==3.6.3) (0.6.0)
Requirement already satisfied: six>=1.10.0 in /usr/local/python3/lib/python3.6/site-packages (from pytest==3.6.3) (1.12.0)
Requirement already satisfied: atomicwrites>=1.0 in /usr/local/python3/lib/python3.6/site-packages (from pytest==3.6.3) (1.3.0)
Requirement already satisfied: more-itertools>=4.0.0 in /usr/local/python3/lib/python3.6/site-packages (from pytest==3.6.3) (6.0.0)
Requirement already satisfied: attrs>=17.4.0 in /usr/local/python3/lib/python3.6/site-packages (from pytest==3.6.3) (18.2.0)
Requirement already satisfied: py>=1.5.0 in /usr/local/python3/lib/python3.6/site-packages (from pytest==3.6.3) (1.7.0)
Requirement already satisfied: setuptools in /usr/local/python3/lib/python3.6/site-packages (from pytest==3.6.3) (40.6.2)
[root@yoyo site-pkg]#

如果安装过程中有的包会有其他的依赖包,需先安装依赖包,一个个的安装就可以了。
(注意:windows系统上导出的包,在linux上安装可能会有失败的)

(0)

相关推荐

  • windows下pip包管理工具基本命令操作

    #升级pip包管理工具 C:\Users\Administrator>python -m pip install --upgrade pip Looking in indexes: http:/ ...

  • python离线安装外部依赖包

     注意:后面发现很多问题都是pip版本太低造成的,所以在往下做之前,可以先升级一下pip为最新版本: python -m pip install -U pip 1.制作requirement.txt ...

  • Python项目如何生成requirements.txt文件

    Pyhon项目中,一般都会有一个 requirements.txt 文件,这个文件主要是用于记录当前项目下的所有依赖包及其精确的版本号,以方便在一个新环境下更快的进行部署. 使用 pip freeze ...

  • Python项目生成依赖包清单requirements.txt方法总结

    一.背景 工作中跨环境运行相同项目时,总会出现缺少各种包的问题,还需要一个个安装,相当的蛋疼,这里推荐一个工具叫pipreqs,可以通过一条命令直接生成项目所有依赖包清单requirements.tx ...

  • python笔记25-mock-server之moco环境搭建

    前言 mock除了用在单元测试过程中,还有一个用途,当前端开发在开发页面的时候,需要服务端提供API接口 此时服务端没开发完成,或者说没搭建测试环境,这个时候前端开发会自己mock一个api服务端,自 ...

  • python笔记41-虚拟环境virtualenv

    前言 如果你是一个python初学者,我是不建议你搞python虚拟环境的,我看到很多python的初学者同学,使用最新版的pycharm,新建一个工程时候默认就是venu虚拟环境. 然后在使用cmd ...

  • python一键生成生成requirements文件

    表白:黑白圣堂血天使,天剑鬼刀阿修罗.  讲解对象:/python一键生成生成requirements文件  作者:融水公子 rsgz Python3 教程 python教程 http://www.r ...

  • 胡希恕伤寒论学习笔记——40

    40.伤寒表不解,心下有水气,干呕,发热而咳,或渴,或利,或噎,或小便不利少腹满,或喘者,小青龙汤主之. 小青龙汤方 麻黄 芍药 细辛 干姜 甘草(炙) 桂枝(去皮)各三两 五味子半升 半夏半升(洗) ...

  • 完整版Python笔记|超级有用

    完整版Python笔记|超级有用

  • 【python笔记】python java 语法,对比学习

    【python笔记】python java 语法,对比学习

  • python笔记2-冒泡排序

    前言 面试的时候经常有面试官喜欢问如何进行冒泡排序?这个问题相信能难倒一批英雄好汉,本篇就详细讲解如何用python进行冒泡排序. 一.基本原理 1.概念: 冒泡排序(Bubble Sort),是一种 ...