Python项目生成requirements.txt文件

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] <path>

Options:
    --use-local                            Use ONLY local package info instead of querying PyPI
    --pypi-server <url>               Use custom PyPi server
    --proxy <url>                        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 <dirs>...                   Ignore extra directories, each separated by a comma
    --encoding <charset>           Use encoding parameter for file open
    --savepath <file>                  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 <file>                            Compare modules in requirements.txt to project imports.
    --clean <file>                        Clean up requirements.txt by removing modules that are not imported in project.

使用方法:
    pipreqs [options] <path>

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

猜你喜欢

转载自blog.csdn.net/qq_25174673/article/details/85268624