【Python】项目生成requirements.txt文件

原文链接:https://blog.csdn.net/qq_25174673/article/details/85268624

Python项目生成requirements.txt文件

我们在写Python脚本的时候往往会用到很多第三方库,但是当我们把脚本换个环境之后就需要手动安装第三方库,有时候有的第三方库还需要一些别的依赖。为了省事,我们可以导出一个requirements.txt,把需要安装的第三方库放在里面。下面我们就讲一下如何导出这个requirements.txt。

方法一:

pip freeze > requirements.txt

这种方法配合virtualenv才好用,否则会把整个环境中的包都列出来。所以往往不适用

方法二:

使用pipreqs,这个工具的好处是可以通过对项目目录的扫描,发现使用了哪些库,生成依赖清单。

安装

pip install pipreqs

使用

在项目的根目录下 使用 pipreqs ./

要注意:Windows系统下直接使用会出现编码错误:
UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xad in position 173: illegal multibyte sequence。
所以在使用时要指定编码格式。

pipreqs ./ --encoding=utf8

最后

最后生成出来的requirements.txt,可以根据这个文件下载所有依赖

pip install -r requriements.txt

附上pipreqs的使用说明:

Usage:
pipreqs [options]

Options:
–use-local Use ONLY local package info instead of querying PyPI
–pypi-server Use custom PyPi server
–proxy Use Proxy, parameter will be passed to requests library. You can also just set the
environments parameter in your terminal:
$ export HTTP_PROXY=“http://10.10.1.10:3128”
$ export HTTPS_PROXY=“https://10.10.1.10:1080”
–debug Print debug information
–ignore … Ignore extra directories, each separated by a comma
–encoding Use encoding parameter for file open
–savepath Save the list of requirements in the given file
–print Output the list of requirements in the standard output
–force Overwrite existing requirements.txt
–diff Compare modules in requirements.txt to project imports.
–clean Clean up requirements.txt by removing modules that are not imported in project.

使用方法:
pipreqs [options]

选项:
–use-local 仅使用本地报信息而不查询PyPI
–pypi-server 使用自定义的PyPI服务
–proxy 使用Proxy,参数将传递给请求库。你也可以设置
终端中的环境参数:
$ export HTTP_PROXY=“http://10.10.1.10:3128”
$ export HTTPS_PROXY=“https://10.10.1.10:1080”
–debug 打印调试信息
–ignore … 忽略额外的目录,每个目录用逗号分隔
–encoding 使用编码参数打开文件
–savepath 保存给定文件中的需求列表
–print 输出标准输出中的需求列表
–force 覆盖现有的requirements.txt
–diff 将requirements.txt中的模块与项目导入进行比较。
–clean 通过删除未在项目中导入的模块来清理requirements.txt。

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值