前言

我们用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.whlSaved ./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上安装可能会有失败的)

转载于:https://www.cnblogs.com/yoyoketang/p/11409978.html

python笔记40-环境迁移freeze生成requirements.txt相关推荐

  1. Python 项目依赖包 第三方库 生成requirements.txt的两种方法

    python项目如何在另一个环境上重新构建项目所需要的运行环境依赖包? 使用的时候边记载是个很麻烦的事情,总会出现遗漏的包的问题,这个时候手动安装也很麻烦,不能确定代码报错的需要安装的包是什么版本.这 ...

  2. 查找python项目依赖并生成requirements.txt

    多人一起开发项目的时候总是要搭建环境和部署环境的,这个时候必须得有个python第三方包的list,一般都叫做requirements.txt. 如果一个项目使用时virtualenv环境,还好办 p ...

  3. python requirements.txt_python生成requirements.txt的两种方法

    这篇文章主要介绍了python生成requirements.txt的两种方法,每种方法给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 python项目如何在另一个环境上重新构建项目 ...

  4. 查找python项目依赖并生成requirements.txt的两种方法

    项目开发的时候,总是要搭建和部署环境,这时,就需要一个python第三方包的list,一般叫做requirements.txt.如果项目使用virtualenv环境,直接使用pip freeze即可, ...

  5. 查找python项目依赖并生成requirements.txt——pipreqs 真是很好用啊

    查找python项目依赖并生成requirements.txt 转自:http://blog.csdn.net/orangleliu/article/details/60958525 一起开发项目的时 ...

  6. pipreqs------查找python项目依赖并生成requirements.txt

    项目开发的时候,总是要搭建和部署环境,这时,就需要一个python第三方包的list,一般叫做requirements.txt.如果项目使用virtualenv环境,直接使用pip freeze即可, ...

  7. python 项目自动生成requirements.txt文件

    任何应用程序通常需要设置安装所需并依赖一组类库来满足工作要求.要求文件是指定和一次性安装包的依赖项具体一整套方法. 学习python中有什么不懂的地方,小编这里推荐加小编的python学习群:895 ...

  8. python生成requirements.txt的两种方法

    python项目如何在另一个环境上重新构建项目所需要的运行环境依赖包? 使用的时候边记载是个很麻烦的事情,总会出现遗漏的包的问题,这个时候手动安装也很麻烦,不能确定代码报错的需要安装的包是什么版本.这 ...

  9. python如何引用txt_python项目依赖库生成requirements.txt文件

    python项目依赖库生成requirements.txt文件 第一种:pip freeze > requirements.txt 第二种:pipreqs 项目根目录(不能有中文)--encod ...

最新文章

  1. MySQL5.7 : 对隐式锁转换的优化
  2. 双电阻差分电流采样_小小的采样电阻,还真有点门道!
  3. 雨滴桌面时间插件_真香!这 3 款软件,让你的电脑桌面清爽又高效!
  4. IDEA的postfix自定义,自定义postfix
  5. 大数据之-Hadoop3.x_MapReduce_序列化概述---大数据之hadoop3.x工作笔记0094
  6. python定时任务之cron_Python定时任务框架APScheduler 3.0.3 Cron示例
  7. Bug管理的流程和几个重点
  8. python 打印三维数据_Python中的面向对象编程(二):数据隐藏和对象打印
  9. Oracle分析函数
  10. 快速使用 Javassist
  11. macos系统镜像iso_Windows10操作系统iso镜像、微软正版软件下载站:MSDN,我告诉你...
  12. 我对技术的态度是什么样的?
  13. 华为交换机dhcp获取不到_S7706交换机客户端无法通过DHCP获取地址问题
  14. macbook自带python保存文件夹_在mac下查找python包存放路径site-packages的实现方法 在Mac系统下python如何安装第三方函数库?...
  15. matlab二维绘图部分
  16. 联邦学习((Federated Learning,FL)
  17. 【四足机器人】学习笔记 足端轨迹规划和步态规划
  18. 想学ui设计从哪里入手?基础怎么入门学习UI设计呢?
  19. sqli-labs系列——第一关
  20. Spring、Mybatis、Spring MVC整合实例

热门文章

  1. 【win7】NtWaitForKeyedEvent
  2. 【报告分享】2021中国新能源车险生态共建白皮书-艾瑞咨询(附下载)
  3. 电脑数据恢复大师方法!为你解决日常恢复难题
  4. 微软年度开发者大会,请亚马逊VP来站台?
  5. EasyNet,更优雅地搭建接口请求及解析网络框架。
  6. php中设置table表格边框大小,html中关于table边框设置的总结
  7. C++20 Atomic 原子 内存模型(二)
  8. w3cschool教你一天入门AJAX
  9. 锐浪报表 Grid++Report 免注册DLL C/S报表开发(一)
  10. [Android] (Android 视频悬浮窗)