1
 

pip

通常我们熟悉使用的都是 pip, 这个工具确实方便项目管理依赖包。当想把当前项目依赖的包的名称和版本导入指定的 txt 文件中时,只需要执行

pip freeze > ./requirements.txt

此时可以看到项目下面生成了 requirements.txt
然后通过 pip list 查看项目依赖包

Django (1.8.3)
djangorestframework (3.1.3)
docopt (0.6.2)
gnureadline (6.3.3)
ipython (3.2.1)
MySQL-python (1.2.3)
pip (7.1.0)
pipreqs (0.2.8)
Pygments (2.0.2)
requests (2.7.0)
setuptools (18.0.1)
wheel (0.24.0)
yarg (0.1.9)

接着通过 cat ./requirements.txt

Django==1.8.3
djangorestframework==3.1.3
docopt==0.6.2
gnureadline==6.3.3
ipython==3.2.1
MySQL-python==1.2.3
pipreqs==0.2.8
Pygments==2.0.2
requests==2.7.0
wheel==0.24.0
yarg==0.1.9

此时可以看到 pip freeze 生成的列表比 pip list 少了两个包,分别是

  • pip (7.1.0)
  • setuptools (18.0.1)

至于原因:因为 virtualenv 创建虚拟环境时会自动包含了上面的两个依赖包

这种生成 requirements.txt 的方法很通用,可以在其他项目中执行

pip install -r path/requirements.txt

安装相关的依赖包,这是惯用的做法

pipreqs

使用 pipreqs 则需要安装,简单执行

pip install pipreqs

即可

现在看看此工具帮助提示,执行 pipreqs -h

pipreqs - Generate pip requirements.txt file based on imports

Usage:
pipreqs [options] <path> Options:
--use-local Use ONLY local package information instead of querying PyPI
--debug Print debug information
--savepath <file> Save the list of requirements in the given file
--force Overwrite existing requirements.txt

很直白的知道此工具是创建环境依赖包的列表文件

注意

pipreqs - Generate pip requirements.txt file based on imports

此工具是基于 imports,这是什么意思呢,即你的项目引入了哪个包,此工具才会把引入的包写到 requirements.txt 中,是不是觉得要比 pip freeze 干净,注意生成的是 requirements.txt 文件,而不是 requirement.txt

例子

执行 pipreqs --use-local ./ 生成 requirements.txt

因为项目只引入了 djangopygments, 此时 cat requirements.txt, 文件中只包含了两条数据

Django == 1.8.3
Pygments == 2.0.2

引入问题

引入不是很完整,比如数据库依赖包,就不会包含进来,

pip-compile

使用前需要安装 pip install pip-tools 如果权限不够,请 sudo

使用步骤 1

先在项目目录中创建 requirements.in 文件,然后手动写入包文件名称

例如:requirements.in (例子随便写的)

django
yolk

使用步骤2

执行 pip-compile requirements.in, 然后 cat requirements.txt

#
# This file is autogenerated by pip-compile
# Make changes in requirements.in, then run this to update:
#
# pip-compile requirements.in
#
django==1.8.3
yolk==0.4.3 # The following packages are commented out because they are
# considered to be unsafe in a requirements file:
# setuptools==18.1 # via yolk

结论

生成 xx.txt 文件的方法有很多,上面三种方法各有优劣

名称 优点 缺点
pip freeze 包含列表完全 不相关的依赖包也会包含进来
pipreqs 只会包含项目 imports 的包 包含列表不是很完全
pip-compile 精准控制项目依赖包 需要手动操作,不方便

Djanog|requirements.txt生成的更多相关文章

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

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

  2. pycharm中使用配置好的virtualenv环境,自动生成和安装requirements.txt依赖

    1.手动建立: 第一步 建立虚拟环境 Windows cmd: pip install virtualenv 创建虚拟环境目录 env 激活虚拟环境 C:\Python27\Scripts\env\S ...

  3. Python 生成requirements文件以及使用requirements.txt部署项目

    生成requirements.txt 当你的项目不再你的本地时,为了方便在新环境中配置好环境变量,你的项目需要一个记录其所有依赖包以及它们版本号的文件夹requirements.txt 文件. pip ...

  4. requirements.txt文件教程

    方法有2种,命令都是一样,只是执行地方不一样 此方法主要用于迁移新环境使用,为防止代码出现问题,最好使用原装库,所以就有了迁移代码和库的操作 第一种:在pycharm中,左下角有个双重正方形,点击里面 ...

  5. Python requirements.txt

    安装 pip install -r requirements.txt 生成 # 将当前环境下的所有以来导出, 配合虚拟环境更佳 pip freeze > requirements.txt

  6. 从pip+requirements.txt+virtualenv管理依赖到使用pipenv管理依赖-修改布署方式

    背景: 已经使用pip+requirements.txt+virtualenv管理了项目一段时间,为了不要每次都 导出依赖(本地),安装依赖(服务器) 现在要使用pipenv来管理项目的依赖关系 思路 ...

  7. python 环境迁移之requirements.txt (window环境)

    一.背景 1.有时候你本地用python编写代码,然后本地也安装了很多的库,那么你要迁移到服务器上,那么该怎么快速弄环境库呢?           #第二,三步就可以解决. 2.有时候你本地用pyth ...

  8. pip自动生成requirements.txt依赖关系清单

    Python项目中经常会带requirements.txt文件,里面是项目所依赖的包的列表,也就是依赖关系清单,这个清单也可以使用pip命令自动生成. pip命令: 1 pip freeze > ...

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

    主要使用目的: 任何应用程序通常需要设置安装所需并依赖一组类库来满足工作要求.通过requirements.txt可以一次性安装程序所需要和依赖的包. 为工程生成requirements.txt的两种 ...

随机推荐

  1. ecplise中修改reviewboard密码

    一.概述 如果想在ecplise中修改reviewboard密码,步骤请参考如下图片:

  2. maven 压缩、合并 js, css

    转载自:http://blog.csdn.net/fangxing80/article/details/17639607 我们知道在 Web 应用开发中为了提高客户端响应速度,需要将页面使用的资源最小 ...

  3. HTML5学习之新增标签

    转自:http://www.cnblogs.com/fly_dragon/archive/2012/05/25/2516142.html 作者:FlyDragon 一.引言 在本节中,笔者将向大家讲述 ...

  4. CSS3学习笔记之loading动画

    效果截图: HTML代码: <div class="divBox"> <div class="loader"> <div clas ...

  5. 【Foreign】魔法 [组合数][质因数分解]

    魔法 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 4 10 ...

  6. [POJ2954&POJ1265]皮克定理的应用两例

    皮克定理: 在一个多边形中.用I表示多边形内部的点数,E来表示多边形边上的点数,S表示多边形的面积. 满足:S:=I+E/2-1; 解决这一类题可能运用到的: 求E,一条边(x1,y1,x2,y2)上 ...

  7. python2.7字典转换成json时中文字符串变成unicode的问题:

    参考:http://blog.csdn.net/u014431852/article/details/53058951 编码问题: python2.7字典转换成json时中文字符串变成unicode的 ...

  8. Opencv 中透视变换函数对IplImage图像变换时出现的问题?

    最近一直在做视频稳像的项目,为了简化部分实现,使用了部分Opencv的函数,其中包括Opencv中对IplImage进行同时变换的函数cvWarpPerspective(src, dst,...) 发 ...

  9. linux驱动基础系列--Linux I2c驱动分析

    前言 主要是想对Linux I2c驱动框架有一个整体的把控,因此会忽略协议上的某些细节,同时里面涉及到的一些驱动基础,比如平台驱动.设备模型.sysfs等也不进行详细说明原理,涉及到i2c协议部分也只 ...

  10. 移植WordPress到Ubuntu16.04

    移植WordPress到Ubuntu16.04 新建 模板 小书匠 移植WordPress到Ubuntu16.04 搭建好LAMP环境后,可以按照以下方法,将本地站点移植到服务器上. 以WordPre ...