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. HDU 1698 Just a Hook(线段树

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. Spring Boot(一)

    1.注解  @EnableAutoConfiguration 官方文档:The @EnableAutoConfiguration annotation is often placed on your ...

  3. L2-002. 链表去重---模拟

    https://www.patest.cn/contests/gplt/L2-002 L2-002. 链表去重 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 ...

  4. 【数据结构】bzoj2957楼房重建

    Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些 ...

  5. 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)

    Description Given n numbers, your task is to insert '+' or '-' in front of each number to construct ...

  6. [bzoj1692][Usaco2007 Dec]队列变换——贪心+后缀数组

    Brief Description 给定一个数列,您每次可以把数列的最前面的数或最后面的数移动到新数列的开头,使得新数列字典序最小.输出这个新序列. Algorithm Design 首先我们可以使用 ...

  7. 安装l Xposed Framework

    How to install Xposed Framework on Android 4.x.x :   1. For Android 4.0.3 through 4.4.4 Visit this X ...

  8. 【Mysql优化】MySQL Profiling 的使用

    要想优化一条 Query,我们就需要清楚的知道这条 Query 的性能瓶颈到底在哪里,是消耗的 CPU计算太多,还是需要的的 IO 操作太多?要想能够清楚的了解这些信息,在 MySQL 5.0 和 M ...

  9. Invalidation queue with "bit-sliceability"

    BACKGROUND, FEATURES In a computer system having more than one memory storage facility, a special da ...

  10. kuangbin带你飞 最短路 题解

    求一个图最短路边的办法.好像下面的那个有问题.单向边和双向边一定是有区别的.这个比较容易.参照该文的最短路网络流题目和连通图题目一题求最短路关节边 另外上述2个题目的代码好像有问题. 在UVALIVE ...